How to set the maximum memory usage for JVM?

JavaMemoryMemory ManagementMemory LeaksProfiling

Java Problem Overview


I want to limit the maximum memory used by the JVM. Note, this is not just the heap, I want to limit the total memory used by this process.

Java Solutions


Solution 1 - Java

use the arguments -Xms<memory> -Xmx<memory>. Use M or G after the numbers for indicating Megs and Gigs of bytes respectively. -Xms indicates the minimum and -Xmx the maximum.

Solution 2 - Java

You shouldn't have to worry about the stack leaking memory (it is highly uncommon). The only time you can have the stack get out of control is with infinite (or really deep) recursion.

This is just the heap. Sorry, didn't read your question fully at first.

You need to run the JVM with the following command line argument.

-Xmx<ammount of memory>

Example:

-Xmx1024m

That will allow a max of 1GB of memory for the JVM.

Solution 3 - Java

If you want to limit memory for jvm (not the heap size ) ulimit -v

To get an idea of the difference between jvm and heap memory , take a look at this excellent article http://blogs.vmware.com/apps/2011/06/taking-a-closer-look-at-sizing-the-java-process.html

Solution 4 - Java

The answer above is kind of correct, you can't gracefully control how much native memory a java process allocates. It depends on what your application is doing.

That said, depending on platform, you may be able to do use some mechanism, ulimit for example, to limit the size of a java or any other process.

Just don't expect it to fail gracefully if it hits that limit. Native memory allocation failures are much harder to handle than allocation failures on the java heap. There's a fairly good chance the application will crash but depending on how critical it is to the system to keep the process size down that might still suit you.

Solution 5 - Java

> The NativeHeap can be increasded by -XX:MaxDirectMemorySize=256M > (default is 128)

I've never used it. Maybe you'll find it useful.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionerotsppaView Question on Stackoverflow
Solution 1 - JavaPrabhu RView Answer on Stackoverflow
Solution 2 - JavajjnguyView Answer on Stackoverflow
Solution 3 - JavavsinghView Answer on Stackoverflow
Solution 4 - JavaSmoothieMonsterView Answer on Stackoverflow
Solution 5 - JavaAfisBoyView Answer on Stackoverflow