tags:

views:

79

answers:

5
+3  Q: 

Java memory usages

I cannot understand the Java memory usage. I have an application which is executed with maximum memory size set to 256M. Yet, at some point in time I can see that according to the task manager it takes up to 700MB!

Needless to say, all the rest of the applications are a bit unresponsive when this happens as they are probably swapped out.

It's JDK 1.6 on WinXP. Any ideas ?

+2  A: 

The amount you specify with -Xmx is only for the user accessible heap - the space in which you create runtime objects dynamically.

The Java process will usea lot more space for its own needs, including the JVM, the program and other libraries, constants pool, etc.

In addition, because of the way the garbage collection system works, there may be more memory allocated than what is currently in the heap - it just hasn't been reclaimed yet.

All that being said, setting your program to a maximal heap of 256MB is really lowballing it on a modern system. For heavy programs you can usually request at least 1GB of heap.

As you mentioned, one possible cause of slowness is that some of the memory allocated to Java gets swapped off to disk. In that case, the program would indeed start churning the disk, so don't go overboard if you have little physical memory available. On Linux, you can get page miss stats for a process, I am sure there's a similar way on windows.

Uri
+1  A: 

The memory configured is available to the application. It won't include

  1. the JVM size
  2. the jars/libs loaded in
  3. native libraries and related allocated memory

which will result in a much bigger image. Note that due to how the OS and the JVM work that 700Mb may be shared between multiple JVMs (due to shared binary images, shared libraries etc.)

Brian Agnew
Eventually it turned out that native libraries were to blame as I was using JNA in my application.
Demiurg
A: 

You mean the heap right? As far as i know there are two things to take care. The Xms option which sets an initial java heap size and the Xmx option which sets the maximum java heap space. If the heap memory is overreaching the Xmx value there should be an OutOfMemoryException.

kukudas
A: 

What about the virtual pages it's taking up. I think Windows shows you the full set of everything aggregated.

jghost
+1  A: 

The -Xmx option only limits the java heap size. In addition to the heap, java will allocate memory for other things, including a stack for each thread (2kB by default, set by -Xss), the PermGenSpace, etc. So, depending on how many threads you launch, the number of classes your application loads, and some other factors, you may use a lot more memory than expected.

Also, as pointed out, the Windows task manager may take the virtual memory into account.

Olivier Croisier