简体   繁体   English

SGI STL默认分配器是否存在内存泄漏?

[英]Does SGI STL default allocator has memory leak?

Two static members of __default_alloc_template have been taken for managing it's memory pool: __default_alloc_template的两个静态成员已用于管理其内存池:

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. 然后,它使用此堆的一部分来构建名为_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: SGI STL的默认分配器__default_alloc_template在下面的释放函数中释放其内存:

 /* __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. 当需要释放的内存块更大时,则是_MAX_BYTES(128个字节,“大块”),该函数将调用malloc_alloc :: deallocate(__ p,__n) ,该调用简单地调用c malloc函数以释放目标块,并为其提供目标块回到操作系统。 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. 其背后的理念是尽可能减少内存碎片,因为频繁请求和释放空间将导致大量内存碎片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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