This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method that keep Garbage Collection form collecting? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Method that keep Garbage Collection form collecting?" Watch "Method that keep Garbage Collection form collecting?" New topic
Author

Method that keep Garbage Collection form collecting?

Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 70

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

Joined: Sep 20, 2010
Posts: 3052
    
    1

Did you try it out? What happened?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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
Greenhorn

Joined: Jan 18, 2012
Posts: 25

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.


Alex Theedom Senior Java Programmer.
Joey Sanchez
Ranch Hand

Joined: Jun 23, 2011
Posts: 70

Is there a way to check i out? I mean, how can I know how many objects are eligible?
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
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

Joined: Jul 09, 2001
Posts: 1123
Please have a look at How to prevent an object from getting garbage collected?

Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16695
    
  19



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

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Method that keep Garbage Collection form collecting?
 
Similar Threads
garbage collection
finalize()
finalize() question
Garbage Collector
a doubt on garbage collection.