Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of reclaiming memory in Linux

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains the "Linux memory recovery method", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Linux memory recovery method" bar!

1. Definition of Linux memory monitoring memory leak:

Generally speaking, memory leaks refer to heap memory leaks. Heap memory refers to the arbitrary size allocated by the program from the heap (the size of the memory block can be determined at run time of the program), and the freed memory must be displayed after use. Applications usually use functions such as malloc,realloc,new to allocate a piece of memory from the heap, and after using it, the program must be responsible for calling free or delete to release the block of memory, otherwise, this piece of memory can not be used again, we will say that this piece of memory is leaked.

2. The harm of Linux memory monitoring memory leak

From the user's point of view of the program, the memory leak itself will not cause any harm, as ordinary users, do not feel the existence of memory leakage at all. The real danger is the accumulation of memory leaks, which will eventually deplete any memory in the system. From this point of view, one-time memory leaks are harmless because they do not accumulate, while implicit memory leaks are harmful because they are more difficult to detect than recurrent and sporadic memory leaks. Programs with memory leaks will not only take up more memory, but 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 usage.

3. Detection and recovery of Linux memory monitoring memory leak

Problems such as memory overflows may be encountered when writing complex programs with more pointers. Under Linux or unix, C and C++ languages are the most used tools. However, our C++ program lacks the corresponding means to detect memory information, so we can only use the top instruction to observe the total dynamic memory of the process. And when the program exits, we can't get any memory leak information.

Use the kill command

Using the Linux command to reclaim memory, we can use the Ps and Kill commands to detect memory usage and reclaim it. Use the command "Ps" when using the * user right, which lists the name of any running program and the corresponding process number (PID). The Kill command works by sending a system operation signal and program process number (PID) to the kernel of the Linux operating system.

Application examples:

To reclaim memory efficiently, you can use the command ps parameter v:

[root@www ~] # ps v

PID TTY STAT TIME MAJFL TRS DRS RSS MEM COMMAND

2542 tty1Ss+ 0:00 08 1627 428 0.1 / sbin/mingetty tty1

2543 tty2Ss+ 0:00 08 1631 428 0.1 / sbin/mingetty tty2

2547 tty3Ss+ 0:00 08 1631 432 0.1 / sbin/mingetty tty3

2548 tty4Ss+ 0:00 08 1627 428 0.1 / sbin/mingetty tty4

2574 tty5Ss+ 0:00 08 1631 432 0.1 / sbin/mingetty tty5

2587 tty6Ss+ 0:00 08 1627 424 0.1 / sbin/mingetty tty6

2657 tty7Ss+ 1 br 1812 1710 29981 7040 3.0 / usr/bin/Xorg: 0-br-a

2670 pts/2 Ss0:01 2 682 6213 1496 1496-bash

3008 pts/4 Ss0:00 2 682 6221 1472 0.6 / bin/bash

3029 pts/4 1783 1783 3029 ping 192.168.1.12

3030 pts/2 Rango pts/2 00 2 73 5134 768 0.3 ps v

Then if you want to reclaim the memory of the Ping command, use the command:

# Kill-9 3029

Thank you for your reading, the above is the content of "Linux memory recovery method", after the study of this article, I believe you have a deeper understanding of the memory recovery method of Linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report