Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep object on GC destruction attempt

I got this idea while thinking of ways to recycling objects. I am doing this as an experiment to see how memory pooling works and I realize that in 99% of scenario this is very unnecessary .

However, I have a question. Is there a way to force the GC to keep the object? In other words, can I tell the GC not to destroy an object and say instead have new reference created in a list that holds available to use objects? The issue, though I have not tested this, is that if I add something like this to a list:

~myObject()
{
     ((List<myObject>)HttpContext.Current.Items[typeof(T).ToString()]).add(this);//Lets assume this is ASP
}

it will add a pointer to this object into the list, but if the object is destroyed I will get a null pointer exception because the object is no longer there. However, maybe I could tell the GC not to collect this item and thus keeping the object?

I know this is the type of stuff that most programmers would go "why the hell would you do this?". But on the other hand programming is about trying new and learning new things. Any suggestions, thoughts, implementatons? Any help would be greatly appreciated!

like image 894
Serguei Fedorov Avatar asked Oct 11 '12 21:10

Serguei Fedorov


People also ask

Can we force to garbage collector to destroy objects?

No, you can't destroy a specific object. It is possible to invoke the garbage collector, which will look for objects to destroy, but it's almost never a good idea. Save this answer.

Can an object be garbage-collected while it is still reachable?

It's possible to have unused objects that are still reachable by an application because the developer simply forgot to dereference them. Such objects cannot be garbage-collected.

How can we prevent objects from garbage collection?

By Increasing Heap Memory This approach leads to the garbage collector running infrequently, however when it runs, will take longer than before to complete the garbage collection task.


2 Answers

Yes, this is legitimate. It is called 'resurrection'. When you assign the this reference to somewhere that is 'live', then the object is no longer considered garbage.

You will also need to reregister for finalization using GC.ReRegisterForFinalize(this), or the next time the object becomes garbage, it will not be finalized (the destructor will not be called).

You can read more about object resurrection in this MSDN Magazine article.

like image 133
porges Avatar answered Oct 30 '22 01:10

porges


Yes, this is possible and it even has a name: resurrection.

but if the object is destroyed I will get a null pointer exception because the object is no longer there.

Much worse, you would get an invalid pointer (reference) error. And possibly a blue screen or crashing server. But luckily the CLR won't let that happen. Simply putting a doomed instance in any kind of list makes it reachable again and then it will not be reclaimed.

When you want your object to be recycled multiple times you will have to call GC.ReRegisterForFinalize(x) on it.

Most practical answer though: don't do it. The destructor alone accounts for considerable overhead and there are many ways to get this wrong.

like image 44
Henk Holterman Avatar answered Oct 30 '22 01:10

Henk Holterman