How to avoid garbage collection.

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

Jesse Houwing

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?
 
J

ja_1410

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

Alberto Poblacion

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

Hans Kesting

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
 
P

Pavel Minaev

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

Ignacio Machin ( .NET/ C# MVP )

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 :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top