Home  >  Article  >  Backend Development  >  Reasons for golang memory leaks

Reasons for golang memory leaks

(*-*)浩
(*-*)浩Original
2019-12-27 10:47:104109browse

Reasons for golang memory leaks

#1. Putting pressure on the system will increase the memory usage, but if the memory cannot be reduced after stopping the pressure, there may be a memory leak. (Recommended learning: Go )

## 2, TOP cannot reflect the procedure to occupy memory in real time, because GO does not return the system immediately after applying for memory from the system.

3. The program occupies system memory, Go's heap memory, and actual used memory: the memory requested from the system will be managed in Go's memory pool, and the entire memory page will not be accessed for a long time. It is returned to the operating system only after certain conditions are met. And because of the GC, heap memory does not represent memory usage. What is left after cleaning is the actual memory used.

There are two ways to find memory leaks in Go, one is a general monitoring tool, and the other is go pprof:

Monitoring tool: fixed periodic monitoring of the process The memory usage is sampled. After the data is visualized, it is easy to find whether a memory leak occurs based on the memory usage trend (continuous increase).

go pprof: Suitable for situations where there is no monitoring tool. Use the pprof tool provided by Go to determine whether a memory leak occurs.

Monitoring tools to check the internal usage of the process

If you use the cloud platform to deploy Go programs, the cloud platform provides memory viewing tools to check the memory usage of the OS. And the memory usage of a certain process. For example, in Alibaba Cloud, we only deploy one Go service on one cloud host, so the memory usage of the OS basically reflects the memory usage of the process. The OS memory usage is as follows , you can see that as time goes by, the memory usage continues to increase, which is the most obvious phenomenon of memory leaks:

Reasons for golang memory leaks

The above is the detailed content of Reasons for golang memory leaks. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn