Monday, October 11, 2010

Meminfo in Linux

To check the total memory and swap allocated while installing the OS and all other useful information can be found using this command:

cat /proc/meminfo

While for checking the total memory used and free memory normally we use the command:

free -m (which shows the memory in MB)

The Details description of the free is given below:
   

The free command provides information about unused and used memory and swap space on any computer running Linux or another Unix-like operating system.

Memory consists of mainly of random access memory (RAM) chips that have been built into multi-chip modules that are, in turn, plugged into slots on the motherboard (i.e., the main circuit board on a personal computer or workstation). Swap space is is a portion of a hard disk drive (HDD) that is used to simulate additional main memory (i.e., which is used for virtual memory).

The basic syntax of free is

    free [options]

free accepts no arguments (i.e., input data) and is commonly used without any options. When used with no options, free presents a small table containing six columns and three rows of data, all expressed in kilobytes.

The first row, labeled Mem, displays physical memory utilization, including the amount of memory allocated to buffers and caches. A buffer, also called buffer memory, is usually defined as a portion of memory that is set aside as a temporary holding place for data that is being sent to or received from an external device, such as a HDD, keyboard, printer or network.

The second line of data, which begins with -/+ buffers/cache, shows the amount of physical memory currently devoted to system buffer cache. This is particularly meaningful with regard to application programs, as all data accessed from files on the system that are performed through the use of read() and write() system calls1 pass through this cache. This cache can greatly speed up access to data by reducing or eliminating the need to read from or write to the HDD or other disk.

The third row, which begins with Swap, shows the total swap space as well as how much of it is currently in use and how much is still available.

Several options are available to change the unit of display for free from its default kilobytes, including -b for bytes, -m for megabytes and -g for gigabytes. Of these, -m is usually the most useful. Thus, for example, to show all of the data in megabytes, the following would be used:

    free -m

free will report slightly less memory as being in a computer than the actual total. This is mostly because the kernel (i.e., the core of the operating system) cannot be swapped out (i.e., the kernel always remains in main memory while the computer is in operation), and thus the memory that it occupies can never be freed. There can also be regions of memory that are reserved for other purposes, according to the specific system architecture.

The -t option instructs free to additionally display a fourth line of data containing the totals for physical memory and swap space.

The -s option followed by an integer tells free to keep providing a new, updated output at regular intervals for which the integer indicates the number of seconds. This scrolling output can be terminated by simultaneously pressing the CONTROL and c keys. Thus, for example, the following would provide new data every five seconds and display the output in megabits:

    free -ms 5

An alternative to using free with its -s option would be to run it using the watch command, which by default runs a program provided to it as an argument every two seconds after first temporarily clearing the screen. This can make it easier to see changes as they occur because there is no scrolling. The program can be terminated and the screen returned to its former state by using the CONTROL and c key combination. Thus, for example, to display memory utilization every two seconds, the following would be used:

    watch free


--------------------------------------------------------------------------------------------------------

Alot of theory right..let us consider a small example:

Normally first row in the meminfo tells that most of the memory usage is due to data Buffers and cached which allocated and handled by the OS and tends to use up all the available memory , where the amount of memory used by user process /application is small.

Please take a look at the following example which illustrate how to use free command to verify the amount of free memory :

total used free shared buffers cached
Mem: 4040360 4012200 28160 0 176628 3571348
-/+ buffers/cache: 264224 3776136
Swap: 4200956 12184 4188772
$

In this example the total amount of available memory is 4040360 KB. 264224 KB are used by processes and 3776136 KB are free for other applications. Don't get confused by the first line which shows that 28160KB are free! If you look at the usage figures you can see that most of the memory use is for buffers and cache since Linux always tries to use RAM to the fullest extent to speed up disk operations. Using available memory for buffers (file system metadata) and cache (pages with actual contents of files or block devices) helps the system to run faster because disk information is already in memory which saves I/O. If space is needed by programs or applications like Oracle, then Linux will free up the buffers and cache to yield memory for the applications. So if your system runs for a while you will usually see a small number under the field "free" on the first line.

---------------------------------------------------------------------------------------------------------
How to check the total memory used in the server?

Use the following command, which will list memory usage ascending by process.

ps -e -orss=,args= | sort -b -k1,1n

No comments: