• Post Reply Bookmark Topic Watch Topic
  • New Topic

when to use the Runtime.gc() method?  RSS feed

 
krillian konstatos
Ranch Hand
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!

i have a small question about the Runtime class in the java.lang package. I wonder what
exactly the method gc() does in the above mentioned class and when it is recommended
to use it? i cant find any good answers on this in the api papers. i have heard that you
should use it to free up memory space in the jvm or have i missed something out? any way since
i've heard about this i use this method in my closing operations before calling System.exit.
is there any good articles out there covering the Runtime class and its methods ?

krillian.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the most part you should never need to call Runtime.gc() since the garbage collector runs in the background and will always guarantee to have run before the JVM runs out of heap space. There are times when it does seem to make a difference; for example - just before you iconize a large Swing program it can stop the de-iconization taking a very very long time; just before trying to delete a file you have recently just closed can stop the delete failing. There are a couple of other scenarios but I can't remember them at the moment.
 
krillian konstatos
Ranch Hand
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!

thank you so much for the reply! now i have a much clearer picture of what the gc() mehod does and how it works. i can see now that it should be used in memory operations such beofre or after deleting files or iconizations a specially if you are having large gui's. i hope this was correctly understood by me.

regards,
krillian.
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't read more into my post than I actually said. It is very very rare that you need to use Runtime.gc() and there are no hard and fast rules. There are just a very few heuristic scenarios when using the Sun/Oracle JRE it seems to make a difference. I don't know about JRE from other manufacture's (e.g. IBM).
 
krillian konstatos
Ranch Hand
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am not that very good too communicate through english writing and i apologize for any error in my reply. i see more clearly upon the uses of the gc method now. i am a very early post beginner on java if i dare to call myself that and my knowledge about the jvm are very limited in this matter. but after this reply i aleady know more so thank you very much for it.

krillian.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From what i've read, it's almost NEVER the right thing to do to call it yourself. If it needs to run, the JVM will run it. If it doesn't need to be run, then why waste time doing it?

I have also read that many JVMs will ignore it anyway - they take it "under advisement", but are free to not run it. The algorithms for when it needs to run are rather sophisticated, and probably do a better job of knowing when it needs to run than any single developer.
 
krillian konstatos
Ranch Hand
Posts: 49
Java Netbeans IDE PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have wondered about this method for a long time and what it really does but things are much clearer now. i just have to read more about the jvm and how it works in the basics. does anyone know of some good books about memory handling in java or links to articles about theese issues?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, you should never call System.gc(); unless you have a very specific reason to do it, for example because you've discovered that it helps to solve performance or memory problems.

Never add this to your program because you have a vague feeling that it helps, without really knowing. It is not so that it cannot do harm; calling System.gc(); at the wrong moment can hurt the performance of your application.

Here are some documents (note, this is really advanced stuff!):

Java SE 6 Performance White Paper
Java SE HotSpot at a Glance
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!