Article Title: Linux memory monitoring Memory leakage and memory recovery methods. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
From the perspective of the user's program, the memory leakage itself will not cause any harm. As a general user, there is no sense of Memory leakage. The real danger is the accumulation of Memory leakage, which will eventually consume all the memory of the system. The following describes how to leak and recycle the Linux memory monitoring memory.
1. Linux memory monitoring Memory leakage definition:
Generally, memory leakage refers to heap memory leakage. Heap memory refers to the memory allocated by the program from the heap, which is of any size (the size of the memory block can be determined during the program runtime). The released memory must be displayed after use. Applications generally use functions such as malloc, realloc, and new to allocate a block of memory from the heap. after use, the program must call free or delete to release the block. Otherwise, this memory cannot be used again, so we can say this memory is leaked.
2. hazards of Linux memory monitoring Memory leakage
From the perspective of the user's program, the memory leakage itself will not cause any harm. As a general user, there is no sense of Memory leakage. The real danger is the accumulation of Memory leakage, which will eventually consume all the memory of the system. From this point of view, one-time memory leakage is not harmful, because it will not accumulate, while implicit memory leakage is harmful, because it is more difficult to detect than frequent and occasional memory leaks. In addition to occupying more memory, programs with Memory leakage can also cause a sharp decline in program performance. For the server, if this happens, even if the system does not crash, it will seriously affect the use.
3. Linux memory monitoring Memory leakage detection and recovery
You may encounter problems such as memory overflow when writing complex programs with many pointers. In Linux or unix, C and C ++ are the most commonly used tools. However, our C ++ program lacks proper means to detect memory information. Instead, we can only use the top command to observe the total amount of dynamic memory of the process. When the program exits, we cannot know any memory leakage information.
Use the kill command
Using Linux commands to recycle memory, we can use the Ps and Kill commands to detect memory usage and recycle memory. When you use the "Ps" command when using the ultimate user permission, it lists the names of any running programs and the corresponding process ID (PID ). The operating principle of the Kill command is: to send a system operation signal and program process number (PID) to the Linux operating system kernel ).
Example:
To efficiently recycle memory, use the ps parameter v:
[Root @ www ~] # Ps v
Pid tty stat time majfl trs drs rss % MEM COMMAND
2542 tty1Ss + 08 1627 428 0.1/sbin/mingetty tty1
2543 tty2Ss + 08 1631 428 0.1/sbin/mingetty tty2
2547 tty3Ss + 08 1631 432 0.1/sbin/mingetty tty3
2548 tty4Ss + 08 1627 428 0.1/sbin/mingetty tty4
2574 tty5Ss + 08 1631 432 0.1/sbin/mingetty tty5
2587 tty6Ss + 08 1627 424 0.1/sbin/mingetty tty6
2657 tty7Ss + 1: 1812 1710 29981 7040/usr/bin/Xorg: 0-br-
2670 pts/2 Ss0: 01 2 682 6213 1496 0.6-bash
3008 pts/4 Ss0: 00 2 682 6221 1472 0.6/bin/bash
3029 pts/4 S + 2 32 1783 548 0.2 ping 192.168.1.12
3030 pts/2 R + 2 73 5134 768 0.3 ps v
If you want to recycle the memory of the Ping command, run the following command:
# Kill-9 3029