Linux – Identify Process Consuming All Memory

javalinuxmemory

We have 4GB on our linux server but we can currently use only ~1.8GB for our java-server, which is the first java process listed below. (200 MB are free so we can maximal use 1.6GB + 0.2GB)

The machine crashes when we are using more. So we specified -Xmx1600m and -XX:MaxPermSize=200m to limit the server RAM to 1.8GB. But we need more RAM! Where is the remaining RAM gone?

Here is the program output of top sorted against memory usage (via big 'M'):

Mem:   4083952k total,  3857856k used,   226096k free,   169320k buffers
Swap:  2104504k total,      176k used,  2104328k free,  1939080k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                           
28155 root      16   0 1835m 1.6g 7848 S    2 40.5  47:36.26 java                                                                                              
19609 root      16   0 45996 7052 3148 S    0  0.2  14:35.97 httpd2-prefork                                                                                    
 6802 root      16   0 46132 5916 1932 S    0  0.1   0:00.09 httpd2-prefork                                                                                    
 6866 root      15   0 46132 5916 1932 S    0  0.1   0:00.07 httpd2-prefork    

As you can see there are a lot (and even more) httpd2-prefork processes. But even if I accumulate the bytes (25* ~46KB = ~1MB) it will never get that big.

free -m prints:

             total       used       free     shared    buffers     cached
Mem:          3988       3768        219          0        165       1894
-/+ buffers/cache:       1708       2279
Swap:         2055          0       2055

Where is my mistake? Can I tune the server to give the java process more RAM?

BTW: we are not using a virtual machine like here

Update

As it was pointed out in the comments: it is a 32bit kernel only 🙁 (via uname -a).
So I can use only 3GB at least?
But the server itself seems to be 64 bit? Because of the lm (long mode) in the output?

grep flags /proc/cpuinfo
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe lm constant_tsc pni monitor ds_cpl cid cx16 xtpr
...

Here is another very similar question.

Best Answer

Ah, I see you have mistaken how to calculate free RAM in Linux.

Linux tends to heavily utilize all the RAM by caching stuff. Instead of reading directory listing from the disk every time, it caches the directory entries in RAM. It caches the frequently used files, so they don't need to be loaded from the disk every time. In case some process actually needs the RAM for its use, the cache will be immediately evicted.

So the formula for calculating the actually used RAM is total - (free + buffers + cached), in your case 3988 - (219 + 165 + 1894) or in other words 3988 - 2278. That is 1710 megabytes of RAM in use and 2278 megabytes of RAM for you still to consume.

Don't believe me? See, your server doesn't even have any swap in use. :)

But of course, a machine crash -- if you mean a hard lock up -- is a strange thing. That really should not happen. I suspect a faulty RAM stick which only gets accessed in case of >2 GB of RAM in use.