dongrixinyu
dongrixinyu

Reputation: 320

memory leak in docker container will disappear after the container is been killed?

I am writing and testing a cpp program in a docker container. And I did not designate the max memory size for the container.

docker run -it xxx:latest /bin/bash

And cpp program will sometimes cause memory leak such as not free the allocated heap memory.

So I am curious that if the memory leak will disappear in the host linux when I kill the container?

Or this memory leak caused by the program in the container still exists in the host?

Upvotes: 0

Views: 1028

Answers (1)

David Maze
David Maze

Reputation: 159998

A Docker container is a wrapper around a single process. Killing the container also kills that process; conversely, if the process exits on its own, that causes the container to exit too.

Ending a process will release all of the memory that process used. So, if you have a C++ program, and it calls new without a corresponding delete, it will leak memory, but ending the process will reclaim all of the process's memory, even space the application has lost track of. This same rule still applies if the process is running in a container.

This also applies to other leak-like behavior and in other languages; for example, appending a very large number of values to a list and then ignoring them, so they're still "in use" unnecessarily. Some other OS resources like file descriptors are cleaned up this way, but some are not. In particular, if you fork(2) a subprocess, you must clean up after it yourself. Similarly, if you have access to the Docker API, you must clean up any related containers you spawn yourself.

Upvotes: 2

Related Questions