Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict the memory usage of Java application packed in a jar (-Xmx doesn't seem to work) on Windows

I'd want to restrict the maximum heap size for a Java application but it doesn't seem to work. I'm running the application from a jar package through bat in Windows. Bat contents:

@Echo off
set CLASSPATH=.
java -Xmx32m -classpath %CLASSPATH% -jar MyApplication.jar

This should restrict the maximum heap size of 32 megabytes. However, when I do things consuming memory with the application, the Windows Task Manager shows that the memory consumption goes at least to about 70 megabytes... I even tried with -Xmx2m but that didn't make any difference.

So, I'm not totally sure what's the problem. There is of course stack etc contained in the memory usage but the memory used by the program should be mostly heap space...

Java version seems to be 1.6.0_14.

For those interested what I'm after, I'm trying to see how my application would behave with certain functions when it runs out of heap space.

EDIT: Hmm.. I was surprised that the heap usage was limited to the 32M actually when monitoring with JConsole. Gotta get more memory used...

Thanks for ideas, Touko

like image 581
Touko Avatar asked Sep 18 '09 11:09

Touko


3 Answers

I don't think TaskManager shows Heap Memory specifically. It includes all memory the program is using at that moment.

For analyzing heap memory, you could use a profiler (like Yourkit) and see whether the heap limit -Xmx is obeyed or not. This will also help you analyze the parts of program you are interested in.

like image 66
vpram86 Avatar answered Oct 06 '22 01:10

vpram86


Ummm...it doesn't exactly work that way.

While there is a relationship between -Xmx32m and what you see in the task manager, it is not one-to-one. Understand that the Task Manager is not reporting your heap size, but all memory consumed by the JVM, of which your code is only a subset.

like image 38
Stu Thompson Avatar answered Oct 06 '22 01:10

Stu Thompson


You are seeing the sum of heap size & permanent generation. You can adjust perm size with the -XX:MaxPermSize command-line argument. For example, this will set it to 128m.

java -Xmx32m -XX:MaxPermSize=128m ...
like image 33
Billy Bob Bain Avatar answered Oct 05 '22 23:10

Billy Bob Bain