How to set the maximum memory usage for JVM?
Asked Answered
T

5

172

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.

Totem answered 29/9, 2009 at 17:24 Comment(0)
C
111

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.

Creamcolored answered 29/9, 2009 at 17:32 Comment(9)
you may want to look at MaxPermSize as well.Chronon
he is asking about JVM memory. What you have said is the heap size. They both are differentRosy
To re-iterate what the other comments mention, Xms and Xmx only configure the heap. Although configuring these variables has an indirect effect on non-heap space, the person asking the question is trying to establish if there is a way to configure total memory usage (heap+non-heap)Niacin
Is it possible to do this without using the command line? Actually, I am using a python library which uses a jar file. It terminates due to insufficient memory during runtime. It will be helpful if there is a way to set this size before I execute my script.Educe
uhu. so I set -Xmx524M and the process takes up 1.2 GB of RAM. (?)Leucopoiesis
This is not the correct answer, -Xms and -Xmx options only regulate the jvm heap size, not the total memory allocation.Recidivate
I think this used to control total memory, JVM8 now uses a free size "PermGen" for classes and code.Brazil
This is not the correct answer. As mentioned above, it restricts the heap size but there is not much effect on over all JVM memory.Menhir
This can be useful in some cases, but in my case it did not solve my memory consumption issue.Sugihara
T
46

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.

Teressaterete answered 29/9, 2009 at 17:26 Comment(6)
That is not true, according to this thread, there are multiple ways you can leak outside of the heap #1475790Totem
You are correct, there are lots of ways to have memory issues not relating to the stack. However, they are not very common.Teressaterete
Pretty sure you can't control non-heap memory size, can you?Midnight
Pretty sure you can control it via -XX:MaxDirectMemorySize. Not that I've profiled heavily to make sure but still ;)Scythe
@Scythe The MaxDirectMemorySize only affects NIO buffers. All kinds of other native memory are used by the JVM.Sir
Good point @ChristopherSchultz. Coming back to this a year later, I realize this is the case. It's unfortunate but unavoidable. Luckily many Java libs (like Netty) use NIO buffers only for off-heap memory (and are thus configurable in that way) rather than e.g. using JNI.Scythe
R
21

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

Rosy answered 31/1, 2013 at 21:28 Comment(3)
Is ulimit a Linux command? I did a quick Google search and didn't see any relationship between ulimit and the JVM. YInspirit
Yes it is linux command . tldp.org/LDP/solrhe/Securing-Optimizing-Linux-RH-Edition-v1.3/…Rosy
The JVM doesn't handle hitting the limit set this way wellNurserymaid
C
12

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.

Counter answered 29/9, 2009 at 19:47 Comment(0)
K
0

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

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

Knocker answered 29/9, 2009 at 23:54 Comment(2)
I doubt that op wanted this: native memory is used when you make a call to C/C++ code from java.Grandiloquent
native memory is also used when making nio calls if you allocate the buffers with direct memory. (... and classloaders, and thread information....)Shantay

© 2022 - 2024 — McMap. All rights reserved.