Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Softpanorama Search

Unix top command

News Books Minitutorial Recommended Links Reference nice netstat
uptime free ps pmap ptree

Humor

Etc

The top command displays currently running processes and important information about them including their memory and CPU usage. The list is both real-time and interactive. An example of output from the top command is provided as follows:

To exit top press the q key. Useful interactive commands that you can use:

For more information, refer to the top(1) manual page.

Old News

The Joel on Software Discussion Group - UNIX 'Top' command

Under 'Top' there are two columns: SIZE and RES

SIZE- refers to the virtual memory

RES- refers to the resident memory used in the RAM


It is ideal to ensure that RES value is as low as possible? And to keep the RES value far below the SIZE value?
Lotus
Friday, May 16, 2008
 
 
The general wisdom is to create your swap partition to be twice the amount of ram you have, up to about 512MB swap (at least I've never bothered making a larger swap for my desktop machine).

Keep in mind that Linux will try to keep RAM as full as it possible can, so it will appear that you're hurting for RAM when you're really doing ok, the kernel is just attempting to keep the system running as quickly as possible.

I have no idea about the behavior of other unices.
Fake Programmer
Friday, May 16, 2008
 
 
Some stuff from 'man top':

VIRT  --  Virtual Image (kb)
The total amount of virtual memory used by the task.  It includes all code, data and shared libraries plus pages that have been swapped out.

VIRT = SWAP + RES.

SWAP  --  Swapped size (kb)
The swapped out portion of a task’s total virtual memory image.

RES  --  Resident size (kb)
The non-swapped physical memory a task has used.

RES = CODE + DATA.

----
Btw I don't have a 'SIZE' column when I run top, so YMMV.

But I think if you're looking for memory efficiency then I'm reasonably sure you want to be able to reduce RES as much as possible, and try to have SWAP as close as possible to RES.

The lower RES is, the less memory your program will need, the more consistently you can have SWAP approaching RES the more of your program is available to be swapped out, thereby reducing the drain on the system's memory. No idea how easy it would be to do this though; think it would require a pretty tight control over the compilation to have the most used code bundled into the smallest number of pages.

No doubt I'll probably be shot down over some gross inaccuracy, but hopefully I'm in the right vicinity of what you're asking...
Graham Allan
Friday, May 16, 2008
 
 
What is your real problem?

Are you a programmer asking how to write your program efficiently so it is not a memory hog, or a system administrator asking how to slow the system down by increasing swapping and paging so it looks like you have plenty of unused memory?

Because the latter is a really bad idea. As Fake Programmer said, most systems use "spare" memory for caching in order to run faster. Are you sure this is not what you are seeing? This memory can be instantly freed if required by an application.

That said, there are legitimate reasons for wanting to limit memory usage by processes. Perhaps one memory hog is "crowding out" another application. You may be able to do this using startup parameters of the program itself, with java vm's and databases for example, or the operating system may provide resource controls.
John L Send private email
Saturday, May 17, 2008
 
 
 
This topic is archived. No further replies will be accepted.

 

Recommended Links

top (Unix) - Wikipedia, the free encyclopedia

top - Linux Command - Unix Command

How do I Use the Unix Top Command

Linux and Unix top command help

Reference

About top

Display Linux tasks.

Syntax

top -hv | -bcisS -d delay -n iterations -p pid [, pid ...]

The traditional switches '-' and whitespace are optional.

-b Batch mode operation
Starts top in 'Batch mode', which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you've set with the '-n' command-line option or until killed.
-c Command line/Program name toggle
Starts top with the last remembered 'c' state reversed. Thus, if top was displaying command lines, now that field will show program names, and visa versa. See the 'c' interactive command for additional information.
-d Delay time interval as: -d ss.tt (seconds.tenths)
Specifies the delay between screen updates, and overrides the corresponding value in one's personal configuration file or the startup default. Later this can be changed with the 'd' or 's' interactive commands.

Fractional seconds are honored, but a negative number is not allowed. In all cases, however, such changes are prohibited if top is running in 'Secure mode', except for root (unless the 's' command-line option was used). For additional information on 'Secure mode' see topic 5a. SYSTEM Configuration File.

-h Help
Show library version and the usage prompt, then quit.
-i Idle Processes toggle
Starts top with the last remembered 'i' state reversed. When this toggle is Off, tasks that are idled or zombied will not be displayed.
-n Number of iterations limit as: -n number
Specifies the maximum number of iterations, or frames, top should produce before ending.
-u Monitor by user as: -u somebody
Monitor only processes with an effective UID or user name matching that given.
-U Monitor by user as: -U somebody
Monitor only processes with a UID or user name matching that given. This matches real, effective, saved, and filesystem UIDs.
-p Monitor PIDs as: -pN1 -pN2 ... or -pN1, N2 [,...]
Monitor only processes with specified process IDs. This option can be given up to 20 times, or you can provide a comma delimited list with up to 20 pids. Co-mingling both approaches is permitted.

This is a command-line option only. And should you wish to return to normal operation, it is not necessary to quit
and and restart top -- just issue the '=' interactive command.

-s Secure mode operation
Starts top with secure mode forced, even for root. This mode is far better controlled through the system configuration file (see topic 5. FILES).
-S Cumulative time mode toggle
Starts top with the last remembered 'S' state reversed. When 'Cumulative mode' is On, each process is listed with the cpu time that it and its dead children have used. See the 'S' interactive command for additional information regarding this mode.
-v Version
Show library version and the usage prompt, then quit.

Examples

When operating top, the two most important keys are help ('h' or '?') and quit ('q') key. Alternatively, you could simply use the traditional interrupt key ('^C') when you're done.

top

Running the above command would give you a display similar to the below example. While the tasks are being displayed you can use any of the startup commands listed after the below example output.

top - 20:50:55 up 167 days, 14:26, 82 users, load average: 0.13, 0.05, 0.01
Tasks: 3 total, 1 running, 2 sleeping, 0 stopped, 0 zombie
Cpu(s): 0.0% user, 2.2% system, 0.0% nice, 97.8% idle
Mem: 514964k total, 507860k used, 7104k free, 30404k buffers
Swap: 1630588k total, 158708k used, 1471880k free, 275380k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 
3747 hope 11 0 2004 1608 1608 S 0.3 0.3 0:00.56 sshd 
22245 hope 10 0 688 676 672 S 0.0 0.1 0:00.06 csh 
14015 hope 10 0 1024 1024 848 R 0.0 0.2 0:00.00 top

Startup Defaults
The following startup defaults assume no configuration file, thus no user customizations. Even so, items shown with an
asterisk ('*') could be overridden through the command-line.

A Alt display Off (full-screen)
* d Delay time 3.0 seconds
I Irix mode On (no, 'solaris' smp)
* p PID monitoring Off
* s Secure mode Off (unsecured)
B Bold disable Off
l Load Avg/Uptime On (thus program name)
t Task/Cpu states On (1+1 lines, see '1')
m Mem/Swap usage On (2 lines worth)
1 Single Cpu On (thus 1 line if smp)
b Bold hilite On (not 'reverse')
* c Command line Off (name, not cmdline)
* i Idle tasks On (show all tasks)
R Reverse sort On (pids high-to-low)
* S Cumulative time Off (no, dead children)
x Column hilite Off (no, sort field)
y Row hilite On (yes, running tasks)
z color/mono Off (no, colors)


Copyright © 1996-2009 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Site uses AdSense so you need to be aware of Google privacy policy. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Disclaimer:

Last modified: August 11, 2009