-->
Page 302
# free total used free shared buffers cached Mem: 30892 28004 2888 14132 3104 10444 -/+ buffers: 14456 16436 Swap: 34268 7964 26304
This shows a 32MB system with 34MB swap space. Notice that nearly all the system memory is being used, and nearly 8MB of swap space has been used.
By default, the free command displays memory in kilobytes, or 1024-byte notation. You can use the -b option to display your memory in bytes, or the -m option to display memory in megabytes. You can also use the free command to constantly monitor how much memory is being used through the -s command. This is handy as a real-time monitor if you specify a .01-second update and run the free command in a terminal window under X11.
The vmstat is a general-purpose monitoring program, which offers real-time display of not only memory usage, virtual memory statistics, but disk activity, system usage, and central processing unit (CPU) activity. If you call vmstat without any command-line options, you'll get a one-time snapshot, for example:
# vmstat procs memory swap io system cpu r b w swpd free buff cache si so bi bo in cs us sy id 0 0 0 7468 1060 4288 10552 1 1 10 1 134 68 3 2 96
If you specify a time interval in seconds on the vmstat command line, you'll get a continuously scrolling report. Having a constant display of what is going on with your computer can help you if you're trying to find out why your computer suddenly slows down, or why there's a lot of disk activity.
You've already seen how you can limit the size of core dump files previously in this hour. There are other settings you can set in your shell. If you're using the bash or pdksh (ksh) shell, you can use the ulimit command's -a option to print your current settings, for example:
# ulimit -a core file size (blocks) 1000000 data seg size (kbytes) unlimited file size (blocks) unlimited max memory size (kbytes) unlimited stack size (kbytes) 8192 cpu time (seconds) unlimited max user processes 256 pipe size (512 bytes) 8 open files 256 virtual memory (kbytes) 2105343
If you're using the tcsh or csh shell, you can use the limit command to list the current settings:
Page 303
$ limit cputime unlimited filesize unlimited datasize unlimited stacksize 8192 kbytes coredumpsize 1000000 kbytes memoryuse unlimited descriptors 256 memorylocked unlimited maxproc 256
These limits are different from limits for the root operator. The limits shown in this example are known as soft limits. To view the shell's hard limits, log in as the root operator, and use the -a limit option (use -Ha for bash or ksh's ulimit command), for example:
$ su Password: # limit -h cputime unlimited filesize unlimited datasize unlimited stacksize 8192 kbytes coredumpsize unlimited memoryuse unlimited descriptors 256 memorylocked unlimited maxproc 256
As you can see, viewing the limits as the root operator in the tcsh shell shows a much different situation. This is another good reason not to run as the root operator! As a sysadmin, you can use these settings to limit the amount of memory or number of processes available to each user. This is extremely handy if you have a number of people working on your computer at the same time, and you want to conserve system memory. For using Linux on a standalone computer under your normal login and working conditions, you'll find the default limits quite reasonable.
As a desperate measure if you need to quickly reclaim memory, you can stop running programs by using the kill command. In order to kill a specific program, you should use the ps command to list current running processes, and then stop any or all of them with the kill command. By default, the ps command lists processes you own and which you can kill, for example:
# ps PID TTY STAT TIME COMMAND 367 p0 S 0:00 bash 581 p0 S 0:01 rxvt 582 p1 S 0:00 (bash) 747 p0 S 0:00 (applix) 809 p0 S 0:18 netscape index.html
Page 304
810 p0 S 0:00 (dns helper) 945 p0 R 0:00 ps
The ps command will list the currently running programs and the program's process number, or PID. You can use this information to kill a process with
# kill -9 809
However, if you need to reclaim memory efficiently, you should use the ps command's -m option, which also lists the memory usage of each process, for example:
# ps -m PID TTY MAJFLT MINFLT TRS DRS SIZE SWAP RSS SHRD LIB DT COMMAND 747 p0 0 3 16 208 364 140 224 224 0 0 (applix) 582 p1 151 274 124 184 436 128 308 268 0 10 (bash) 959 p0 89 20 28 376 404 0 404 320 0 21 ps -m 367 p0 305 826 220 316 600 64 536 428 0 27 bash 810 p0 313 38 164 696 968 108 860 596 0 47 (dns helpe 581 p0 212 508 28 960 1280 292 988 304 0 171 rxvt 809 p0 2615 1205 3900 3692 8684 1092 7592 4644 0 699 netscape
By using this information, you can see that if you want to reclaim the most memory, you should stop the Netscape Web browser, as it is using nearly 9MB of system memory. Although you wouldn't normally use the kill command to stop programs, the kill command can be helpful to stop runaway, or nonresponsive, programs. The kill command works by sending a signal to the Linux kernel, along with the PID, so the kernel can act on the process. There are various signals you can use, although, as I've pointed out, the -9, or SIGKILL option is the most abrupt and drastic. You can see a list of different signals by using the kill command's -l option, for example:
# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGIOT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
For more details on these signals, and the kill command, see its manual page.
The ps command has nearly two dozen command-line options, and you can also list all running processes. See the ps manual page for more information. You can also use the top command, discussed next, to find and kill processes.
The top command, found under the /usr/bin directory, is a system monitor that displays