This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Core Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Core Java" Watch "Core Java" New topic
Author

Core Java

ramesh vadugu
Greenhorn

Joined: Apr 21, 2007
Posts: 6
In java explitly how we call a gc() on which,like System.gc()or

runtime.gc() ?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You can't explicity call the garbage collector from Java code. The method System.gc() only suggests to the JVM it should run the garbage collector. It may not actually run.

A more important question to ask is not how you do this, but why you need to write code to attempt to manipulate how the JVM behaves after Java has been so carefully designed to avoid you having to do this sort of thing.
[ April 24, 2007: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
James A Anderson
Greenhorn

Joined: Mar 02, 2007
Posts: 12
Originally posted by Paul Sturrock:
You can't explicity call the garbage collector from Java code. The method System.gc() only suggests to the JVM it should run the garbage collector. It may not actually run.

A more important question to ask is not how you do this, but why you need to write code to attempt to manipulate how the JVM behaves after Java has been so carefully designed to avoid you having to do this sort of thing.

[ April 24, 2007: Message edited by: Paul Sturrock ]


I would think a time you would want to explicitly invoke garbage collection would be when you know you are finished using large resource/resources and want garbage collection to occur as soon as possible to free them (the resources).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

but what does that gain you? if the JVM doesn't need the resources, then forcing a GC just wastes time. if the JVM does need the resources, it's going to do a GC anyway.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Fred Rosenberger:
but what does that gain you? if the JVM doesn't need the resources, then forcing a GC just wastes time. if the JVM does need the resources, it's going to do a GC anyway.


If your program is in an "idle" state and you have freed up a large amount of objects, then it may be of benefit to suggest that the GC runs to dispose of the freed objects, so that when the next "busy" period arrives you do not have to wait around for a time-consuming GC to take place. In that way you could possibly get a more responsive system.


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

Joined: Apr 14, 2004
Posts: 10336

You may be right. I am always aware that I am unlikely to be able to successfully second guess what is best for the JVM to do at any current point in time. In the example you give my suspicions would be that the garbage collector may well run anyway during this idle state. However, since this is the beginner's forum I'd suggest the best strategy to follow is not to try to interact with the GC directly (not that you can) and if you are trying to stop and ask yourself "am I complicating this code with an unnecessary early optimization"?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Core Java