views:

96

answers:

4

I think I may have a memory leak in my LAMP application (memory gets used up, swap starts getting used, etc.). If I could see how much memory the various processes are using, it might help me resolve my problem. Is there a way for me to see this information in *nix?

+1  A: 

Use top or htop and pay attention to the "RES" (resident memory size) column.

drhirsch
@drhirsch, I see the RES, but I don't think I need that. My used Mem and used Swap keeps going up. I need to know what's making those go up. Ideas?
StackOverflowNewbie
Resident memory is the memory used by your processes. If none of the processes seems to be using much memory in spite of your total memory usage increasing, the memory could only be used by the kernel. Try sorting after the RES column. Another point maybe too high swappiness when you have heavy disk IO.
drhirsch
A: 

ps shell command

alxx
+1  A: 

Use ps to find the process id for the application, then use top -p1010 (substitute 1010 for the real process id). The RES column is the used physical memory and the VIRT column is the used virtual memory - including libraries and swapped memory.

More info can be found using "man top"

Zaz
@Zaz, by the time I can execute top -pXXXX, the process is already done. So, I get nothing. Suggestions?
StackOverflowNewbie
Regarding "VIRT": For almost all practical purposes, the size of the virtual image tells you nothing - almost every linux system is configured to allow overcomitting of memory and a lot of apps actually do heavy overcommit.
drhirsch
+3  A: 

Getting right memory usage is trickier than one may think. The best way I could find is:

echo 0 $(cat /proc/`pidof process`/smaps  | grep TYPE | awk '{print $2}' | sed 's#^#+#') | bc

Where "process" is the name of the process you want to inspect and "TYPE" is one of:

  • Rss: resident memory usage, all memory the process uses, including all memory this process shares with other processes. It does not include swap;
  • Shared: memory that this process shares with other processes;
  • Private: private memory used by this process, you can look for memory leaks here;
  • Swap: swap memory used by the process;
  • Pss: Proportional Set Size, a good overall memory indicator. It is the Rss adjusted for sharing: if a process has 1MiB private and 20MiB shared between other 10 processes, Pss is 1 + 20/10 = 3MiB

Other valid values are Size (i.e. virtual size, which is almost meaningless) and Referenced (the amount of memory currently marked as referenced or accessed).

You can use watch or some other bash-script-fu to keep an eye on those values for processes that you want to monitor.

For more informations about smaps: http://www.kernel.org/doc/Documentation/filesystems/proc.txt.

Giuseppe Cardone