Re: Memory utilization

Endre Stølsvik <[email protected]> Thu, 31 Jan 2008 03:15:33 +0100
Newsgroups gmane.comp.windows.devel.java.advanced
Message-ID <[email protected]>
Randall R Schulz wrote:
> On Wednesday 30 January 2008 17:06, Endre Stølsvik wrote:
>> Avinash Lakshman wrote:
>>> ...
>> Try with a couple of System.gc()'s  before checking memory from
>> within the JVM, and then going into some sleep and checking the
>> memory as observed from the OS (do e.g. 10 GCs in a row - more than
>> one actually makes a difference sometimes - might be finalization and
>> stuff?).
>
> Invoking System.gc() only enqueues a request to perform garbage
> collection activities. GC activity has its own thread, so something
> must ensue to allow the memory management thread to run before any
> actual consequences of the call to become manifest.

The gc happens pretty fast.  System.gc() is always (on sun, at least) a
full GC - but you can configure both if it should be minor, and if it
should be ignored all together. Often, if there is much to reclaim,
you'll notice that the entire jvm locks for a small while
("stop-the-world" pause) right on the System.gc(), kinda indicating that
things are happening.

> I doubt calling it
> multiply is of any consequence.

Tried it, have you? Check it out. As I mentioned, this "stepping" of the
reclaim might happen more if much of your garbage have implemented
finalize(), or you use References and ReferenceQueues.

It certainly seems logical, and that's also what I get.

Enable the gc-logging.

> Perhaps java.lang.Thread.yield() would
> achieve this, but I'm not an expert on thread programming, so I don't
> really know if this is true or not.

On my swing client, I have this little button that does GC 10 times with
a couple of hundred ms in between, logging the Runtime.*Memory values
(and also having gc-logging on). It is interesting. I get to see if I
handle all my resources well. Also, I have this cool "reboot"
functionality - firing up a completely new instance, ditching the old,
"in-VM". If after the 20'th successful reboot, the memory after such a
super-GC is still the same, I assume that I have my resources pretty
much under control.

Regarding Thread.yield(), I've found that it doesn't work at all, while
Thread.sleep(x) works perfectly (where x is some tens of milliseconds,
probably would work with 1). This is however some versions ago, on
Linux, so things might have changed, and YMMV.

Endre.

===================================
This list is hosted by DevelopMentor®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com