gc before save on exit

Questions about YourKit Java Profiler
Post Reply
spdenne
Posts: 3
Joined: Wed Aug 31, 2005 8:49 pm

gc before save on exit

Post by spdenne »

I've had some variability in the size of memory allocation snapshots saved on exit of one application I've written.

The snapshot sizes varied between 1.9MB and 220MB :shock: (no typo - the largest snapshots are one hundred times the size of the smallest snapshots).

What was confusing was that the algorithm was virtually unchanged, and the snapshots appeared near enough to equivalent when the snapshots were opened and examined.

I played around with a whole lot of options, and finally decided that the snapshot size was related to the amount of garbage collection performed.

My application is a short-running, memory hungry application. When it has finished its complex task, it exits. This all happens in 4 seconds if given a NewSize approaching 200MB (garbage collection never occurs) and about 12 seconds when run with the default mem settings (with a large number of garbage collections).

When the application is exiting, just about everything is no longer accessible, but for speed reasons there is no point garbage collecting immediately prior to exiting.

However when opening a snapshot, YJP treats inaccessible objects as though they were collected. I think that this explains why I was seeing equivalent result in YJP even though the snapshot size varied so much.

I altered my application to System.gc() as the very last thing it does, and now snapshots are 1.9MB every time.

Could YJP please have an additional option to force a gc prior to saving a snapshot on exit of the application?
Anton Katilin
Posts: 6172
Joined: Wed Aug 11, 2004 8:37 am

Post by Anton Katilin »

Hi,

Explicit gc's are not allowed in the JVM state when the on-exit snapshots are captured.

I can suggest the following workaround.
You can use in your application the following shutdown hook that explicitly calls gc:

Runtime.getRuntime().addShutdownHook(
new Thread(){
public void run() {
System.gc();
System.gc();
System.gc();
}
}
);

It should trigger before the on exit snapshot capturing.
Post Reply