0

Possible Duplicate:
Problem usage memory in C

I have problem: operator "delete" does not return allocated memory.

Peace of code to illustrate how I use "delete" operator. Сertainly, before this code called all pointers created with operator "new" at other parts of the program:

std::multimap<std::string, Container*>::iterator it;
for ( it = MAPX.begin() ; it != MAPX.end(); )
{
    Container* ptr_container = it->second;

    // delete every object at the container
    std::multimap<unsigned int, TMB*>::iterator it_tmb;
    for ( it_tmb = ptr_container->map_thumbs.begin() ; it_tmb != ptr_container->map_thumbs.end(); )
    {
        TMB* ptr_currentGlobalThumb = it_tmb->second;

        ptr_container->map_thumbs.erase( it_tmb++ ); // erase from map      
        delete ptr_currentGlobalThumb; // delete at the memmory
    }


    delete ptr_container; // lets delete empty container now
    MAPX.erase( it++ ); // and erase it from map
}

Before I start this peace of code I look at my top and ps aux output at FreeBSD and see that program use 241M of memory:

ps: abrahab 21351  3.5  7.3 263108 246932  ??  S     3:19PM   1:50.94 program
top: 21351 abrahab 8  -8    0   257M   241M biord   2   0:47  8.30% program

After cleaning process nothing changed! As program use 241M of RAM, so program continued to use memory (not deallocated). Why?

After restart of program, I see that memory returned for system (of course):

abrahab 71595  0.0  5.0 185284 170360  ??  I     4:50PM   0:18.62 program 

EDIT: 1) So how can I control real memory usage of my program when its running? 2) Seems that program crash(end without the .core file) when its begin to use ~1Gb of memory on 32bit FreeBSD - is it possible that FreeBSD "end" program that use too much memory?

Community
  • 1
  • 1
abrahab
  • 2,292
  • 8
  • 33
  • 57
  • 6
    You have made an assumption that does not hold. There is no reason to expect that all the process's virtual memory will be returned to the OS on a byte-by-byte basis. This would be quite inefficient and almost never happens. – Lightness Races in Orbit Dec 09 '12 at 18:04
  • Lightness nailed it. `new` and `delete` work on the process heap, which is allocated, and expanded by the OS when the library requests it. – Jonathon Reinhart Dec 09 '12 at 18:06
  • So, how can I control my program memory usage when its running? – abrahab Dec 09 '12 at 18:06
  • 1
    Why do you think it's a problem? – JasonD Dec 09 '12 at 18:08
  • 1
    There's no problem here. Let the OS do its job. – Lightness Races in Orbit Dec 09 '12 at 18:10
  • I can not control the program. I think program falls because it uses too much memory. Also I can not understand "delete" (deallocation) work or not?.. – abrahab Dec 09 '12 at 18:12
  • 1
    Use valgrind to check if you have memory leaks, and for the rest let the allocator do its job (unless you experience specific performance/memory usage problems). – Matteo Italia Dec 09 '12 at 18:14
  • @MatteoItalia I would be happy to use valgrind, but I can not: http://stackoverflow.com/questions/10861384/cpp-gdb-valgrind-memory-usage-statistic-while-app-is-running-and-valgrind-unha – abrahab Dec 09 '12 at 18:16

0 Answers0