• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any way to force garbage collection in java? If I am not wrong, System.gc() is a hint to the garbage colector to run but it cannot be assured. Is there a more reliable way of doing the same.

I see that app servers like weblogic has an option called "force Garbage collection" and they seem to reduce memory usage at a click of a button. Does anybody have an idea how they might be doing this.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by pra kamat:
Does anybody have an idea how they might be doing this.



By calling System.gc(). Most of the time, System.gc() will indeed return some memory -- often quite a lot. It will never grind away until every last byte is reclaimed. And in theory, it can simply do nothing. But the reality of it is that calling System.gc() frees up a bunch of memory.

Is this actually useful in some way? No, not really, most of the time; just gives you the warm fuzzies, and as you say, looks nice on a graph. As soon as the heap filled up, the GC would run automatically, anyway.
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing I've wondered about by looking at this example is why the finalize method is never called if you take out the call to the garbage collector.
I would have assumed at the end of execution of all programs there should be a memory clean up. By the looks of this, the JVM allocates this job to another process. Is this correct?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JVM of course cleans up after itself when it terminates but this may well happen only AFTER all means it has to communicate with the outside world (such as logfiles, terminal, etc.) have been made unavailable.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that System.gc() usually finalizes some objects and returns memory as a kindness to Java students who want to test garbage collection.

The production use of System.gc() is not to avoid running out of memory (we are guaranteed that gc will run on its own when memory is getting short) but to get the gc processing over with for a while so some time-sensitive code can run without interruption.

The reason that students will seldom see finalization happen without calling System.gc() is that the available memory is so large that only an all-the-memory-you-can-get algorithm or a badly looping method will use enough memory to trigger gc. Production programs with large numbers of threads are another story.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going to rave about JConsole in way too many posts. It's a tool in JDK 5 that attaches a management console to a running JVM via a socket. It has a button that appears to force the JVM to GC right now. If that's so, there must be some API that they probably don't want you to use.
 
Ranch Hand
Posts: 724
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is API written in native language (C++) i guess, it is called JVMPI (Java Virtual Machine Profiling Interface) and there are functions for forcing GC.

http://java.sun.com/j2se/1.4.2/docs/guide/jvmpi/jvmpi.html#RunGC
 
pra kamat
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the inputs.

I really appreciate
 
Police line, do not cross. Well, this tiny ad can go through:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic