Garbage collector Question
Hi,
http://www.javacaps.com/scjp_gar.html#questions
At the above given URL I saw a question which is,
2. Which statements about garbage collection are true?
[a] The garbage collector runs in low memory situations
You can run the garbage collector when ever you want.
[c] When it runs it releases the memory allocated by an object.
[d] Garbage collector immediately runs when you set the references to null.
The answer they have given are "a" and "c". But I dont think "a" is correct always. Is garbage collector running only when the system memory goes low or is it guaranteed that GC will run when system memory runs low. I have always thought that since GC is a low priority thread , it will run as and when other high priority threads are not busy and when there are resources to claim. Please let me know u'r comments on this.
Thanks in advance...
regards Stallon
[965 byte] By [
stallon] at [2007-9-26 21:23:24]

"a" is correct too. Carbage collection is very JVM spesific operation. You can not force it to run, but you can suggest it to run. "a" is correct, because it often runs when more memory is needed, but it's not still guaranteed to run.
See what Madame Zhang has to say about Garbage collection.
http://www.webappcabaret.com/javachina/faq/06.htm#gar_col
-Raine-
> "a" is correct too. Carbage collection is very JVM
> spesific operation. You can not force it to run, but
> you can suggest it to run. "a" is correct,
> because it often runs when more memory is needed, but
> it's not still guaranteed to run.
I was under the impression that a JVM had to run the GC before it threw an OutOfMemoryError.
That is, it is impossible to get an OutOfMemoryError by starving the GC thread.
I tried to look that from JVM specification, but I didn't found. Can anyone confirm is that a "must" for JVM to run gc before OutOfMemoryError?-Raine-
> I tried to look that from JVM specification, but I
> didn't found. Can anyone confirm is that a "must" for
> JVM to run gc before OutOfMemoryError?
>
> -Raine-
This may be a claim I am unable to back up. In preliminary searching, this link seems to imply that GC will be run before throwing the exception
http://www.onjava.com/pub/a/onjava/2001/08/22/optimization.html
Encountering an OutOfMemoryError means that the garbage collector has already tried its best to free memory by reclaiming space from any objects that are no longer strongly referenced.
Smug in the hopes of finding what I was looking for in the next few hits, I ran across
http://www.artima.com/designtechniques/cleanup2.html
It explicitly states that a JVM does not require a garbage collector. Given this, I can't see how the GC "must" run before throwing an OutOfMemoryError, since there might not even be one :-P
I would like it to be the case that most popular JVMs will exhibit this behaviour, although I wouldn't say that it's particularly necessary... if a program is relying on the GC to ensure that there's enough free memory to continue creating objects, chances are there's room for the design to be improved to ameliorate the situation.
Apologies if I misled,
-T
> I would like it to be the case that most popular JVMs
> will exhibit this behaviour
I agree that Troy, so shall we summarize that topic. Original question was
[a] The garbage collector runs in low memory situations
So we assume that in most popular cases that is true, but it might be possible that the whole garbage collector is missing from the JVM
hi everybody,
Today I heard that garbage collecting is in fact a VERY JVM specific task. It can even be implemented differently across different JVM's. GC can run under a low priority thread and do its thing not stopping the JVM, but there are also cases where the JVM is stopped in order to GC. The GC normally will kick in under a certain treshhold, in such a manner, no real saw-tooth pattern will emerge. I found the following definition of the GC that all JVM must support (or should anyway).
The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); onjects are never explicitely deallocated. The java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implemetor's system requirements.
Which means that all JVM has to have is a GC, nothing more nothing less...I don't know what this means for the question, but personally I agree that "a" probably is not a correct answer to the question.
regards and have a nice weekend,
Jeroen.
jewes at 2007-7-3 21:08:42 >

Hi again!
I see that, if we deside that "a" is not correct, then we say that
The garbage collector does not run in low memory situations
I think this is not true either.
If the original question would have been
The garbage collector runs always in low memory situations , then question would have been incorrect, but that was not the question. I'm still in that opinion that gc often runs in situations, when there is a lack of memory in the heap, but gc is not forced to do so.
How do you see these speculations?
-Raine-
Hi,
Object temp;
for(int x=0;x<Integer.MAX_VALUE;x++)
temp=new MyClass();
So from fiotan said, the above code snippet will never throw OutOfMemoryError since JVM makes its every effort to collect objects before throwing the error and its obvious that, when the Error occurs there are (n-1) un-referenced objects in memory.
><based on contents in URL http://www.onjava.com/pub/a/onjava/2001/08/22/optimization.html >
Also the option (a) says that "GC runs in low memory situations". But I think its not dependent on Memory levels. I though that GC might be running when the low priority thread (GC) gets its schedule slot, and not when memory gets low. If GC is running when memory gets low, then why am I getting an OutOfMemoryError on executing the above code snippet.
regards Stallon
> Hi again!
>
> I see that, if we deside that "a" is not correct, then
> we say that
> The garbage collector does not run in low memory
> situations
> I think this is not true either.
>
> If the original question would have been
> The garbage collector runs always in low
> memory situations , then question would have been
> incorrect, but that was not the question. I'm still in
> that opinion that gc often runs in situations, when
> there is a lack of memory in the heap, but gc is not
> forced to do so.
>
> How do you see these speculations?
>
> -Raine-
>
>
Is the statement,
a)Garbage Collector does run in low memory situations.
TRUE?
> a)Garbage Collector does run in low memory> situations.> > TRUE?In my opinion yes!Btw I am a SCJP, but that does not actually prove anything.-Raine-
Hi again!
I posted this question to the jchq.net and got some replies. I pute those here. It seems that this question stays open and we can't find any solid answer to that.
-Raine-
Here are replies from www.jchq.net
By William Brogden
I think it is reasonable to say that the JVM will run GC if it can't provide memory for a new object. JVMs optimized for various purposes might have more advanced strategies for running GC but even the most basic would have to provide for running under these conditions.
By Jim Yingst
Anyway, the statement is too vague to interpret exactly. Does it mean it always runs in low-memory situations? That it can run then? Or that it only runs then? And what exactly is a low-memory situation? Does it apply only to "just before an OutOfMemoryError", or also to other low-but-not-empty situations? Who knows? It's not worth worrying about the details here for such a vague question. I'd agree with Bill about what the question probably means - that will have to be good enough, I think.
Hi Raine,
Thankyou very much for u replies. I was just trying to get a clear picture about the Garbage Collection strategy. If I admit that the GC runs in low memory situations, then how come I get an OutOfMemoryError while there are may objects to be Garbage collected. I am referring to the small code snippet which I posted last time.(Please see my yesterday's post).
I admit that this is very much a vague question; but the fact is that I got this from a person responsible for training java in our company. I was pretty confused regarding the same.
regards Stallon
I looked your code snippet Jacob and I have to say that I did not succeed to get OutOfMemoryError with it. Well now I can't say why you get the error. Maybe it's the JVM you are using. I have Windows 2000 and jdk 1.3 what do you use?-Raine-
Hi,
Actually it was not the loop that was causing the problem; it was a huge byte array inside that class and on encountering it I got an OutOfMemoryError. Sorry to trouble with the code snippet. When I view the GC in verbose mode (using java -verbose:gc className) using JDK1.4, I could see the objects being garbage collected after the creation of some 1000 stray objects. So it might be happening when JVM gets short of memory.
Anyway thankyou very much for u'r posts..
regards Stallon