((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
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 ]
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
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.
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.