c++ - 如何避免意外的内存分配

标签 c++ memory

我只是用c++写了一个小的MemoryHook库,作为preload使用。但是每次运行测试的时候,都分配了大量的内存,大小是72704。 用法是这样的: LD_PRELOAD=./libPreload.so ./XXX

我试图在ldd列表中找到所有相关的库,但没有大小是72704。我删除了其他信息,只保留了大小,但也有这个问题。

我的初始化代码:

    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 );
    }

并跟踪 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;
  }

当我测试一个 demo 问题时,只有一个空的 main 函数。我希望内存不会释放,但实际输出是:

++++++++++++++ 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 ++++++++++++++

没有回溯,输出是:

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

还有一个额外的问题是无法统计静态全局变量的内存释放。我找不到合适的时间来做这个,即使有attribute ((destructor(101))) .

最佳答案

您看到的大分配是C++ STL。正如所问here通过 rerumuanswered通过 Nikos C.

The heap usage comes from the C++ standard library. 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. With GCC and Clang, you can compile the file with:

 g++ -Wl,--as-needed 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++ - 如何避免意外的内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56786196/

相关文章:

c++ - ALSA:以共享模式打开PCM设备

c++ - 设置 std::ostream 精度的最小小数位数

c - 为从函数返回的变量释放动态分配的内存

c++ - 全关联缓存

c++ - 在多维数组中对 .csv 进行排序

c++ - 使用访问者模式检查派生类类型?

java - C++ 等效的 java.util.concurrent.ArrayBlockingQueue

java - bufferedwriter close() 和flush() 方法的内存效应是什么?

linux - do_mmap_pgoff 用于其他进程

java - 我能知道 JBoss 在运行时使用了多少内存吗?