Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this object not garbage collected

In the following code segment, I am wondering why testvectors is not collected after the function call. I see memory usage go up to 270Mb and then stay there forever.

This function is directly called from Main.

private static void increaseMemoryUsage()
{
    List<List<float>> testvectors = new List<List<float>>();
    int vectorNum = 250 * 250;
    Random rand = new Random();

    for (int i = 0; i < vectorNum; i++)
    {
        List<Single> vec = new List<Single>();

        for (int j = 0; j < 1000; j++)
                {
            vec.Add((Single)rand.NextDouble());
        }
        testvectors.Add(vec);
    }
}
like image 213
Johnyy Avatar asked Feb 20 '12 13:02

Johnyy


People also ask

What do an object be garbage collected?

An object is eligible to be garbage collected if its reference variable is lost from the program during execution. Sometimes they are also called unreachable objects. What is reference of an object? The new operator dynamically allocates memory for an object and returns a reference to it.

Why are unreachable objects not garbage collected?

There is a chance that the garbage collection was not performed, for example if the object to be allocated was so huge that it was obvious to the JVM that no amount of garbage collection could free enough space, or if the heap dump was triggered by another event.

How do you ensure that an instance is not garbage collected?

We have three ways to achieve same - 1) Increasing the Heap -Eden space size . 2) Create Singleton class with Static reference . 3) Override finalize() method and never let that object dereference.


2 Answers

Don't confuse garbage collection with reference counting. The memory is freed when the runtime decides, not always when it's no longer referenced. To quote:

Garbage collection occurs when one of the following conditions is true:

The system has low physical memory.

The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This means that a threshold of acceptable memory usage has been exceeded on the managed heap. This threshold is continuously adjusted as the process runs.

The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

Read this if you're interested:

http://msdn.microsoft.com/en-us/library/0xy59wtx.aspx

like image 50
Joe Avatar answered Oct 10 '22 02:10

Joe


The GC can run when it wants. And that can be much later. There is no obligation to free some memory directly after the last reference goes away. Your array will get collected on the next Gen2 collection.

Unless you keep allocating memory, it will likely never run after the function returns.

You can manually trigger a GC with GC.Collect(), but that's generally discouraged.

like image 32
CodesInChaos Avatar answered Oct 10 '22 00:10

CodesInChaos