Tutorial :Does setting the max memory of a Java program affect the GC?


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


Yes, most Java garbage collectors only run as needed, as opposed to running on a schedule whether needed or not. So, in general, more memory causes less frequent, longer garbage collections.

The generational garbage collectors have settings to control the allocation of memory between the young and tenured generations. By setting these to use a smaller proportion of total memory for the young generation, the absolute size of the young generation can be kept the same as the total heap size is increased. Then, you won't see much of a change in garbage collection frequency or durationâ€"but you will have more room for long-lived objects for applications like caching.


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.


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


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.


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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »