Memory leaks with C#

Hello,

I have created two classes, Country and City, Country has a List<> collection which has list of Cities and each City object has Parent property which references its parent Country Object.

When i fill the List in the country object more than one time, i must clear the items in the list " list.Clear()" before refilling, but clearing the items will not allow GC to remove the city and country objects as they both references each others!!

So should i release the references of the of the Parent country by setting Parent property to null before refilling or there is no need for this action?

Thanks and regards

[663 byte] By [TheMaj0r] at [2008-1-10]
# 1
TheMaj0r wrote:

When i fill the List in the country object more than one time, i must clear the items in the list " list.Clear()" before refilling, but clearing the items will not allow GC to remove the city and country objects as they both references each others!!

The GC is capable of handling circular references. As long as nothing else is referencing the Country and the City objects, they can be garbage collected.

MattiasSj?gren at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 2
Yes, the .Net garbage collection is very sophisticated, and works extremely well.

However, it does very lazy freeing, for performance reasons, which sometimes leads people to think it isn't working properly when they don't see the memory usage of their applications dropping after they've disposed of a load of objects. The GC will tend to only actually free stuff up when the system is relatively low on memory. In practise, this doesn't cause a problem at all, since the memory will become available when it is needed.

MatthewWatson at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 3
TheMaj0r wrote:

When i fill the List in the country object more than one time, i must clear the items in the list " list.Clear()" before refilling, but clearing the items will not allow GC to remove the city and country objects as they both references each others!!

Whenever you create an object which has references to other objects or delegate subscriptions, one should create a dispose that will properly release those objects and delegate subscriptions. This informs the GC that all is well and the memory can be reclaimed.

Think of it this way, the GC is the house cleaner you have hired. Yes it picks up clothes while cleaning, but what if you throw your clothes on the bed? The cleaner may not know to clean those up, though you know. By creating a dispose, a clothes hamper, there is no question as to what should be done.

Managed code can leak, meaning that if references hold onto objects then the GC will not dispose of those objects. Its best to create a dispose that will remove all references to objects and unhook any events that the object may be subscribed to.

Here are some things to read up on:

OmegaMan at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 4

Thanks all for your replies, Althought i heard before about the ability of the GC to deal with circular references but i think it would be faster to release references manually in the dispose method rather than let them with each other references..

Am I right? or both are the same?

TheMaj0r at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...
# 5
If the references are to class objects that have a Dispose() method, you certainly should call their Dispose() methods from the referencing class's Dispose(). But otherwise, you really don't need to do anything.
MatthewWatson at 2007-10-3 > top of Msdn Tech,Visual C#,Visual C# General...