简体   繁体   English

如何避免意外的内存分配

[英]How to avoid unexpected memory allocation

I just write a small MemoryHook library with c++ and use as preload.But when I run the test every time,there is a large amount of memory allocated,the size is 72704. The usage is like this: LD_PRELOAD=./libPreload.so ./XXX 我只是用c ++编写了一个小的MemoryHook库,并用作预加载。但是,每次运行测试时,分配了大量的内存,大小为72704。用法如下:LD_PRELOAD =。/ libPreload.so ./XXX

I try to find all related library in ldd list, but no size is 72704.I removed other information and only kept the size, but also has this problem. 我尝试在ldd列表中找到所有相关的库,但没有大小为72704。我删除了其他信息,仅保留了大小,但是也有此问题。

My initialize code: 我的初始化代码:

    static void TraceInitialize()
    {
#ifdef _DEBUG
        fprintf( stderr, "call TraceInitialize\n" );      
#endif
        pthread_mutex_lock( &s_mutexInit );
        if ( s_status == TS_INITIALIZED ) { pthread_mutex_unlock( &s_mutexInit ); return; }

        s_status = TS_INITIALIZING;

        MemoryManager::initialize();

        s_pRealMalloc       = (FUNC_MALLOC)dlsym(RTLD_NEXT, "malloc");
        s_pRealCalloc       = (FUNC_CALLOC)dlsym(RTLD_NEXT, "calloc");
        s_pRealRealloc      = (FUNC_REALLOC)dlsym(RTLD_NEXT, "realloc");
        s_pRealMemalign     = (FUNC_MEMALIGN)dlsym(RTLD_NEXT, "memalign");
        s_pRealValloc       = (FUNC_VALLOC)dlsym(RTLD_NEXT, "valloc");
        s_pRealFree         = (FUNC_FREE)dlsym(RTLD_NEXT, "free");

        assert( !( NULL == s_pRealMalloc || NULL == s_pRealCalloc || NULL == s_pRealRealloc || 
                    NULL == s_pRealMemalign || NULL == s_pRealValloc || NULL == s_pRealFree ) );

        s_status = TS_INITIALIZED;

       // printMap();

        pthread_mutex_unlock( &s_mutexInit );
    }

and trace malloc call: 并跟踪malloc调用:

 static int s_no_hook = 0;
 void* TraceMalloc( size_t size )
 {  
      if ( s_status == TS_INITIALIZING ) return mockMemory::_mockMalloc( size );

      if ( s_status != TS_INITIALIZED )  TraceInitialize();

      void* p = _impMalloc( size, __sync_fetch_and_add( &s_no_hook, 1 ) );
      __sync_fetch_and_sub( &s_no_hook, 1 );
      return p;
  }

When I test a demo problem just has empty main function.I expect not memory unfree but the real output is: 当我测试一个演示问题时,它的主要功能为空。我期望内存不会释放,但是实际输出是:

++++++++++++++ unfreed addr: 0x55cc4ae22260, size: 72704, serial: 1 ++++++++++++++
backtrace:
./libPreLoad.so(_ZN11MemoryTrace13MemoryManager14storeBacktraceEPNS0_11tagUnitNodeE+0x28)[0x7f62fd6cceba]
./libPreLoad.so(_ZN11MemoryTrace13MemoryManager10appendUnitEPvmb+0x9f)[0x7f62fd6ccbb7]
./libPreLoad.so(_ZN11MemoryTrace10_impMallocEmb+0x52)[0x7f62fd6cd4cb]
./libPreLoad.so(_ZN11MemoryTrace11TraceMallocEm+0x58)[0x7f62fd6cd286]
./libPreLoad.so(malloc+0x18)[0x7f62fd6cc812]
/usr/lib/x86_64-linux-gnu/libstdc++.so.6(+0x8f416)[0x7f62fd1cd416]
/lib64/ld-linux-x86-64.so.2(+0x10733)[0x7f62fd9e0733]
/lib64/ld-linux-x86-64.so.2(+0x10ca)[0x7f62fd9d10ca]
++++++++++++++ end ++++++++++++++

With no backtrace, the output is: 没有回溯,输出为:

++++++++++++++ unfreed addr: 0x55f799c8e260, size: 72704, serial: 0 ++++++++++++++
backtrace:
++++++++++++++ end ++++++++++++++

And there is an additional problem that cannot count the memory release of static global variables.I can't find a suitable time to do this,even with attribute ((destructor(101))). 还有一个额外的问题无法计算静态全局变量的内存释放。即使属性 ((destructor(101)))我也找不到合适的时间来执行。

The big allocation which you are seeing is the C++ STL . 您看到的最大分配是C++ STL As asked here by rerumu and answered by Nikos C. 正如rerumu 在这里并由Nikos C 回答

The heap usage comes from the C++ standard library. 堆用法来自C ++标准库。 It allocates memory for internal library use on startup. 它在启动时分配内存供内部库使用。 If you don't link against it, there should be zero difference between the C and C++ version. 如果您不反对它,则C和C ++版本之间的差应该为零。 With GCC and Clang, you can compile the file with: 使用GCC和Clang,可以使用以下命令编译文件:

 g++ -Wl,--as-needed main.cpp g ++ -Wl,-根据需要main.cpp 

This will instruct the linker to not link against unused libraries. 这将指示链接器不要链接未使用的库。 In your example code, the C++ library is not used, so it should not link against the C++ standard library. 在您的示例代码中,未使用C ++库,因此它不应链接到C ++标准库。

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

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