Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java is Allocating Extra 2gb of Memory

I got a new VPS to run some java programs that me and some buddies have made. I start the process with a line like this:

java -Xmx512M -jar program.jar

On our old VPS, you could use the 'top' command to see how much virtual and resident memory was being used. It would use like 600-700mb of virtual memory. Now on our new VPS, with that same command, the virtual memory seems to always be an extra ~2gb over the -Xmx value. So instead of the virtual memory being around 600-700mb, it's instead 2700-3000mb.

The old VPS is running CentOS 5.7 and the new is running CentOS 6.2. Both are running JRE 1.7u3 64bit.

Why is this and how can I fix it?

edit: top

PID   USER    PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
27645 pyro    20   0 3003m 270m  10m S  5.0  1.7   1:19.18 java -Xmx512M -jar cserver.jar

another edit: I am not questioning why virtual memory is using more memory than specified in the java command line. I am questioning why it is using so much more than it used to.

like image 230
Reed D. Avatar asked Mar 12 '12 07:03

Reed D.


People also ask

Why is Java taking up so much memory?

As you might recall, Java is a garbage-collected language. In order for the garbage collector to know which objects are eligible for collection, it needs to keep track of the object graphs. So this is one part of the memory lost to this internal bookkeeping.

How do I allocate more memory to Java?

From the main menu, select Help | Change Memory Settings. Set the necessary amount of memory that you want to allocate and click Save and Restart.

How much RAM can Java handle?

If each memory area consists of 8 bytes then a 16-bit system can access 64KB of memory and the 32-bit system can access 4GB of memory.


1 Answers

The heap is not the only thing which consumes virtual memory. Virtual memory is the amount of address space the application has rather than the amount of memory it is using (the resident is a better indicator)

Virtual memory includes all the thread stack space, direct memory and memory mapped files.

The first thing I would check is the number of threads your application uses, the more threads, the more virtual memory.

like image 155
Peter Lawrey Avatar answered Sep 30 '22 16:09

Peter Lawrey