Memory is one of the most important resources managed by the Linux kernel. The memory management system is the most important part of the operating system because the system's physical memory is always less than the amount of memory required by the system. Virtual memory is the strategy used to overcome this paradox. The system's virtual memory makes the system appear to have more memory than the actual memory by sharing memory between processes. Linux supports virtual memory by using disk as an extension of RAM, allowing available memory to expand effectively. The core saves the memory blocks that are not currently in use to the hard drive, freeing up memory for other purposes. When the original content is to be used again, read back the memory.
I. Memory usage monitoring
(1) Real-time monitoring of memory usage
Use the ' free ' command at the command line to monitor memory usage
Total used free shared buffers Cached
mem:256024 192284 63740 0 10676 101004
-/+ buffers/cache:80604 175420
swap:522072 0 522072
It gives a system of 256 megabytes of RAM and 512 MB of swap space. The third line of output (Mem:) shows physical memory. The total column does not show the physical memory used by the core (typically around 1MB). The Used column shows the total amount of memory being used (the second row does not count as buffering). The free column shows all unused memory. Shared columns show the total amount of memory shared by multiple processes. The Buffers column displays the current size of the disk cache. Line five (swap:) on the swap space, display information similar to the above. If this behavior is 0, then swap space is not used. In the default state, the free command displays memory usage in kilobytes (that is, 1024 bytes). You can use the-H parameter to display memory usage in bytes, or you can use the-m parameter to display memory usage in megabytes. You can also use commands with the-s parameter to continuously monitor memory usage:
#free–b–s2
This command will continuously report memory usage in the terminal window, updated every 2 seconds.
(2) Combination watch? The free command is used to monitor memory usage in real time:
Every 2.0s:free Fri 6 06:06:12 2007
Total used free shared buffers Cached
mem:233356 218616 14740 0 5560 64784
-/+ buffers/cache:148272 85084
swap: 622584 6656 615928
The
Watch command performs a free every two seconds, clearing the screen before performing and displaying the data in the same position. Because the watch command does not scroll the screen, it is suitable for a long time to monitor memory usage. You can use the-N option to control the frequency of execution, or you can take advantage of the-D option to have the command display in a different place each time. The watch command executes until you press [Ctrl]-[c].
&NBSP
Second, the concept of virtual memory
(1) Linux virtual Memory Implementation mechanism
 
The implementation of Linux virtual memory requires six mechanisms: address mapping mechanism, memory allocation recycle mechanism, Caching and refreshing mechanism, request page mechanism, exchange mechanism, memory sharing mechanism.
First, the memory management program maps the logical address of the user program to the physical address through the mapping mechanism. The Request page request is issued if the virtual address to be used in the program is found without the corresponding physical memory when the user program is running, and if there is free memory available for allocation, the memory is requested ( The allocation and recycling of memory is used, and the physical pages being used are recorded in the cache (using the caching mechanism). If there is not enough memory available to allocate, then the swap mechanism is invoked to free up a portion of the memory. In the address map, the physical page is searched through TLB (translation backup memory), the Exchange cache is also used in the exchange mechanism, and the page table is modified to map the file address after the physical page content is exchanged to the Exchange file.
(2) virtual memory capacity set
&NBSP
You may be told that you should allocate twice times the amount of virtual memory in physical memory, but this is an unfixed rule. If your physical preservation is relatively small, you can set this up. If you have 1G of physical memory or more, you can shrink the virtual memory. Linux uses a lot of memory as cache, but reclaims it when resources are tight. You just have to see that swap is 0 or too small to be assured, because the storage is not the biggest waste.
Third, the Vmstat command to monitor virtual memory usage
Vmstat is the abbreviation for Virtual Meomory Statistics (fictitious memory statistics) that monitors virtual memory, processes, and CPU activity on the operating system. It is the overall situation of the system statistics, the disadvantage is that a process can not be in-depth analysis. Typically, Vmstat 5 5 (for 5 sampling in 5 seconds) is used to test the command. Will get a data rollup that can reflect the real system situation.
procs-----------Memory-------------Swap-------io------System------CPU----
R b swpd free buff cache si so bi bo in CS US sy ID WA
1 0 62792 3460 9116 88092 6 30 189 89 1061 569 17 28-54 2
0 0 62792 3400 9124 88092 0 0 0 14 884 434 4 14-81 0
0 0 62792 3400 9132 88092 0 0 0 14 877 424 4 15-81 0
1 0 62792 3400 9140 88092 0 0 0 14 868 418 6 20-74 0
1 0 62792 3400 9148 88092 0 0 0 15 847 400 9 25-67 0
The Vmstat command output is divided into six sections:
(1) Process procs:
R: The number of processes waiting in the run queue.
B: The number of processes waiting for IO.
(2) Memory Memoy:
SWPD: Swap memory currently available (in kilobytes).
Free: Idle memory (in kilobytes).
Buff: Buffers The number of memory (in kilobytes).
Cache: The amount of memory that is used as the cache (in kilobytes).
(3) Swap page
Si: The number of swap pages exchanged from disk to memory, in units: kb/seconds.
So: the number of swap pages from memory to disk, in units: kb/seconds.
(4) IO block device:
BI: The number of blocks sent to the block, in units: Block/sec.
Bo: The number of blocks received from the block device, in units: Block/sec.
(5) System systems:
In: The number of interrupts per second, including clock interrupts.
CS: The number of ambient (context) switches per second.
(6) CPU central Processor:
CS: Time used by the user process. expressed as a percentage.
SY: The time used by the system process. expressed as a percentage.
ID: The idle time of the central processing Unit. expressed as a percentage.
If R is often greater than 4, and the ID is often less than 40, the CPU load is heavy. If the Bi,bo is not equal to 0 for a long time, the physical memory capacity is too small.
Four, the Linux server memory leak and reclaim the memory method
1, the definition of memory leakage:
In general we often say that the memory leak is the heap memory leakage. Heap memory is that the program is allocated from the heap, of any size (the size of the memory block can be determined during the program run), and the freed memory must be displayed after use. The application generally uses functions such as malloc,realloc,new to allocate a piece of memory from the heap, and after use, the program must be responsible for the corresponding call free or delete to release the memory block, otherwise, this piece of memory can not be reused, we say this memory leak.
2, the harm of memory leakage
From the user's point of view of the program, the memory leak itself will not produce any harm, as a general user, do not feel the existence of memory leaks. What really harms is the accumulation of memory leaks, which ultimately consumes all the memory of the system. In this sense, a one-time memory leak is harmless because it does not accumulate, and the implicit memory leak is very harmful because it is more difficult to detect than frequent and accidental memory leaks. A program that has a memory leak problem can also cause the program to degrade dramatically, in addition to consuming more memory. If this happens to the server, it can severely affect usage even if the system does not crash.
3, Memory leak detection and recovery
Trouble with memory overflows may come when you're writing complex programs with more pointers. In Linux or UNIX, C, C + + language is the most used tool. But our C + + program lacks the means to detect memory information, and can only use the top command to observe the total dynamic memory of the process. And when the program exits, we cannot learn any memory leaks.
Using the KILL command
Using the Linux command to reclaim memory, we can use PS, kill two commands to detect memory usage and recycle. Using the command "Ps" when using superuser privileges, it lists all running program names, and the corresponding process number (PID). The KILL command works by sending a process number (PID) of a system operation signal and program to the kernel of the Linux operating system.
application Example:
To efficiently reclaim memory, you can use the command PS parameter V:
PID TTY STAT time MAJFL TRS DRS RSS%mem COMMAND
2542 tty1 ss+ 0:00 0 8 1627 428 0.1/sbin/mingetty Tty1
2543 Tty2 ss+ 0:00 0 8 1631 428 0.1/sbin/mingetty Tty2
2547 tty3 ss+ 0:00 0 8 1631 432 0.1/sbin/mingetty tty3
2548 Tty4 ss+ 0:00 0 8 1627 428 0.1/sbin/mingetty Tty4
2574 tty5 ss+ 0:00 0 8 1631 432 0.1/sbin/mingetty tty5
2587 tty6 ss+ 0:00 0 8 1627 424 0.1/sbin/mingetty Tty6
2657 tty7 ss+ 1:18 1710 29981 7040 3.0/usr/bin/xorg:0-br-a
2670 PTS/2 Ss 0:01 2 682 6213 1496 0.6-bash
3008 PTS/4 Ss 0:00 2 682 6221 1472 0.6/bin/bash
3029 PTS/4 s+ 0:00 2 1783 548 Ping 0.2
3030 PTS/2 r+ 0:00 2 5134 768 0.3 PS V
Then, if you want to recycle the ping command's memory, use the command:
Using tool software
Memprof is a very attractive and easy to use software that was founded by Owen Talyor of Red Hat. This tool is a boehm-demers-weiser garbage collector for the Gnome front-end. The tool can be executed directly, and it works without any modification to the source code. When the program executes, the tool displays the memory usage in a graphical manner.