float_dublin
float_dublin

Reputation: 511

How to microbenchmark java garbage collector?

I need to test different approaches of storing my domain objects and indexes from gc (garbage collection) point of view.

(related to my question Will GC be much slower if i have mulitple references for each of millions of my business objects?)

Does this make any sense?

long start = System.currentTimeMillis();
System.gc();
long time = start - System.currentTimeMillis();

How to do it properly?

Upvotes: 1

Views: 199

Answers (3)

Boris the Spider
Boris the Spider

Reputation: 61148

Nope, it makes absolutely no sense; see my answer here and this question.

As GC is non-deterministic you can only really measure averages over a period of time.

VisualVM has various tools for monitoring GC activity. The new Mission Control has even more powerful tools in the flight recorder suite of runtime analysis tools.

Note that the flight recorder requires the -XX:+UnlockCommercialFeatures flag which has a licensing condition attached to it:

COMMERCIAL FEATURES You may not use the Commercial Features for running Programs, Java applets or applications in your internal business operations or for any commercial or production purpose, or for any purpose other than as set forth in Sections B, C, D and E of these Supplemental Terms. If You want to use the Commercial Features for any purpose other than as permitted in this Agreement, You must obtain a separate license from Oracle.

Essentially you cannot use commercial features in production. More information here.

This will allow you to measure such things and number of collections run, CPU usage of the GC. Number of objects created, number of object references, number cleared.

But the issue still stands that the running of the GC is non-deterministic.

You need to create real world test suite that runs for a period of time. You need to collect metrics from your code, such are response times, and metrics of the monitoring side. A few hours is good. A day is better.

Once you have this you can be fairly sure which GC is best for your application.

Upvotes: 4

yshavit
yshavit

Reputation: 43391

Firstly, when you benchmark you should use System.nanoTime() rather than System.currentTimeMillis.

But I wouldn't time System.gc(), because that method comes with absolutely no guarantees. It's just a suggestion to the system.

JVMs come with non-standard options to show GC activity. For instance, HotSpot (the standard JVM Oracle puts out) has options -XX:+PrintGCDetails and -XX:+PrintGCTimeStamps. I would use those if you want to get a handle on GC activity, rather than rolling your own version.

Upvotes: 1

Jens Schauder
Jens Schauder

Reputation: 81882

I'd expect that you get rather useless results, because you probably will trigger full gc run all the time, but on a real system the gc will use different kind of runs based on a non trivial logic.

So I would use the logging of the gc to gather the numbers.

Upvotes: 0

Related Questions