我的进程中所有这些未提交的保留内存是什么? [英] What's all this uncommitted, reserved memory in my process?

查看:117
本文介绍了我的进程中所有这些未提交的保留内存是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SysInternals的VMMap来查看WinXP上Win32 C ++进程分配的内存,我看到了一堆分配,其中部分分配的内存是保留的,但未提交.据我所知,通过阅读和测试,C ++程序中使用的所有常见内存分配器(例如,malloc,new,LocalAlloc,GlobalAlloc)始终分配完全提交的内存块. 堆是保留内存但直到需要时才提交的常见代码示例.我怀疑其中一些块是Windows/CRT堆,但是这些类型的块似乎比我期望的要多.我在进程中看到了30个这样的块,大小在64k到8MB之间,而且我知道我的代码从不故意调用VirtualAlloc来分配保留的,未提交的内存.

I'm using VMMap from SysInternals to look at memory allocated by my Win32 C++ process on WinXP, and I see a bunch of allocations where portions of the allocated memory are reserved but not committed. As far as I can tell, from my reading and testing, all of the common memory allocators (e.g., malloc, new, LocalAlloc, GlobalAlloc) used in a C++ program always allocate fully committed blocks of memory. Heaps are a common example of code that reserves memory but doesn't commit it until needed. I suspect that some of these blocks are Windows/CRT heaps, but there appears to be more of these types of blocks than I would expect for heaps. I see on the order of 30 of these blocks in my process, between 64k and 8MB in size, and I know that my code never intentionally calls VirtualAlloc to allocate reserved, uncommitted memory.

以下是VMMap中的几个示例: http://www.flickr. com/photos/95123032 @ N00/5280550393/

Here are a couple of examples from VMMap: http://www.flickr.com/photos/95123032@N00/5280550393/

还有什么会分配这样的内存块,其中大部分是保留的但没有提交?我的进程有30个堆有意义吗?谢谢.

What else would allocate such blocks of memory, where much of it is reserved but not committed? Would it make sense that my process has 30 heaps? Thanks.

推荐答案

我知道了-通过调用malloc分配的是CRT堆.如果您使用malloc分配大量内存(例如2 MB),则会分配一个已提交的内存块.但是,如果您分配较小的数据块(例如177kb),则它将保留1 MB的内存块,但仅提交您所请求的内容(例如,我的177kb请求为184kb).

I figured it out - it's the CRT heap that gets allocated by calls to malloc. If you allocate a large chunk of memory (e.g., 2 MB) using malloc, it allocates a single committed block of memory. But if you allocate smaller chunks (say 177kb), then it will reserve a 1 MB chunk of memory, but only commit approximately what you asked for (e.g., 184kb for my 177kb request).

当您释放那个小块时,那个更大的1 MB块不会返回到操作系统.除4k以外的所有内容都未提交,但整个1 MB仍保留.如果然后再次调用malloc,它将尝试使用该1 MB块来满足您的请求.如果使用已保留的内存无法满足您的请求,它将分配一个新的内存块,该内存块是先前分配的两倍(在我的情况下,它从1 MB变为2 MB).我不确定这种加倍模式是否继续.

When you free that small chunk, that larger 1 MB chunk is not returned to the OS. Everything but 4k is uncommitted, but the full 1 MB is still reserved. If you then call malloc again, it will attempt to use that 1 MB chunk to satisfy your request. If it can't satisfy your request with the memory that it's already reserved, it will allocate a new chunk of memory that's twice the previous allocation (in my case it went from 1 MB to 2 MB). I'm not sure if this pattern of doubling continues or not.

要真正将释放的内存返回给操作系统,可以调用_heapmin.我认为这将使将来的大型分配更有可能成功,但是这全都取决于内存碎片,而且如果分配失败(?),也许我已经调用过heapmin,我不确定.由于heapmin会释放内存(占用时间),然后malloc需要再次从操作系统重新分配内存时,也会对性能造成影响.此信息适用于Windows/32 XP,您的里程可能会有所不同.

To actually return your freed memory to the OS, you can call _heapmin. I would think that this would make a future large allocation more likely to succeed, but it would all depend on memory fragmentation, and perhaps heapmin already gets called if an allocation fails (?), I'm not sure. There would also be a performance hit since heapmin would release the memory (taking time) and malloc would then need to re-allocate it from the OS when needed again. This information is for Windows/32 XP, your mileage may vary.

更新:在我的测试中,heapmin完全不执行任何操作. malloc堆仅用于小于512kb的块.即使malloc堆中有连续可用MB的空间,它也不会将其用于超过512kb的请求.以我为例,释放,未使用但仍保留的malloc内存占用了进程2GB地址空间的大部分,最终导致内存分配失败.而且由于heapmin不会将内存返回给操作系统,所以除了重新启动进程或编写自己的内存管理器之外,我没有找到解决此问题的任何方法.

UPDATE: In my testing, heapmin did absolutely nothing. And the malloc heap is only used for blocks that are less than 512kb. Even if there are MBs of contiguous free space in the malloc heap, it will not use it for requests over 512kb. In my case, this freed, unused, yet reserved malloc memory chewed up huge parts of my process' 2GB address space, eventually leading to memory allocation failures. And since heapmin doesn't return the memory to the OS, I haven't found any solution to this problem, other than restarting my process or writing my own memory manager.

这篇关于我的进程中所有这些未提交的保留内存是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆