• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there any way that we can prevent an object from being Garbage collected?  RSS feed

 
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way that we can prevent an object from being Garbage collected?
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think you are saying something like "pinning" a data block in oracle. IMHO, there is nothing other than keeping a reference!. think of a servlet which is extending GenericServler. the GenericServelt object resides in the JVM and when called standalone wont "stand/pinned" to the JVM if there are no references. But when i put the GenericServlet in a webserver, it keeps the reference to the deployed servlet and so it is not garbage collected.
Hope this may be of help!
 
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by senthil sen:
Is there any way that we can prevent an object from being Garbage collected?


you mean in java? GB is automatically in java. i think the only way you can prevent it be GB collected is to keep it occupied, like set static variable...
 
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, as long as there is any variable anywhere in the JVM that holds a reference to the object it will not be gc'd.
Slight clarification - as long as it is not a WeakReference - which is another whole conversation.
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the best way to prevent GC on a single object, is to hold a static reference to it in the main thread.
To prevent GC in the JVM, you can explore usage of a non standard JVM option -

-GB.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!