Visual Studio 2010 c++: malloc()/HeapAlloc总是为最小的alloc分配新页面

时间:2022-05-01 03:17:26

I have a rather large C/C++ project, and I have been trying to track down why it consumes an inordinate amount of memory (judged by the "Working Set" in the Task Manager). I finally tracked it down to the bizarre behavior that, even for the smallest malloc() requests, it will allocate a full new 4k page. Code like this

我有一个相当大的C/ c++项目,我一直在试图找出为什么它会消耗过多的内存(根据任务管理器中的“工作集”判断)。我最终找到了这种奇怪的行为,即使是最小的malloc()请求,它也会分配一个全新的4k页面。这样的代码

    for(int bla = 0; bla < 1000; bla++)
    {
        char* blu = (char*)malloc(10);
    }

which should increase memory consumption by a measly 10KB, ends up racking it up by 4MB, since it does 1000 4kB allocations.

它应该会增加少量10KB的内存消耗,最终会增加4MB,因为它执行了1000 4kB的分配。

The really frustrating part is that I can't reproduce it as standalone. A small app just with the above code works fine. Only the big project exhibits the wrong behavior. To answer some obvious suggestions right upfront:

真正令人沮丧的是我不能独立地复制它。一个小的应用程序只需使用上面的代码就可以了。只有大项目表现出错误的行为。直接回答一些明显的建议:

  • I am pulling in the same libraries as the big project, and also made sure the compilation flags are the same

    我正在与大项目拉入相同的库,并确保编译标志是相同的。

  • "new" behaves the same way

    “new”的行为也是一样的

  • It happens both in Debug and Release mode

    它同时发生在调试和发布模式中

  • I really tracked it down to the HeapAlloc call which is the culprit. Sadly one can't step into HeapAlloc to investigate further.

    我真的找到了HeapAlloc call,它是罪魁祸首。遗憾的是,我们无法深入研究。

Any ideas? I am totally stumped.

什么好主意吗?我完全一头雾水。

1 个解决方案

#1


3  

Windows includes a feature called "Page Heap" which is helpful in locating memory corruption defects. It operates by placing each allocation on a page, which causes the processor to issue an access violation if the program corrupts memory instead of entering undefined behavior territory.

Windows包含一个名为“页面堆”的特性,有助于查找内存损坏缺陷。它通过将每个分配放在页面上进行操作,这将导致处理器在程序损坏内存而不是进入未定义的行为区域时发出访问违规。

Sounds like somewhere in your big application someone has turned on page heap, or you have triggered some kind of application compatibility setting in Windows in your big application that enabled Page Heap.

听起来像是在你的大应用程序中有人打开了页面堆,或者你在你的大应用程序中在Windows中触发了某种应用程序兼容性设置,从而启用了页面堆。

Note that this is a Windows setting (HeapAlloc is a Win32 API), not a debug/release compiler setting.

注意,这是一个Windows设置(HeapAlloc是一个Win32 API),而不是一个调试/发布编译器设置。

#1


3  

Windows includes a feature called "Page Heap" which is helpful in locating memory corruption defects. It operates by placing each allocation on a page, which causes the processor to issue an access violation if the program corrupts memory instead of entering undefined behavior territory.

Windows包含一个名为“页面堆”的特性,有助于查找内存损坏缺陷。它通过将每个分配放在页面上进行操作,这将导致处理器在程序损坏内存而不是进入未定义的行为区域时发出访问违规。

Sounds like somewhere in your big application someone has turned on page heap, or you have triggered some kind of application compatibility setting in Windows in your big application that enabled Page Heap.

听起来像是在你的大应用程序中有人打开了页面堆,或者你在你的大应用程序中在Windows中触发了某种应用程序兼容性设置,从而启用了页面堆。

Note that this is a Windows setting (HeapAlloc is a Win32 API), not a debug/release compiler setting.

注意,这是一个Windows设置(HeapAlloc是一个Win32 API),而不是一个调试/发布编译器设置。