1

for sure this question has been asked before, but I couldn't find any thread about this, so also a link to an answered question would be helpful:

I have a volume class (VoxelVolume) that contains an array of Slices (Slice).

public class VoxelVolume
{
    Slice[] mySlices;

    public VoxelVolume(int noOfSlices)
    {
        mySlices = new Slice[noOfSlices];

        for (int i = 0; i < mySlices.Length; i++)
        {
            mySlices[i] = new Slice(this);
        }
    }
}

The volume knows the Slices and the Slices know the Volume:

public class Slice
{
    public VoxelVolume Volume
    {
        get;
        private set;
    }

    public Slice(VoxelVolume aVolume)
    {
        Volume = aVolume;
    }
}

The problem is that the volume never gets garbage collected since there is a mutual object referencing. For that reason I implemented the reference from the slice to the volume as WeakReference:

public class Slice
{
    WeakReference myVolume;

    VoxelVolume Volume
    {
        get
        {
            return (VoxelVolume)myVolume.Target;
        }
    }

    public Slice(VoxelVolume aVolume)
    {
        myVolume = new WeakReference(aVolume);
    }
}

Now the garbage collection works, but with the effort of an additional WeakReference. Since I can have many thousands of slices the effort of WeakReferences is very high.

Edit: First it didn't show up, but since we are using a lot of memory there was a "memory leak". Therefore we had a look into the referenced and available objects using JetBrains dotTrace. There we have seen that a lot of objects are available although they are not used anymore. As I said before using the WeakReference solved the problem.

The question is, how the TreeView solves these problems, since the TreeNode also has a knowledge of the TreeView.

Do you know a suitable solution to this?

Thank you Martin

msedi
  • 1,437
  • 15
  • 25
  • http://stackoverflow.com/questions/8840567/garbage-collector-and-circular-reference – m0s Jul 23 '12 at 07:21
  • The .NET garbage collector correctly handles unreferenced cycles in the object graph. E.g. If object A references object B and B references A, providing that neither A, nor B are reachable from a GC root, they will be collected, even though they have a reference to each other. How are you determining that your Volume is not being collected? – Iridium Jul 23 '12 at 07:24
  • Hi Iridium. You are right. Please see my Edit in the text above. – msedi Jul 23 '12 at 10:36
  • Since the WeakReference version does not exhibit the issue, it suggests that your Slices are still reachable somehow. Does dotTrace give any indication as to what (other than the VoxelVolumed) may be referencing the Slices? – Iridium Jul 23 '12 at 11:32
  • I will have a look at it again. If I know more, I will add a comment here. Thank you. – msedi Jul 23 '12 at 15:41

2 Answers2

3

The problem is that the volume never gets garbage collected since there is a mutual object referencing.

That's only going to be a problem if something has access to either the Slice or the VoxelVolume. When you no longer have references to either object, they will both be eligible for garbage collection. Cycles in objects aren't a problem in .NET, as it doesn't use reference counting.

Of course, you do need to make sure you don't have a reference to either the Slice or the VoxelVolume from any GC roots. You haven't really given us enough context to know what's going on here.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi Jon. I made an edit in my text, that I did the debugging using JetBrains dotTrace. As I see from all your posts, cyclic references can be resolved by the GC. So it seems that there is something wrong with my code. – msedi Jul 23 '12 at 10:38
0

GC engine in CLR should collect cyclic referenced objects, as long as no one else referencing any of them.

It's also a good idea to implement IDisposable pattern and clear the references when you decide you don't need them anymore. It could reduce the chance of large amount of memory leak in case something bad kept a reference that you didn't notice.

Whyllee
  • 281
  • 1
  • 7