简体   繁体   中英

Does SGI STL default allocator has memory leak?

Two static members of __default_alloc_template have been taken for managing it's memory pool:

static char* _S_start_free;
static _Obj* __STL_VOLATILE _S_free_list[_NFREELISTS];

The allocator query heap space from operate system like follows:

_S_start_free = (char*)malloc(__n);

Then it use a part of this heap building a free memory list named _S_free_list.

But I can't find any code resbonsible for giving the memory back to operate system like:

free(_S_start_free);

I am confused.

  • It depend on system's cleaning?
  • Or Somewhere else has code for cleaning?

Help me.

the answer for the first question is NO!
The default allocator __default_alloc_template of SGI STL frees its memory in it's deallocate function bellow:

 /* __p may not be 0 */
static void deallocate(void* __p, size_t __n) {
    if (__n > (size_t) _MAX_BYTES)
        malloc_alloc::deallocate(__p, __n);
    else {
        _Obj* __STL_VOLATILE*  __my_free_list
            = _S_free_list + _S_freelist_index(__n);
        _Obj* __q = (_Obj*)__p;

        // acquire lock
#       ifndef _NOTHREADS
        /*REFERENCED*/
        _Lock __lock_instance;
#       endif /* _NOTHREADS */
        __q -> _M_free_list_link = *__my_free_list;
        *__my_free_list = __q;
        // lock is released here
    }
}

When memory blocks needed to be released is lager then _MAX_BYTES(128 bytes, "the big block"), the function will call malloc_alloc::deallocate(__p, __n) which simply calls the c malloc function to free the target blocks, giving them back to OS. Otherwise, instead of giving the memory back, for those small blocks, the function will put them back to the memory pool.

The philosophy behind this is reducing memory fragments as much as possible, since requesting and freeing space frequently will cause lots of memory fragments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM