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

How to solve the memory leak of Linux kernel

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article mainly explains "how to solve the Linux kernel memory leak". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the Linux kernel memory leak".

What is a memory leak:

The program applies for memory from the system, and after using it, it does not release the memory and returns it to the system for recycling, resulting in a waste of applied memory.

It is found that the memory usage in the system is consuming more and more over time, as shown in the following figure:

The next train of thought for investigation is:

1. Monitor the PSS consumed by each user process in the system (using the pmap tool (pmap pid)).

PSS: proportionally reported physical memory, such as process An occupies 20m of physical memory, process B and process A share 5m of physical memory, then the PSS of process An is (20-5) + 5max 2 = 17.5m

two。 Monitor / proc/meminfo output, focusing on Slab usage and / proc/slabinfo information corresponding to slab

3. Reference / proc/meminfo output to calculate uncounted memory changes in the system, such as kernel driver code

The memory taken from buddy by calling alloc_page () directly will not be counted separately.

The above troubleshooting ideas correspond to the following figure: 1 and 2, respectively:

In the process of investigation, it was found that the system was very idle and did not run any user business processes.

When using slabtop to monitor the use of slab, it is found that size-4096 keeps growing.

Through monitoring / proc/slabinfo, it is also found that the use of SReclaimable continues to grow.

While true; do sleep 1; cat / proc/slabinfo > > / tmp/slabinfo.txt; echo "= =" > / tmp/slabinfo.txt; done

As a result, it is likely that the kernel space has a memory leak when using size-4096.

Next, use the trace event (tracepoint) function to monitor the use and release of size-4096

It is mainly used to track the trace event corresponding to the kmalloc () and kfree () functions, because when their trace event is triggered, the memory addresses requested and freed by kmalloc () and kfree () are printed, and then only 4096 bytes are further filtered.

# trace-cmd record-e kmalloc-f 'bytes_alloc==4096'-e kfree-T

(- T print stack)

After waiting a few minutes...

# cp / sys/kernel/debug/tracing/trace_pipe / tmp/kmalloc-trace

# trace-cmd report

The above steps are equivalent to:

After waiting a few minutes...

# cp / sys/kernel/debug/tracing/trace_pipe / tmp/kmalloc-trace

Judging from the output results of trace-cmd report, many kmalloc corresponding PTR values do not have the corresponding PTR values of kfree.

This shows that the cat process is not released after using size-4096 in kernel space, resulting in a memory leak.

To further pinpoint which kernel function is used to cause the problem, vmcore is triggered manually at this point

# echo c > / proc/sysrq-trigger

Then use the crash tool to analyze vmcore:

# crash. / vmcore. / vmlinux.debug

Read out the ptr memory information requested by kmalloc above

(read the first 4096 bytes of 0xffff880423744000 memory and display it in characters)

It is found that the contents read from the above ptr memory are very similar. Take a closer look and find that they are all the output of / proc/schedstat.

By reading the relevant code, it is found that when the / proc/schedstat content is read, the memory is really not released.

Then it is found that there is already a patch upstream of kernel to solve this problem:

Commit: 8e0bcc722289

Fix a leak in / proc/schedstats

Thank you for reading, the above is the content of "how to solve the Linux kernel memory leak". After the study of this article, I believe you have a deeper understanding of how to solve the problem of Linux kernel memory leak, 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