LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   How do I find out what's using the memory on a Linux box (https://www.linuxquestions.org/questions/linux-general-1/how-do-i-find-out-whats-using-the-memory-on-a-linux-box-468588/)

karlovac 07-28-2006 11:42 AM

How do I find out what's using the memory on a Linux box
 
I have a Linux server with about 20 websites on it. None of them (as far as I know) receives a particularly large amount of traffic.

Recently we've been getting out of memory errors in PHP pages across various web sites on that server, e.g.

Quote:

Fatal error: Allowed memory size of 1048880 bytes exhausted (tried to allocate 46080 bytes) in /home/virtual/site14/fst/var/www/squirrelmail/functions/imap_mailbox.php on line 658

Fatal error: Allowed memory size of 300 bytes exhausted (tried to allocate 64 bytes) in Unknown on line 0
The machine in question has 1GB RAM, which is quite a generous helping. If I find out how much memory is free:

# free

... it seems that most of the memory is in use:

Code:

            total      used      free    shared    buffers    cached
Mem:      1032264    997416      34848          0      63484    365264
-/+ buffers/cache:    568668    463596
Swap:      3004072      94620    2909452

Note that there's only about 35MB (of the 1GB) free now. Next I run top to see what's using up the memory:

# top

... and hit "M" to sort by memory usage. The results don't seem to lead me anywhere:

Code:

  PID USER    PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM  TIME CPU COMMAND
16209 root      16  0 11844  11M 11592 S    0.0  1.1  0:04  0 httpd
 3341 root      15  0 19624 4372  3288 S    0.0  0.4  0:18  0 spamd
 3181 mysql    16  0 14428 3188  3188 S    0.0  0.3  2:32  0 mysqld

Apparently Apache is the biggest guzzler at 1.1% of total memory consumption, at around 12MB. In fact, the top memory consumers don't even add up to all that much.

Is top the wrong tool to be using to profile memory? Is there another way to see what's causing the large amount of memory to be used?

Thanks and take care,

Antun

pljvaldez 07-28-2006 12:21 PM

Quote:

Originally Posted by karlovac

Code:

            total      used      free    shared    buffers    cached
Mem:      1032264    997416      34848          0      63484    365264
-/+ buffers/cache:    568668    463596
Swap:      3004072      94620    2909452


Actually, it's the -/+ buffers/cache line that tells you how much memory is free and used for application space. Linux always takes unused RAM and uses it to buffer/cache data for quicker recall. This allows your system to run faster. But when a program needs that RAM, it drops the cached data and gives the RAM to an application.

The bigger question I have is why you have 463MB of RAM free for applications, but your machine is swapping 95MB...

Black Chaos 07-28-2006 12:25 PM

All you memory is being used by PHP.

Quote:

Fatal error: Allowed memory size of 1048880 bytes exhausted (tried to allocate 46080 bytes) in /home/virtual/site14/fst/var/www/squirrelmail/functions/imap_mailbox.php on line 658
That says that PHP has used all the memory and failed to get more.(cause it doesn't exist)

So you need to find out whats wrong with that script and why its using all your memory.

karlovac 07-28-2006 12:33 PM

What do you mean - is that something that I mis-set? Is there a place on a Linux machine where you can configure how much memory it allocates for different tasks?

-Antun



Quote:

Originally Posted by pljvaldez
The bigger question I have is why you have 463MB of RAM free for applications, but your machine is swapping 95MB...


karlovac 07-28-2006 12:38 PM

Thanks, but it's not just that PHP script that's throwing that error. Other scripts are throwing it too. So it's not that particular PHP script, but I guess it could be *any* PHP script.

What I'm trying to do is figure out what script or service is using up all the memory and leaving none for PHP. If PHP is using up all the memory, how come it doesn't show up on top?

Will some things not get written out to top?

Take care,

Antun



Quote:

Originally Posted by Black Chaos
That says that PHP has used all the memory and failed to get more.(cause it doesn't exist)

So you need to find out whats wrong with that script and why its using all your memory.


Black Chaos 07-28-2006 12:39 PM

Something in the script, like a variable, is becoming too large and using all the memory. It looks like the script currently has access to the entire amount of memory. There will be a file called php.ini usually in /etc/ that has a setting called memory_limit. If you set that to something like 8M it will limit the PHP script to only be able to use 8megs of ram. But you will still have the problem of that script failing, it will just fail earlier.

karlovac 07-28-2006 12:50 PM

The /etc/php.ini file caps the amount of memory for a PHP script to 300MB at the moment. That's in the global /etc/php.ini. We use Ensim control panel to divide the server into separate sites, each of which has its own php.ini file. The one for the site in question has memory_limit = 32M.

The error I'm getting says this:

Quote:

Fatal error: Allowed memory size of 1048880 bytes exhausted (tried to allocate 46080 bytes)
(The italics are mine). 1048880 bytes is only around 1MB - that's way less than either of the memory_limit settings in PHP. So it doesn't seem like PHP is using all (1GB) of system memory at all.

-Antun


Quote:

Originally Posted by Black Chaos
Something in the script, like a variable, is becoming too large and using all the memory. It looks like the script currently has access to the entire amount of memory. There will be a file called php.ini usually in /etc/ that has a setting called memory_limit. If you set that to something like 8M it will limit the PHP script to only be able to use 8megs of ram. But you will still have the problem of that script failing, it will just fail earlier.


Black Chaos 07-28-2006 12:54 PM

Whoops my bad. Calculated the amount wrong.

In top make sure it is showing both RES and VIRT memory usages.

karlovac 07-28-2006 01:58 PM

My version of top doesn't seem to have RES and VIRT. The man page says that SIZE (which is displayed in the output I get) is the virtual size of the process (code+data+stack). I'm guessing that VIRT is the virtual size, so it appears that I am seeing that value.

I'm not sure what RES is.

Take care,

Antun


Quote:

Originally Posted by Black Chaos
In top make sure it is showing both RES and VIRT memory usages.


Black Chaos 07-28-2006 02:17 PM

You could give `ps aux` a try see if that gives you any helpful information.

karlovac 07-28-2006 02:34 PM

Wow - that turns up some much more interesting information.

There are a bunch of apache processes guzzling 40% of my memory.

I wonder why top didn't show anything like that up and ps did?

-Antun

Quote:

Originally Posted by Black Chaos
You could give `ps aux` a try see if that gives you any helpful information.


haertig 07-28-2006 02:57 PM

Quote:

Originally Posted by karlovac
My version of top doesn't seem to have RES and VIRT.

You have four "groups" (a.k.a. "windows") available in your top display. Many think there's only one - the one that comes up by default.

Run top, then type "G1", or "G2", or "G3", or "G4" to switch between the various groups (you need to use a capital "G"). Default display is "G1", but "G3" is the memory group.
Quote:

I'm not sure what RES is.
Run "man top" to see all the stuff you can do and what each column means. There's lots of stuff there - it's a long manpage.

haertig 07-28-2006 03:06 PM

Quote:

Originally Posted by karlovac
There are a bunch of apache processes guzzling 40% of my memory.

I wonder why top didn't show anything like that up and ps did?

Top shows individual processes. If you have a bunch of Apaches processes and each on individually does not use up a terrible amount of resources, but the group as a whole does, this won't be readily obvious with top.

When using ps, often times the display of the command being run is truncated, to fit your horizontal screen width. Add a few w's to the command to make it "wide", "wider", "widest"
Code:

$ ps aux
$ ps auxw
$ ps auxww
$ ps auxwww



All times are GMT -5. The time now is 09:29 AM.