Search code examples
c#garbage-collectionweak-references

Garbage Collection for mutually referenced objects in C#


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


Solution

  • 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.