LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Java Linux Process memory footprint with and without -Xmx (https://www.linuxquestions.org/questions/linux-general-1/java-linux-process-memory-footprint-with-and-without-xmx-586324/)

rolandofghent 09-21-2007 08:56 AM

Java Linux Process memory footprint with and without -Xmx
 
I have questions about some strange behavior I am seeing with Java 5.0 on Linux.

First here is the environment:

Code:

[~]:> uname -a
        Linux xxxxxxxxxx 2.4.21-47.ELsmp #1 SMP Wed Jul 5 20:38:41 EDT 2006 i686 i686 i386 GNU/Linux

        [~]:> java -version
        java version "1.5.0_12"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_12-b04)
        Java HotSpot(TM) Server VM (build 1.5.0_12-b04, mixed mode)

I wrote a very simple java program that just starts up and sleeps.

Wait.java:
Code:

        import java.lang.*;
        public class Wait{
        public static void main(String args[]) throws Throwable{
            Thread.sleep(100000);
        }
        }

I run the program with no memory arguments:

Code:

        [~]:> java -classpath . Wait &
        [1] 30841
        [~]:> pmap 30841 | tail -1
        mapped: 1220332K    writeable/private: 1160336K    shared: 49468K

Note that pmap is showing me that it is using over 1 gig of memory. The java documentation says that if I don't specify a max heap size that the Max heap is 64 Megs. 1 gig of memory for a 64 Meg heap is a lot of overhead.

Top however, seems to be showing me that I am only using 10 megs.

Code:

        [~]:> top -bp 30841


        08:44:00  up 187 days, 14:20, 12 users,  load average: 0.38, 0.69, 0.37
        1 processes: 1 sleeping, 0 running, 0 zombie, 0 stopped
        CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle
                  total  13.6%    0.0%    3.9%  0.0%    0.0%    7.0%  75.3%
                  cpu00  16.9%    0.0%    3.7%  0.0%    0.0%    5.6%  73.5%
                  cpu01  11.2%    0.0%    2.8%  0.0%    0.0%    8.4%  77.5%
                  cpu02  11.2%    0.0%    2.8%  0.0%    0.0%    6.5%  79.4%
                  cpu03  15.0%    0.0%    6.6%  0.0%    0.0%    7.5%  70.7%
        Mem:  4101252k av, 3859028k used,  242224k free,      0k shrd,  145796k buff
                          2619760k actv,  545440k in_d,  65836k in_c
        Swap: 2048184k av,  594480k used, 1453704k free                1726032k cached

          PID USER    PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM  TIME CPU COMMAND
        30841 xxxxxxx  21  0 10336  10M  6408 S    0.0  0.2  0:00  0 java

Now I run the program with a 64 meg min and max heap.

Code:

        [~]:> java -classpath . -Xms64m -Xmx64m  Wait &
        [2] 30894

Then run pmap.

Code:

        [~]:> pmap 30894 | tail -1
        mapped: 256276K    writeable/private: 196280K    shared: 49468K

So it looks like it is using 196 megs. Which is fine because I know that there is overhead on top of the actual Heap size.

Then I run top on the process:

Code:

        [~]:> top -bp 30894

        08:45:13  up 187 days, 14:21, 12 users,  load average: 0.26, 0.58, 0.36
        1 processes: 1 sleeping, 0 running, 0 zombie, 0 stopped
        CPU states:  cpu    user    nice  system    irq  softirq  iowait    idle
                  total    8.4%    0.0%    0.4%  0.2%    0.2%    7.4%  83.1%
                  cpu00    8.4%    0.0%    0.0%  0.9%    0.0%    6.5%  84.1%
                  cpu01    7.4%    0.0%    0.0%  0.0%    0.9%    7.4%  84.1%
                  cpu02    4.6%    0.0%    0.0%  0.0%    0.0%    8.4%  86.9%
                  cpu03  13.0%    0.0%    1.8%  0.0%    0.0%    7.4%  77.5%
        Mem:  4101252k av, 3904008k used,  197244k free,      0k shrd,  146224k buff
                          2657632k actv,  549712k in_d,  65836k in_c
        Swap: 2048184k av,  594480k used, 1453704k free                1731604k cached

          PID USER    PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM  TIME CPU COMMAND
        30894 xxxxxxx  21  0 10340  10M  6408 S    0.0  0.2  0:00  3 java

And it shows me that I am using 10 megs.

So what is going on here?

1) Why is top and pmap showing different values between the SIZE (top) and the writeable/private (pmap) ?
2) if the value in pmap is correct, why if I don't specify the max heap is it not taking the document default value?:

From the Java documentation:

Quote:

-Xmxn Specify the maximum size, in bytes, of the memory allocation pool. This value must a multiple of 1024 greater than 2MB. Append the letter k or K to indicate kilobytes, or m or M to indicate megabytes. The default value is 64MB.

timothyb89 09-22-2007 06:36 PM

two things:
#1: You could be wasting memory importing java.lang.*, and, even so, its imported by default anyway.

Assume, for clarity, that kB = kilobyte and kb = kilobit, and mB = megabyte and mb is megabyte.
#2: pmap is returning a value in kilobits, not kilobytes.
Its not *actually* 191.67 MBs- if you want MBs then its really 23.96 MB.
So from that, we should be able to deduce that top is giving us our usage value in kilobytes. Hopefully that should explain why the numbers were so different ;-)

(Note: 8 bits=1 byte, so kb/8 = kB. )

More info:
http://en.wikipedia.org/wiki/Byte
http://en.wikipedia.org/wiki/Bit

rolandofghent 09-26-2007 12:17 PM

Quote:

Originally Posted by timothyb89 (Post 2900551)
two things:
#2: pmap is returning a value in kilobits, not kilobytes.
Its not *actually* 191.67 MBs- if you want MBs then its really 23.96 MB.
So from that, we should be able to deduce that top is giving us our usage value in kilobytes. Hopefully that should explain why the numbers were so different ;-)

(Note: 8 bits=1 byte, so kb/8 = kB. )

More info:
http://en.wikipedia.org/wiki/Byte
http://en.wikipedia.org/wiki/Bit

Ok, so it is is kilobits instead of bytes.

But why is pmap showing 23.96 MB and Top is showing 10 MB?

Joe

jlliagre 09-26-2007 06:09 PM

Quote:

Originally Posted by timothyb89 (Post 2900551)
two things:
#1: You could be wasting memory importing java.lang.*, and, even so, its imported by default anyway.

I'm afraid you are contradicting yourself here. If it's imported by default which is indeed true, how could importing it explicitly waste memory ?
Anyway, the import statement in java has no visible effect on the resulting application memory footprint. It is mostly a source code terseness convenience.
Quote:

Assume, for clarity, that kB = kilobyte and kb = kilobit, and mB = megabyte and mb is megabyte.
m is the ISO prefix for milli so mB would be millibyte and mb millibit (one thousandth of a bit ...) , shoud these units exist ;)
One should use uppercase M to mean Mega.
Quote:

#2: pmap is returning a value in kilobits, not kilobytes.
I sorry but this is incorrect.
pmap is certainly returning values in bytes so these Ks are kiloBytes.

rolandofghent 09-27-2007 07:55 AM

Quote:

Originally Posted by jlliagre (Post 2904845)
pmap is certainly returning values in bytes so these Ks are kiloBytes.

So can you answer my Question #1? Why is the value shown for Top different than what is shown for pmap?

Thanks,

Joe

jlliagre 09-27-2007 09:08 AM

There can be many reasons, including you are comparing java heap size, physical, virtual and possibly multiply times mapped memory.

I can't tell much more as you only show pmap last output line.


All times are GMT -5. The time now is 08:16 PM.