62

I'm working on a Java application at the moment and working to optimize its memory usage. I'm following the guidelines for proper garbage collection as far as I am aware. However, it seems that my heap seems to sit at its maximum size, even though it is not needed.

My program runs a resource intensive task once an hour, when the computer is not in use by a person. This task uses a decent chunk of memory, but then frees it all immediately after the task completes. The NetBeans profiler reveals that memory usage looks like this:

Java program memory usage

I'd really like to give all of that heap space back to the OS when not in use. There is no reason for me to hog it all while the program won't even be doing anything for at least another hour.

Is this possible? Thanks.

14
  • I think this depends on what JVM you're using. The JVM spec says nothing about this, IIRC. Feb 10, 2011 at 1:48
  • 3
    @Fakrudeen, what you said is incorrect. If it is not decommit by process, OS may reclaim the physical memory, but it needs to write the content to disk first, which degrades performance a lot. For many servers it doesn't have swap to make sure it doesn't thrashing. In this case, OS can never reclaim the memory, however long it is untouched. Your comment will mislead anyone who didn't take OS course before. It's harmful.
    – Kan Li
    Oct 1, 2013 at 4:49
  • 3
    @Fakrudeen, swapping unused memory to disk is always bad idea. It incurs IO when writing it to disk. More importantly, if JVM needs more memory again, it needs to read garbage content from disk. If JVM can shrink and avoid being swapped, then if it needs more memory, it can ask OS to allocate free pages to them, which is much cheaper than reading garbage content from disk. I feel sad that you guys feel OK about things being swapped. What if a newbie reads about this and had a life-time impression that it is not a big deal things got swapped.
    – Kan Li
    Oct 1, 2013 at 18:23
  • 3
    @Fakrudeen, look at OP's chart, JVM does GC after his resource intensive job finishes, and JVM definitely knows how much memory it is using. I never say GC is not expensive. The problem we are talking about is not whether GC is expensive or not, it is after GC, JVM knows it has a bunch of free heap, but never returns it to OS. Man, I know people is instinctively defensive when criticized. But please think about it. Also, I want newbie to know that swapping is REALLY bad thing. If some process has heavy disk IO, almost every other process will hang, e.g. you can't even SSH to the machine.
    – Kan Li
    Oct 5, 2013 at 1:19
  • 1
    Finally, note that all of this applies only to the Java's heap (that is, GC managed memory). Relying on swap for off-heap memory (for example, ByteBuffer.allocateDirect()) pose absolutely no issue form the GC's perspective, and may be perfectly acceptable (depending on the use case, obviously). This is for example what is done by all major Java-based databases, caches and search engines, though even these generally recommend that memory settings be adjusted to avoid anonymous pages from being swapped out.
    – James
    Sep 19, 2018 at 15:49

7 Answers 7

44

You could perhaps play around with -XX:MaxHeapFreeRatio - this is the maximum percentage (default 70) of the heap that is free before the GC shrinks it. Perhaps setting it a bit lower (40 or 50?) and then using System.gc() might go some lengths to get you the desired behaviour?

There's no way to force this to happen however, you can try and encourage the JVM to do so but you can't just yank memory away as and when you want to. And while the above may shrink the heap, that memory won't necessarily be handed straight back to the OS (though in recent implementations of the JVM it does.)

5
  • This is interesting, but it doesn't address the question itself which is "I'd really like to give all of that heap space back to the OS when not in use... Is this possible?"... GC directions just tell the JVM how to manage the (monotonically growing) Java heap.
    – andersoj
    Feb 10, 2011 at 2:10
  • 11
    @andersoj: This setting does give back RAM to the OS. See stopcoding.wordpress.com/2010/04/12/… for an impressive example.
    – A.H.
    Feb 21, 2012 at 13:22
  • 1
    @A.H.: Too cool, too cool. I was unaware, thank you for the pointer. (Wish I was smart enough to improve my answer above to cover this... if you post an answer here I'll upvote.)
    – andersoj
    Feb 21, 2012 at 15:24
  • 2
    @andersoj Please upvote this answer, because berry120 is the one who introduced that specific parameter.
    – A.H.
    Feb 21, 2012 at 17:05
  • 5
    Another very interesting article, showing that SerialGC and ParNewGC give back memory: stefankrause.net/wp/?p=14
    – Cobra_Fast
    Nov 20, 2013 at 15:46
20

Short version: Yes you can.

Long version:

How Java/JVM manages memory

For most applications the JVM defaults are okay. It looks like the JVM expects applications to run only a limited period of time. Therefore it does not seem to free memory on it's own.

In order to help the JVM to decide how and when to perform garbage collection, the following parameters should be supplied:

  • -Xms Specifies the minimal heap size
  • –Xmx Specifies the maximal heap size

For server applications add: -server

That's not enough for me. I want more control!

In case the above mentioned parameters are not enough, you can influence the behavior of the JVM regarding garbage collection.

First you can use System.gc() to tell the VM when you believe garbage collection would make sense. And second you can specify which of the garbage collectors the JVM should use:

Different Kinds of Garbage collectors:

  • Serial GC

    Command line parameter: -XX:+UseSerialGC

    Stops your application and performs GC.

  • Parallel GC

    Command line parameter: -XX:+UseParallelGC -XX:ParallelGCThreads=value

    Runs minor collections in parallel with your application. Reduces time needed for major collections, but uses another thread.

  • Parallel Compacting GC

    Command line parameter: -XX:+UseParallelOldGC

    Runs major collections in parallel with your application. Uses more CPU resources, reduces memory usage.

  • CMS GC

    Command line parameter: -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=value -XX:+UseCMSInitiatingOccupancyOnly

    Performs smaller collections, and more often than Serial GC, thus limiting the breaks/stops of the application.

  • G1

    Command line parameter: -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC

    Experimental (at least in Java 1.6): Tries to make sure the application is never stopped for more than 1s. #Example Memory usage of a Play Framework web application without any optimizations: Play Framework WebApp without optimizations As you can see, it uses quite a lot of heap space, and the used space is regularly freed.

In this case the optimizations with parameters only were not effective. There were some scheduled tasks which used rather a lot of memory. In this case the best performance was achieved by using the CMS GC combined with System.gc() after the memory intensive operations. As a result the memory usage of the WebApp was reduced from 1.8 GB to around 400-500 MB.

You can see here another screenshot from the VisualVM which shows how memory is freed by the JVM and actually returned to the OS:

Memory is being freed by the JVM and returned to the OS Note: I used the "Perform GC"-button of the VisualVM to perform the GC rather than System.gc() in my code, as the scheduled tasks which consume the memory are only launched at specific times and somewhat harder to capture with VisualVM.

Further Reading

5

Java 12 supports this feature using G1GC.

JEP 346: Promptly Return Unused Committed Memory from G1.

Enhance the G1 garbage collector to automatically return Java heap memory to the operating system when idle.

https://openjdk.java.net/jeps/346

Java 13 supports this feature using zgc

JEP 351: ZGC: Uncommit Unused Memory

ZGC does not currently uncommit and return memory to the operating system, even when that memory has been unused for a long time. This behavior is not optimal for all types of applications and environments, especially those where memory footprint is a concern. For example: Container environments where resources are paid by use.

  • Environments where an application might be idle for long periods of time and is sharing or competing for resources with many other applications.

  • An application might have very different heap space requirements during its execution. For example, the heap needed during start up might be greater than what is needed later during steady state execution.

http://openjdk.java.net/jeps/351

3

One possibility is to have your background java application launch an external jvm instance each hour to run your task. That way only your original jvm application is running between tasks.

0
3

If your app is quiescent during periods of inactivity, it's possible the OS will swap out those pages for you, mitigating their pressure on physical memory.

http://www.linuxvox.com/2009/10/what-is-the-linux-kernel-parameter-vm-swappiness/

3

Java best kept secret: -Xincgc It does impact performance but not always that much. Sometimes it does, depends on what you're doing. The incremental garbage collector hands memory back to the system quite well !

2
  • Thanks! I will look into that if I get a chance to go back to this project.
    – jocull
    Jul 28, 2011 at 18:49
  • 3
    Note: Using OpenJDK 8, this prints out OpenJDK 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release Oct 22, 2015 at 13:11
3

The JVM doesn't work that way. You can't give it back to the OS.

As noted by several people since this was written four years ago, you can give memory back to the OS if you give the proper GC settings to the JVM.

12
  • 2
    Getting the (Java) heap down to a lower level doesn't give the memory back to the OS. The effect is the same, though, since unused pages are swapped out and don't have a significant impact.
    – andersoj
    Feb 10, 2011 at 1:49
  • 2
    @jocull: JVM can garbage-collect and compact its heap; there'll be many unused pages. JVM can't return it to the OS as freed memory, but it will not use these pages. Running out of RAM, OS will swap these pages out, allowing active processes to use the physical RAM these pages used to occupy. This is not exactly 'freeing memory', and some disk I/O is involved, but the effect is similar: you give active processes some of the RAM that JVM occupied earlier.
    – 9000
    Feb 10, 2011 at 1:59
  • 2
    I've seem the JVM give back memory to the OS under Windows XP. So it is possible.
    – Steve Kuo
    Feb 10, 2011 at 5:04
  • 2
    -Xincgc does give memory back to the system !
    – Mike
    Jul 28, 2011 at 18:10
  • 9
    At least since Java 1.5 memory CAN be returned to the OS. See stopcoding.wordpress.com/2010/04/12/… for an impressive example.
    – A.H.
    Feb 21, 2012 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.