• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method that keep Garbage Collection form collecting?

 
Ranch Hand
Posts: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try it out? What happened?
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 116
11
Spring Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 101
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at How to prevent an object from getting garbage collected?

Regards,
Dan
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic