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.
5 Answers
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.

- 37,128
- 15
- 99
- 111

- 13,836
- 21
- 78
- 112
-
1you may want to look at MaxPermSize as well. – urmalp Sep 30 '09 at 00:36
-
128he is asking about JVM memory. What you have said is the heap size. They both are different – vsingh Jan 31 '13 at 19:06
-
11To 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) – murungu Mar 03 '14 at 16:08
-
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. – Raghuram Vadapalli Jun 16 '16 at 18:34
-
7uhu. so I set `-Xmx524M` and the process takes up 1.2 GB of RAM. (?) – phil294 Mar 19 '17 at 17:40
-
22This is not the correct answer, -Xms and -Xmx options only regulate the jvm heap size, not the total memory allocation. – Peter De Winter Mar 21 '18 at 09:30
-
1I think this used to control total memory, JVM8 now uses a free size "PermGen" for classes and code. – ChRoNoN Feb 26 '19 at 15:31
-
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. – Will Apr 01 '19 at 08:19
-
1This can be useful in some cases, but in my case it did not solve my memory consumption issue. – LeYAUable May 29 '19 at 09:06
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.

- 136,852
- 53
- 295
- 323
-
1That is not true, according to this thread, there are multiple ways you can leak outside of the heap http://stackoverflow.com/questions/1475290/java-memory-mystery-do-i-have-a-leak – erotsppa Sep 29 '09 at 18:31
-
You are correct, there are lots of ways to have memory issues not relating to the stack. However, they are not very common. – jjnguy Sep 29 '09 at 18:37
-
11
-
Pretty sure you can control it via `-XX:MaxDirectMemorySize`. Not that I've profiled heavily to make sure but still ;) – alexandergunnarson Feb 02 '17 at 16:37
-
2@alexandergunnarson The `MaxDirectMemorySize` only affects NIO buffers. All kinds of other native memory are used by the JVM. – Christopher Schultz Apr 23 '18 at 13:11
-
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. – alexandergunnarson Apr 23 '18 at 22:14
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

- 6,365
- 3
- 53
- 57
-
9Is `ulimit` a Linux command? I did a quick Google search and didn't see any relationship between `ulimit` and the JVM. Y – Sam May 26 '15 at 04:09
-
Yes it is linux command . http://tldp.org/LDP/solrhe/Securing-Optimizing-Linux-RH-Edition-v1.3/x4733.html – vsingh May 15 '17 at 02:41
-
2
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.

- 121
- 2
The NativeHeap can be increasded by -XX:MaxDirectMemorySize=256M (default is 128)
I've never used it. Maybe you'll find it useful.

- 1,933
- 3
- 33
- 45
-
1I doubt that op wanted this: native memory is used when you make a call to C/C++ code from java. – om-nom-nom May 20 '13 at 08:59
-
12native memory is also used when making nio calls if you allocate the buffers with direct memory. (... and classloaders, and thread information....) – stu Mar 07 '14 at 15:07