LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 07-28-2006, 11:42 AM   #1
karlovac
LQ Newbie
 
Registered: Mar 2004
Posts: 25

Rep: Reputation: 15
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
 
Old 07-28-2006, 12:21 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 281Reputation: 281Reputation: 281
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...
 
Old 07-28-2006, 12:25 PM   #3
Black Chaos
LQ Newbie
 
Registered: Jan 2005
Posts: 15

Rep: Reputation: 0
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.
 
Old 07-28-2006, 12:33 PM   #4
karlovac
LQ Newbie
 
Registered: Mar 2004
Posts: 25

Original Poster
Rep: Reputation: 15
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...
 
Old 07-28-2006, 12:38 PM   #5
karlovac
LQ Newbie
 
Registered: Mar 2004
Posts: 25

Original Poster
Rep: Reputation: 15
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.
 
Old 07-28-2006, 12:39 PM   #6
Black Chaos
LQ Newbie
 
Registered: Jan 2005
Posts: 15

Rep: Reputation: 0
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.
 
Old 07-28-2006, 12:50 PM   #7
karlovac
LQ Newbie
 
Registered: Mar 2004
Posts: 25

Original Poster
Rep: Reputation: 15
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.
 
Old 07-28-2006, 12:54 PM   #8
Black Chaos
LQ Newbie
 
Registered: Jan 2005
Posts: 15

Rep: Reputation: 0
Whoops my bad. Calculated the amount wrong.

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

Last edited by Black Chaos; 07-28-2006 at 12:57 PM.
 
Old 07-28-2006, 01:58 PM   #9
karlovac
LQ Newbie
 
Registered: Mar 2004
Posts: 25

Original Poster
Rep: Reputation: 15
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.
 
Old 07-28-2006, 02:17 PM   #10
Black Chaos
LQ Newbie
 
Registered: Jan 2005
Posts: 15

Rep: Reputation: 0
You could give `ps aux` a try see if that gives you any helpful information.
 
Old 07-28-2006, 02:34 PM   #11
karlovac
LQ Newbie
 
Registered: Mar 2004
Posts: 25

Original Poster
Rep: Reputation: 15
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.
 
Old 07-28-2006, 02:57 PM   #12
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
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.

Last edited by haertig; 07-28-2006 at 03:07 PM.
 
Old 07-28-2006, 03:06 PM   #13
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD, Raspbian, Arch
Posts: 2,331

Rep: Reputation: 357Reputation: 357Reputation: 357Reputation: 357
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
find linux box by name vs. IP zchoyt Linux - Networking 6 08-15-2005 10:24 PM
Building my first Linux box - what processor/memory? miston Linux - Newbie 3 12-27-2004 05:32 PM
How to find a name of Linux box! ardi76 Linux - Software 4 08-28-2003 03:07 PM
How can I find out the users setup on a Linux box R4z0r Linux - General 4 06-28-2003 11:32 AM
windows cant find linux box manthram Linux - Networking 4 03-31-2002 11:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration