1

I have a problem with a Java app. Yesterday, when i deployed it to have a test run, we noticed that our machine started swapping, even though this is not a real monster app, if you know what i mean. Anyway, i checked the results of top and saw that it eats around 100mb of memory (RES in top) I tried to profile memory and check if there is a memory leak, but i couldn't find one. There was an unclosed PreparedStatement, which i fixed, but it didn't mean much. I tried setting the min and max heap size (some said that min heap size is not required), and it didn't make any difference.

This is how i run it now:

#!/bin/sh

$JAVA_HOME/bin/java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9025 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -XX:MaxPermSize=40m -Xmx32M -cp ./jarName.jar uk.co.app.App app.properties

Here is the result of top:

PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+ COMMAND                                                                                                                                                                          
16703 root      20   0  316m 109m 6048 S  0.0 20.8   0:14.80 java   

The thing i don't understand that i configure max PermSize and max Heap size, which add up to 72mb. Which is enough, the app runs well. Why is it eating 109mb of memory still and what is eating it up? It is a 37mb difference, which is quite high ratio. (34%). I don't think this is a memory leak, because max heap size is set and there is no out of memory error, or anything.

One intertesting thing may be that i made a heap dump with VisualVM and then checked it with EclipseMAT and it said that there is a possible leak in a classloader. This is what it says:

The classloader/component "sun.misc.Launcher$AppClassLoader @ 0x87efa40" occupies 9,807,664 (64.90%) bytes. The memory is accumulated in one instance of "short[][]" loaded by "".Keywords sun.misc.Launcher$AppClassLoader @ 0x87efa40

I cannot make much of this, but may be useful.

Thanks for your help in advance.

EDIT

I found this one, maybe there is nothing i can do... Tomcat memory consumption is more than heap + permgen space

Community
  • 1
  • 1
Zsolt János
  • 491
  • 8
  • 18

2 Answers2

3

Java's memory includes

  • the heap space.
  • the perm gen space
  • thread stack areas.
  • shared libraries, including that of the JVM (will be shared)
  • the direct memory size.
  • the memory mapped file size (will be shared)

There is likely to be others which are for internal use.

Given that 37 MB of PC memory is worth about 20 cents, I wouldn't worry about it too much. ;)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • If I could toss in 20 cents every time my computer needs more memory, I would be a poor man... ;) – brimborium May 25 '12 at 10:30
  • I see your point, the whole point of my concern is that the server, which runs this app has 512 Mb RAM and if i run this app, the machine starts swapping. So this 37 MB is important in the way that if i could reduce it, it would help me. – Zsolt János May 25 '12 at 10:45
  • How much would it cost to increase the memory? That would help you also. – Peter Lawrey May 25 '12 at 12:11
  • @brimborium Instead of a TV at home, I have a PC for watching shows. It has 4 GB because its wasn't worth getting less memory (only saved a couple of pounds) – Peter Lawrey May 25 '12 at 12:13
  • @PeterLawrey Well I got 16GB of RAM, it all depends on what you have to do... Also, he might not have access to the server, so he might not be able to increase the memory. And last but not least: Even if we have very cheap and nearly unlimited access to memory, it is still a good thing to look into that kind of stuff. – brimborium May 25 '12 at 12:32
  • @brimborium Exactly, i want to understand. This isn't a critical issue, i can either move the app to an other server or increase memory, but getting knowledge and understanding is a great thing. :) – Zsolt János May 25 '12 at 12:43
  • @user1417034 Knowing is not always a good thing, but in this case, I totally agree. ;) Nevertheless, +1 to Peter for his list. That is quite interesting. – brimborium May 25 '12 at 12:46
  • @brimborium Currently I am running 8 java processes from my ide, each reporting 200 GB of virtual memory ;) – Peter Lawrey May 25 '12 at 13:41
1

Did you try using JConsole to profile the application http://docs.oracle.com/javase/1.5.0/docs/guide/management/jconsole.html Otherwise you can also use JProfiler trial version to profile the application http://www.ej-technologies.com/products/jprofiler/overview.html?gclid=CKbk1p-Ym7ACFQ176wodgzy4YA

However first step to check high memory usage should be to check if you are using collection of objects in your application like array,map,set,list etc. If yes then check if they keep the references to objects (even though of not used) with them ?

Saurabh
  • 7,894
  • 2
  • 23
  • 31
  • I did do profiling, as i wrote, and i couldn't spot any issues. – Zsolt János May 25 '12 at 10:38
  • Will recommend to try tuning these flags : -XX:MaxHeapFreeRatio and -XX:MinHeapFreeRatio. See more details at http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html under 'Performance options' – Saurabh May 25 '12 at 10:43