In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to solve the memory leak of Python program, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
If you run a program under Linux or macOS that may cause a memory leak, you may see something like this:
And if you are using a Windows system, then the computer may simply be stuck.
However, debugging this OOM (Out of Memory) problem is sometimes very difficult because you don't know where the code can cause a memory leak. But if you run the program to debug, the program will be killed or directly stuck in the system.
If we have a way to see the amount of memory occupied by each function in the program, then we can narrow down the scope of the check.
To do this, we can install and use a third-party library called filprofiler, which can analyze the memory footprint of Python programs.
Let's install this library first:
Pip install filprofiler
Then write a piece of code that will cause a memory leak:
Def func_a (): print ('I am a normal function')
Def func_b (): print ('I am the second normal function')
Def func_b (): print ('I am the third normal function')
Def func_oom (): print ('I am a function that causes memory leaks') datas = [] while True: datas.append ('s'* 1024 * 1024) print ('you won't see this line when running the program')
Def run (): func_a () func_b () func_oom ()
Run ()
Running this program directly will be killed by the system directly because of the memory leak.
Before using filprofiler, you also need to adjust the size of the virtual memory; otherwise, the filprofiler itself will be killed by the system because it takes up too much memory.
First use the free command to see how much memory is available on the system:
The available memory of the system is 1619456 KB
We use the ulimit command to slightly lower the memory that the program can use, so that even if it is full, it will not be killed by the system:
Ulimit-Sv 1600000
Then, use filprofiler to run the program:
Fil-profile run test.py
The running effect is shown in the following figure:
Filprofiler will generate a fil-result folder under the current folder, with a folder named after time, and two svg files in the folder, as shown in the following figure:
When we open the out-of-memory.svg file in a browser, we can see the memory footprint shown in the following figure:
As you can see from the figure, the largest memory-consuming function is func_oom, where the program crashes.
The above content is how to solve the memory leak of Python program. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.