views:

475

answers:

5

Does adding -Xmx argument when running a Java program cause the garbage collector to act differently or occur less often?

+4  A: 

The general answer is that garbage will tend to be collected less often but the GC pauses will tend to be longer.

This is assuming you already have more memory available to the VM than your working set size; otherwise you might be spending a lot of time in garbage collection.

The GC characteristics vary greatly depending on which collector you are using and which version of Java, and whether you specified a higher -Xms in addition to the higher -Xmx. Older versions of Java (before 5) did not resize the young generation space after the VM had initialized. So even if you specified a really large -Xmx value, the size of the young generation would still be really small so you would see frequent young generation collections.

When a young generation collection occurs, there will be some "young" objects that will be promoted to the tenured space anyway, even though they are short-lived objects. This means that the tenured generation will slowly fill up with dead young objects, requiring periodic full GCs. The number of such objects that end up in the tenured generation is proportional to the number of young GCs (assuming constant activity in your program). So the advantage of a larger young generation is that there will be fewer young generation GCs, and therefore fewer objects from the young collection will be promoted to the tenured generation.

There are a number of tuning parameters besides -Xmx (google for java gc), and you will have to experiment with them since the optimal settings will vary for each application.

sk
+8  A: 
erickson
+2  A: 

Yes, but it depends on which garbage collector you're using. The GC tuning page is a good resource for this sort of question.

Michael Myers
It would be better if they closed their <h1> tag at the top, though.
Michael Myers
+1  A: 

It does, because when you have more memory then when it comes to collect and compact it has more to scan. This means that when a (full) GC does kick in that means that it will have to scan more memory, and potentially compact more memory too. Perhaps with more context we can help further. There are obviously three different sections that get scanned, so it doesn't necessarily correlate directly in a 1-1 sense.

Egwor
+1  A: 

Only indirectly, and after running for a while. -Xmx sets the MAXIMUM size the heap is allowed to grow to, and doesn't affect the initial size. So it won't affect gc time or frequency early in your program's run. If the heap is still (too close to) full after a gc, the heapsize will be increased, and this is what -Xmx will affect. A higher setting for -Xmx will allow the heap to (eventually) grow larger and thus trigger collections less often (and those collections will take longer, as others have described).

If, however, your application doesn't use too much memory (so the heap is mostly empty after a gc), the heap will never grow, and the -Mx setting will be irrelevant.

You can use -Xms to set the initial GC heap size. This will have an immediate effect on garbage collections.

Chris Dodd