How to avoid garbage collection.

  • Thread starter Thread starter jkalyansagar
  • Start date Start date
J

jkalyansagar

I need to maintain an object, that should not be collected by garbage
collector. This object will have lot of centralized data of
application.But i don't want to create static object. Please let me
know, is there any way to achieve it.
 
Hello (e-mail address removed),
I need to maintain an object, that should not be collected by garbage
collector. This object will have lot of centralized data of
application.But i don't want to create static object. Please let me
know, is there any way to achieve it.

As long as it is referenced by you application, it will not be garbage collected.
What makes you afraid that it might be?
 
I need to maintain an object, that should not be collected by garbage
collector. This object will have lot of centralized data of
application.But i don't want to create static object. Please let me
know, is there any way to achieve it.

Maybe just use "fixed" keyword. This is generally used for pointers,
but fixes object in memory. I'm not sure if this is what you are
looking for.
 
I need to maintain an object, that should not be collected by garbage
collector. This object will have lot of centralized data of
application.But i don't want to create static object. Please let me
know, is there any way to achieve it.

Objects in .Net are only garbage-collected after they become unreachable. As
long as your program keeps a reference to the object it is "reachable"
through that reference, and therefore it will not be collected.
 
After serious thinking (e-mail address removed) wrote :
I need to maintain an object, that should not be collected by garbage
collector. This object will have lot of centralized data of
application.But i don't want to create static object. Please let me
know, is there any way to achieve it.

Why don't you want to create a "static object" (i guess you mean
'singleton')? Some other requirement that you didn't tell?

Hans Kesting
 
Maybe just use "fixed" keyword. This is generally used for pointers,
but fixes object in memory. I'm not sure if this is what you are
looking for.

"fixed" can _only_ be used to declare local variables of pointer
types. Besides, it has nothing to do with keeping the objects alive
for GC purposes; it merely prevents the GC from moving the object
pointed to around during compacting phase.
 
I need to maintain an object, that should not be collected by garbage
collector. This object will have lot of centralized data of
application.But i don't want to create static object. Please let me
know, is there any way to achieve it.

Hi,

If you do not want it to be static, how you will make a reference to
it?
You like it or not, somewhere , something will have to be static :)
 
Back
Top