views:

352

answers:

4

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.

+4  A: 

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.

jjnguy
That 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
You are correct, there are lots of ways to have memory issues not relating to the stack. However, they are not very common.
jjnguy
Pretty sure you can't control non-heap memory size, can you?
matt b
+4  A: 

use the arguments -Xms -Xmx M, G after the numbers for indicating Megs and Gigs of bytes respectively. -Xms indicates the minimum and -Xmx the maximum

Ram
you may want to look at MaxPermSize as well.
prateek urmaliya
+1  A: 

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.

SmoothieMonster
A: 

I've come across this statement reading the following article.

http://java.dzone.com/articles/java-performance-tuning?page=0,1

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

I've never used it. Maybe you'll find is useful. The spelling error is not mine.

AfisBoy