File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes how System.gc()'s work depends on the machine? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how System.gc() Watch "how System.gc() New topic
Author

how System.gc()'s work depends on the machine?

Helen Tal
Greenhorn

Joined: Jun 15, 2002
Posts: 15
i am trying to run some code sample from my Java book, and the output is not the same as in the book: the programm ends before the gc() does it's job. i have tried both jdk1.2.2 and jdk1.3.1_01, but there is no difference (actually, there is: on jdk1.3.1_01 the programm ended even more quickly). then i've did it on another computer, and it worked just as in the book example. why?
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Are you working on same OS on both computer, with same configuration?
Normally, GC won't start collecting object until your memory begins to be full. One thing to know,
there is an initial heap size and a maximum heap size. I'm not sure how the initial and the maximum heap size is calculated, but you can modify it this way:
(Originally posted by Jessica Sant)
Heap can be defined with the following params: -Xms<size> set initial Java heap size -Xmx<size> set maximum Java heap size
You may find somme interesting linkshere
and here
So, as I understand there is a corellation between your RAM and the Heap size.
Hope it helped you
[ June 21, 2002: Message edited by: Younes Essouabni ]

Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Ivan Tamayo
Ranch Hand

Joined: Aug 13, 2001
Posts: 49
The methos gc() implies that the JVM will make a best effort (null effort) for colelcting garbage.
But you can never assume that it will recover garbaje
Helen Tal
Greenhorn

Joined: Jun 15, 2002
Posts: 15
thanks everybody,
i've just forgotten to say that my program (its not mine really,its an example from the book) calls System.gc() explicitly. and that yes, there are different OS's: Win98 and Win2000.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
SYstem.gc() will not cause garbage collection to be executed. You should consider it no more than a suggestion to the JVM that garbage collection might be a good idea right now if it isn't too inconvenient.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Helen Tal
Greenhorn

Joined: Jun 15, 2002
Posts: 15
ok, but what i don't understand is why when i run this program on the first computer the garbage collector always collects all the objects, but if i do it on the second computer, it collects only part of them.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Are the two computers running different JDK versions? There have been a number of improvements (and more rarely, new bugs) in garbage collection, coming out in each new JDK release. You may also see differences because of different OS's on the computers (which may lead to different JDK's) or because the JVM's are allocated different amounts of memory. A JVM with little heap space available will usually work harder to free memory than one with a lot of heap available. (At least, this is true if you're right at the limit - not sure otherwise.)
To see what's going on with GC I like to run with java -verbose:gc. This reports all GC activity. I find that often if I call System.gc() twice in a row, the second call manages to collect some things that weren't collected the first time. If I want to be really sure that it's collected everything it's going to, I call gc() three times in a row; the third time almost never adds anything new. (There's still no guarantee everything has been collected, but if you can call 20 more times in a row and see no change, that's a good clue.) Obviously this makes the JVM work harder, and usually it's not very useful, but it's good for studying memory usage and figuring out which objects are eligible for collection. As long as you're not too concerned about performance at the same time.


"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
OK, I reread your posts above. It seems you're comparing the same JDK across two slightly different OS's. How much RAM is available on each computer? And how much of this RAM is being used by java.exe when it runs? If one system is unable to keep the whole JVM in RAM, the JVM may work harder to collect objects, to avoid using virtual memory since it's much slower. This is just a guess, but seems plausible to me at least.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how System.gc()'s work depends on the machine?