Linux内存管理机制控制--mallopt,避免大量小内存不回收问题

来源:互联网 发布:人类登月是真是假 知乎 编辑:程序博客网 时间:2024/06/08 00:23

http://blog.csdn.net/wscdylzjy/article/details/44244413

一.http://blog.sina.com.cn/s/blog_4673e6030101haxg.html

最近使用ACE的Message_Block时发现,程序运行一段时间之后内存越吃越多,即便没有请求,内存也不会下降。


在使用 valgrind 排除内存泄漏之后,把怀疑的对象转到了Message_Block上。

用ACE的测试用例改了一个测试程序:

#include "ace/Log_Msg.h"
#include "ace/Message_Block.h"
#include
#include
#include
#include


using namespace std;

#define MY_DEBUG(FMT, ...)  \
        ACE_DEBUG((LM_DEBUG, FMT, __VA_ARGS__))
void foo (void);


void create_packet(ACE_Message_Block** packet, int cnt, int packet_size)
{
  for (int i = 0; i < cnt; i++) {
    packet[i] = new ACE_Message_Block(packet_size);
  }

  cout << "Create " << cnt << "packets" << endl;
}

void delete_packet(ACE_Message_Block** packet, int cnt)
{
  for (int i = 0; i < cnt; i++) {
    delete packet[i];
  }

  cout << "Delete " << cnt << "packets" << endl;
}

void hang()
{
  while (true) {
    ACE_OS::sleep(1);
  }
}


int ACE_TMAIN (int, ACE_TCHAR *[])
{
  int cnt;
  int packet_size;

  cout<< "packet size = ";
  cin>>packet_size;
  cout<< "\n packet number = ";
  cin>>cnt;

  ACE_Message_Block **packet = new ACE_Message_Block*[cnt];
  create_packet(packet, cnt, packet_size);
  delete_packet(packet, cnt);
  malloc_trim(0);
  create_packet(packet, cnt, packet_size);
  delete_packet(packet, cnt);
  malloc_trim(0);
  create_packet(packet, cnt, packet_size);
  delete_packet(packet, cnt);
  hang();
  return 0;
}

测试时,每个block 1k,测了10万个block,结果运行到hang时,该程序的内存一直维持在最高位。

上网google了一下,发现是linux平台下 mallopt 的内存管理机制导致的。

当程序对malloc出来的内存执行 free时,它只是标识这块内存被释放了。

       void free(void *ptr) 
    {
            struct mem_control_block *free;
            free ptr sizeof(struct mem_control_block);
            free->is_available 1;
            return;
    }

至于是不是真的把这块内存返还内核就不一定了。
通过man mallopt 可以得知: 如果被free的内存太小了 (小于M_TRIM_THRESHOLD),那么glibc 出于性能的考虑会把这块内存保留在当前进程堆中,以满足该进程之后malloc的需求。

所以当进程从堆中申请了海量小内存时,就会出现该程序的内存始终都是只增不减。

解决办法:
1. 使用内存池,程序改动较大,但对程序性能和管理都是很有益
2. 显示调用 malloc_trim(0) 来强制回收被释放的堆内存。
3. 调小M_TRIM_THRESHOLD ()

这里使用的方法2,上例的程序修改如下:

#include "ace/Log_Msg.h"
#include "ace/Message_Block.h"
#include
#include
#include
#include
#include


using namespace std;

#define MY_DEBUG(FMT, ...)  \
        ACE_DEBUG((LM_DEBUG, FMT, __VA_ARGS__))
void foo (void);


void create_packet(ACE_Message_Block** packet, int cnt, int packet_size)
{
  for (int i = 0; i < cnt; i++) {
    packet[i] = new ACE_Message_Block(packet_size);
  }

  cout << "Create " << cnt << "packets" << endl;
}

void delete_packet(ACE_Message_Block** packet, int cnt)
{
  for (int i = 0; i < cnt; i++) {
    delete packet[i];
  }

  cout << "Delete " << cnt << "packets" << endl;
}
void hang()
{
  while (true) {
    ACE_OS::sleep(1);
  }
}

class worker : public ACE_Task
{
public:
  int svc()
  {
    ACE_OS::sleep(5);
    malloc_trim(0);
    return 0;
  }
}
;


int ACE_TMAIN (int, ACE_TCHAR *[])
{
  int cnt;
  int packet_size;

  cout<< "packet size = ";
  cin>>packet_size;
  cout<< "\n packet number = ";
  cin>>cnt;

/ mallopt(M_TRIM_THRESHOLD, 10240);
  ACE_Message_Block **packet = new ACE_Message_Block*[cnt];
  create_packet(packet, cnt, packet_size);
  delete_packet(packet, cnt);
  malloc_trim(0);
  create_packet(packet, cnt, packet_size);
  delete_packet(packet, cnt);
  malloc_trim(0);
  create_packet(packet, cnt, packet_size);
delete_packet(packet, cnt);
  worker *w = new worker();
  w->activate();
  hang()
;

  ACE_TRACE("main");

  ACE_DEBUG ((LM_INFO, ACE_TEXT ("%IHi Mom\n")));
  foo();
  ACE_DEBUG ((LM_INFO, ACE_TEXT ("%IGoodnight\n")));

  return 0;
}

由于alloc_trim操作过多会影响性能,所以建议在实际程序中另启一个后台线程,周期性地执行。
上面的实例也另起一个线程主要是为了验证另起的线程调用malloc_trim也能回收其他线程的空闲内存。

注1: mem_control_block 是从堆中分配的内存的描述信息
   struct mem_control_block {
    int is_available;    //这是一个标记?
    int size;            //这是实际空间的大小

    };

二.http://my.huhoo.NET/archives/2010/05/malloptmallocnew.html

同事在项目中使用new/delete的时候发现一个奇怪的现象:
int32_t i;
std::queue<char *> qTest;
for (i = 0; i < 100000; i ++) {
    char *p = new char[100];
    qTest.push(p);
    char *p1 = qTest.front();
    delete[] p1;
    qTest.pop();
}

当在一个循环内,如果申请空间,在循环内并释放掉,内存不会引起增长,即使重复上面的单元也不会增长内存,但是当:
int32_t i;
std::queue<char *> qTest;
for (i = 0; i < 100000; i ++) {
    char *p = new char[100];
    qTest.push(p);
    //char *p1 = qTest.front();
    //delete[] p1;
    //qTest.pop();
}
while( !qTest.empty() ) {
    char *p1 = qTest.front();
    delete[] p1;
    qTest.pop();
}
而发现通过delete[]后,内存并没有减少,即使重复上面的单元,内存只会增加,不会减少,用valgrind的memcheck工具查看,也无内存泄漏。
其实,这个与malloc的实现有关,一般来说,系统都会有默认的malloc行为,一般来说(针对FreeBSD和Linux系列),对于大于1M的数 据,malloc行为会直接调用系统的接口,直接向操作系统申请一块比数据块更大内存,然后划分成若干过chunk单元(这里会有一些算法设计),而这些 chunk分配给数据后,肯定还会剩下很多的trunk单元,留给以后的分配用,但这里耗费系统资源,代价较大;而对于小于等于1M的数据,则 malloc行为会利用那些trunk单元,占用系统资源少,速度快。同理free的时候,数据占用的内存被释放,如果大于1M,则系统会回收掉内存,节 省资源,而小于等于1M的数据,则内存释放时,不会还给操作系统,以空trunk形式存在,并由当前进程空间维护着。说到底,这种策略就是一个内存池的概 念。
内存池灵活度更加方便,一般来说(针对FreeBSD和Linux系列),每个trunk最小容纳的字节数是16bytes,而自行设计的内存池,可以更 加优化,节省大量产生的内存碎片,像上面的代码就是因为内存虽然释放,但是这些内存都没有还给操作系统,导致内存碎片越来越多,最好的设计方式就是使用一 个内存池,针对经常使用的分配大小,可以多分配这样的trunk,而其他大小的trunk可以少产生,从而达到优化目的。
另外,也可以使用mallopt来直接调整malloc的行为:
int mallopt (int PARAM, int VALUE)
     When calling `mallopt', the PARAM argument specifies the parameter
     to be set, and VALUE the new value to be set.  Possible choices
     for PARAM, as defined in `malloc.h', are:

    `M_TRIM_THRESHOLD'
          This is the minimum size (in bytes) of the top-most,
          releasable chunk that will cause `sbrk' to be called with a
          negative argument in order to return memory to the system.

    `M_TOP_PAD'
          This parameter determines the amount of extra memory to
          obtain from the system when a call to `sbrk' is required.  It
          also specifies the number of bytes to retain when shrinking
          the heap by calling `sbrk' with a negative argument.  This
          provides the necessary hysteresis in heap size such that
          excessive amounts of system calls can be avoided.

    `M_MMAP_THRESHOLD'
          All chunks larger than this value are allocated outside the
          normal heap, using the `mmap' system call.  This way it is
          guaranteed that the memory for these chunks can be returned
          to the system on `free'.  Note that requests smaller than
          this threshold might still be allocated via `mmap'.

    `M_MMAP_MAX'
          The maximum number of chunks to allocate with `mmap'.
          Setting this to zero disables all use of `mmap'.
值得注意的是mallopt是malloc底层的函数,需要使用info mallopt来查看相关帮助信息。

0 0
原创粉丝点击