This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
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

David Abramowicz
Ranch Hand

Joined: Dec 10, 2004
Posts: 56
Hi!

I had a big discussion today at work regarding whether it is possible to force GC in Java. Being a SCJP I obviously stated that it is impossible to FORCE the GC to do anything, but it is possible to SUGGEST or REQUEST stuff.

However, when you go to java.sun.com as one of my colleagues did and search force "force garbage collection" the FIRST hit states that
you can force object finalization and garbage collection using the appropriate method
. See http://java.sun.com/docs/books/tutorial/essential/system/garbage.html

Now, I obviously wanted to counter that by finding another authoritative source that stated what us SCJPs know, that GC can't be forced, but I COULDN'T! I only found various exam-cramming sites that stated that this...

My question: Does ANYONE have a link from an authoritative source, preferably Sun, that states that GC can't be forced???

Thank you in advance /Dave
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
All a developer can do is to call System.gc() or Runtime.gc().

Here's what the Java 1.5 API says:
Runs the garbage collector. Calling this 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 virtual machine has made its best effort to recycle all discarded objects.

The name gc stands for "garbage collector". The virtual machine performs this recycling process automatically as needed, in a separate thread, even if the gc method is not invoked explicitly.


Notice the usage of the terms "suggests" and "best effort". To me that implies that on returning from this method garbage collection may have taken place but it it not certain (but there is a probability greater than zero that it has taken place).

In practice I have found that a couple of calls to System.gc() usually cause a gc to take place. Although I can recall sometimes when it didn't.
[ January 13, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Jo Peter
Greenhorn

Joined: Jan 13, 2005
Posts: 1
However, when you go to java.sun.com as one of my colleagues did and search force "force garbage collection" the FIRST hit states that . See http://java.sun.com/docs/books/tutorial/essential/system/garbage.html


The mentioned Sun Tutorials says:
You can force object finalization to occur by calling System's run Finalization method System.runFinalization()
. However, the API emphasises that it is only a suggestion for the VM.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
There are more errors in the tutorial.
The API docs are leading (or when in doubt the JLS which is authoritive).


42
David Abramowicz
Ranch Hand

Joined: Dec 10, 2004
Posts: 56
Thanks for the response guys.

I must admit though that despite the terms "suggests" and "best effort" I don't think that it is very clear from the API description of System.gc() can't be used to force garbage collection... :/

Every single exam cramming material for SCJP states that gc can't be forced, and are we basing that on "suggests" and "best effort" from the API documentation??? .... Hmm. I really would like to see a statement from Sun on black and white that GC can't be forced.

Oh well, one day when Java goes open-source I guess we can have an affirmative answer.

/Dave
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Don't forget that GC is implementation dependant, so one JVM may respond to System.gc() but another may not. Anyone familiar with the JVM specs?

Arnold, Gosling, Holmes say in their "The Java Programming Language, 3rd Ed":

The garbage collector may not be able to free any additional memory when Runtime.gc() is invoked. There may be no garbage to collect, and NOT ALL GARBAGE COLLECTORS CAN FIND COLLECTABLE OBJECTS ON DEMAND
(my CAPITALISATION)
[ January 13, 2005: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
In fact GC is not in the JVM spec:

See here.

"For example, the memory layout of run-time data areas, the garbage-collection algorithm used, and any internal optimization of the Java virtual machine instructions (for example, translating them into machine code) are left to the discretion of the implementor."
[ January 13, 2005: Message edited by: Barry Gaunt ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by David Abramowicz:


I must admit though that despite the terms "suggests" and "best effort" I don't think that it is very clear from the API description of System.gc() can't be used to force garbage collection... :/

Every single exam cramming material for SCJP states that gc can't be forced, and are we basing that on "suggests" and "best effort" from the API documentation??? .... Hmm. I really would like to see a statement from Sun on black and white that GC can't be forced.



It is in fact extremely clear that there's no guarantee that System.gc() will result in an garbage collection being performed (or any garbage being collected if it is performed).


Oh well, one day when Java goes open-source I guess we can have an affirmative answer.

/Dave


You can download the JVM source code now if you want to and check it all yourself.
But as Barry pointed out the implementation is JVM specific and a JVM isn't even guaranteed to do anything at all. After all, an algorithm that does nothing is an algorithm albeit a rather poor one
 
GeeCON Prague 2014
 
subject: Garbage Collection