File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

tony kanvas
Ranch Hand

Joined: Oct 26, 2002
Posts: 97
((You should directly invoke the garbage collection system before entering a time critical section of code.))
Could any one explain to me this statement I could not understand it. if you can with exapmle plez
Thanks in advs
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Where did that quote come from?
Realize that it's impossible to directly call the Garbage Collector.
from the System.gc()API:
Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.

[ October 31, 2002: Message edited by: Jessica Sant ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you start to run out of memory in a time critical section of code, garbage collection will possibly start. This may take some time, maybe significantly delaying your thread. So invoking System.gc() before you start yout time critical section may help avoid the memory shortage. But don't rely on it actually causing garbage collection to occur.
Notice all those mays and maybes that's the way garbage collection is.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy -- for the purposes of the exam, I concur with the other folks - NO GUARANTEES. However, in practice, most VMs do respond, so I used it in the past (especially before HotSpot when it *really* made a huge difference) to help prevent the garbage collector from running at an "inappropriate" time. As a game developer then, I defined "inappropriate" as "right in the middle of an intense animation scene." Like, the last thing you want is the guy to be running away from the alien Tiger and then suddenly freeze -- mid-stride -- while gc happens. So I always liked to invoke System.gc() before something that needs to proceed smoothly, OR when I know that the user will not notice. For example, when the user is reading a Help Screen, that is a nice time for gc to happen.
But with HotSpot, this really does not seem to matter much anymore.
I wish I had a garbage collection system at home. But it would have to be smart enough to know that -- even though I still have a live reference to something -- I will never use it/wear it/eat it again if my life depends on it


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Kathy Sierra:
I wish I had a garbage collection system at home. But it would have to be smart enough to know that -- even though I still have a live reference to something -- I will never use it/wear it/eat it again if my life depends on it

--dang... that would be wonderful. Maybe you could patent your idea? However... with my luck, when it got in my house when I "suggested" it should run the garbage collector, it would just ignore me and keep getting OutOfMemoryExceptions.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: garbage collection