How to detect and estimate heap fragmentation in my C++ program?

前端 未结 6 469
鱼传尺愫
鱼传尺愫 2020-12-16 23:11

I\'m developing a VC++ NT service that is meant to operate continuously for many months. It uses VC++ runtime heap intensively. Clearly heap fragmentation can at some point

6条回答
  •  难免孤独
    2020-12-16 23:28

    The easiest way to detect fragmentation is to determine your largest allocation that your program will ever make and then to allocate at least twice that amount every now and again. if the allocation fails i.e. returns NULL AND your heap usage as determined by code - something like this on Windows

    PROCESS_MEMORY_COUNTERS counters;
    if(GetProcessMemoryInfo(process, &counters, sizeof(counters))){
        result = counters.WorkingSetSize;
    }   
    

    is less than some percentage of system memory usually 75% then you definitely have a fragmentation issue.

提交回复
热议问题