How do I find out what's using the memory on a Linux box
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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?
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...
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.
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...
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.
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.
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.
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.
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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.