0

I'm developing a C/C++ server application using docker and kubectl.
When I watch the server memory usage using 'docker stats' or 'kubectl top pod',
I found the 'MEM USAGE in docker' and 'MEMORY(bytes) in kubectl' does not check deleting heap memory.
In valgrind, it's not a memory leaks.

For example)

  1. is ok in 'docker stats' or 'kubectl top pod'.
int buf[100000];
  1. is ok in 'docker stats' or 'kubectl top pod'.
int* buf = new int[100000];
delete[] buf;
  1. it catch memory not decress in 'docker stats' or 'kubectl top pod'
int* buf = new int[100000]();
delete[] buf;

In 1) is in the stack memory, so there are no leaks.

In 2) the array was declared, but a memory leak did not occur because it was in state "Memory Reserved".

In 3) memory leak of 100000 * 4 bytes occurred because it was in state "Memory Reserved and Committed" by new int[100000]()

Deleting the heap memory does not seem to be reflected in the "docker stats" and "kubectl top pod" indicators.
If this problem persists, the server application will be killed by OOM killer event.

Should I change all dynamic allocation declarations to use stack memory?
or Does any method to avoid this situation? Please advise me.

Thanks.

  • I found it that 'docker stats' is same value of RSS. And RSS does not delete heap memory right away. but it is not over 77% memory usage. – Andrew Kwon Aug 05 '22 at 10:15
  • please do not invent terminology. "memory binding"? – Botje Aug 05 '22 at 11:05
  • I tagged your question as a duplicate because you seem to be under the impression that `delete[]` has *anything* to do with your process' memory usage. It does not. Asking the OS for memory is a relatively costly operation that your memory allocator would prefer not to. You may want to consider switching to jemalloc, which at least attempts to give back memory when it can. But no guarantees either way. – Botje Aug 05 '22 at 11:08
  • It was Reserved and Committed. I think this link https://stackoverflow.com/questions/31173374/why-does-a-jvm-report-more-committed-memory-than-the-linux-process-resident-set is more related to this issue. – Andrew Kwon Aug 05 '22 at 16:18

0 Answers0