• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method that keep Garbage Collection form collecting?

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was doing some exercises of Bates & Sierrra book and I came up that question.

Create a class that has a method such that the first time the garbage collector attempts to collect a given instance, this method will keep garbage collector from collecting that instance at that point.

Could it be an option the method finalize(), and pass a reference to the object to another?
 
Stephan van Hulst
Bartender
Pie
Posts: 6113
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try it out? What happened?
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Joey.
I think so. In the finalize method of the object, you can try to assign the object instance to a static variable and see.
This is one of the many examples. It may not be a perfect example though.


But there is no guarantee that finalize method will run. If the garbage collector never GC the object, the finalized method won't be called.
 
Alex Theedom
Author
Ranch Hand
Posts: 77
5
Java Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the finalize() method is called and the object is saved the object could still be GC'ed later on because the finalize() method is only called once on each object. Thus the next time the JVM tries to GC this object it will not run the finalize() method and the object will be destroyed.

So the poor little object only has one chance to live, the second time certain death.

 
Joey Sanchez
Ranch Hand
Posts: 88
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to check i out? I mean, how can I know how many objects are eligible?
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Joey.
When we say an object is eligible for GC, it means it is only possible to be GC. But there is no guarantee that it will be GCed.
When an object has no variable referring to it, it will be eligible for GC.
The object is just a like a piece of trash inside a trash can. The trash will be ready to be picked up by the truck. But there is no guarantee that the truck will come by to pick up the trash.


For more information, you can check on some other posts titled "Garbage Collection" and "Garbage Collection 2".
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please have a look at How to prevent an object from getting garbage collected?

Regards,
Dan
 
Henry Wong
author
Marshal
Pie
Posts: 21415
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


While this is very interesting at a theoretical level -- this is, of course, completely not practical. After all, if an object is still needed, why would an application make it not reachable? And even if it is deemed not needed (and hence, made not reachable), and then deemed needed at a later time, what kind of need can actually wait for a GC cycle for it to be recovered?

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic