Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage: Program allocates too much memory

I have coded a program in C++ for Ubuntu Server (64-Bit) which should run 24/7. The Server has 2GB RAM, but apparently my program is allocating too much memory.

This is the output of top after about 2 hours

top - 13:35:57 up  1:39,  1 user,  load average: 0.15, 0.13, 0.08
Tasks:  68 total,   2 running,  66 sleeping,   0 stopped,   0 zombie
%Cpu(s):  1.9 us,  5.7 sy,  0.0 ni, 92.3 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   2050048 total,   540852 used,  1509196 free,    34872 buffers
KiB Swap:  1509372 total,        0 used,  1509372 free.    93060 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
  902 root      20   0 1019896 364920   4492 S 13.1 17.8  13:07.03 Bether

How you can see my code already consumes 17.8% memory. At some point, the server will crash because it has no memory left.

My problem is that the program should not do that, but I can't find out where the memory gets allocated and not free'd anymore. Is there a tool, maybe even inside gdb, to find out where the program allocates the most memory?

Thanks in advance!

like image 332
Bobface Avatar asked Sep 07 '15 13:09

Bobface


1 Answers

Check out Valgrind, it should be in the Ubuntu repository. it can give you detailed information about memory usage in C++ programs. Kind of like a debugger for memory usage.

valgrind --tool=memcheck <your_app> <your_apps_params>

Also check out ccmalloc, NJAMD, LeakTracer

like image 191
nsa_a1 Avatar answered Sep 28 '22 05:09

nsa_a1