简体   繁体   中英

Java process memory usage much more than what Runtime.totalMemory et al methods are telling

I have been stuck with this problem for long now without any hint whatsoever. Will really appreciate if someone can help.

I am running a Java application on device with ARM v7 processor.It has Ubuntu installed on it. The memory usage of the process when seen using "top" or "free -m" commands is about 180 MB which is much more than what the Runtime.freeMemory et al methods are telling me.

This is how I am starting my application:

java -Xcheck:jni -XX:MaxPermSize=25m -Xmx65m -XX:ReservedCodeCacheSize=10m -jar MyAPP.jar myconfig.xml

As far as I understand, the total memory usage of this process should not cross 25 + 65 + 10 = 100 MB . It may be a bit more (other non-heap spaces I am not aware of). But even then I dont think the difference should be this much (80 MB).

This is what "Runtime" commands seem to tell me in my logs (I use those commands to print usage info as part of my debug logs).

Usage Info: Avaialable processors: 2 Free memory available to JVM: 8474344, Current memory usage: 5943576, Maximum memory JVM is allowed to use: 66977792

Output from "top":

PID   USER     PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
3926  linaro   20  0   266m  188m 3636 S  1.0 21.1   0:37.81 java

Output from "free -m":

When Java process is running:

        total       used       free     shared    buffers     cached
Mem:     893         842         51          0         40        216
-/+ buffers/cache:   585        308
Swap:     0          0          0

When the process has been stopped:

         total      used       free     shared    buffers     cached
Mem:      893        656        237          0         40        216
-/+ buffers/cache:   400        493
Swap:      0          0          0

As we can see, 493 - 308 = 185, is matching what "top" is saying about memory usage of this process (Earlier I thought maybe there is a bug with top installed on this machine).

I did remote monitoring of this process too, it shows "committed" heap space of about 14M and non-heap of about 20M. At least the heap space matches what I got from "Runtime" commands. So even remote monitoring tool seems to say that memory usage is much less than 180M.

The most strange part is that the same jar when I run on my laptop (MacBook Pro) or a standard Intel based Linux box (not ARM) everything seems in place. Process uses only about 60 MB of space which is what I expect.

Another point worth mentioning is that the Java VM I am using on ARM device is Java SE Embedded version by Oracle.

Anyone with an idea of this discrepancy in memory usage. Sorry for asking such a long question :-).

UPDATE:

After doing some research about the various parts of a Java process which can consume memory, I zeroed down on native code interaction (JNI). That is the only guy whose memory consumption we can not limit from command live arguments. In my program I am using sqlite-jdbc driver to access database. I commented the database access part and memory usage came down to 24MB!!! So I am guessing that this memory discrepancy has something to do with this sqlite jdbc access, though not yet sure what it is. Will keep updating as I make some progress.

-Sandeep

What you observe is the difference between JVM vs Process Memory.

Have a look at these answers:

Process Memory Vs Heap -- JVM

JVM Process vs JVM Heap memory usage

Since you are using different JVM implementations on each system you might have different memory behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM