只有当虚拟限制被命中时,内存才会耗尽?

时间:2021-12-17 16:52:09

As I know in win32 every program receives say 4GB of virtual memory. Memory manager is responsible for offloading chunks of memory from physical memory to disk.

正如我所知,在win32中,每个程序都会接收到4GB的虚拟内存。内存管理器负责将内存块从物理内存转移到磁盘。

Does it imply that malloc or any other memory allocation API will throw OUT_OF_MEMORY exception only when virtual limit is hit? I mean is it possible for malloc to fail even if program is far from its virtual size limit e.g. none of physical memory can be offloaded to disk. Assume disk have unlimited capacity and no specific limitation is set.

这是否意味着malloc或任何其他内存分配API仅在达到虚拟限制时才抛出OUT_OF_MEMORY异常?我的意思是,malloc是否有可能失败,即使程序离其虚拟大小的限制很远,例如,没有任何物理内存可以卸载到磁盘上。假定磁盘具有无限的容量,没有设置特定的限制。

4 个解决方案

#1


11  

Yes, it's possible. Remember that memory can be fragmented and that malloc won't be able to find a sufficiently large chunk to serve the size you requested. This can easily be way before you hit your 4 GiB limit.

是的,这是可能的。记住,内存可以是分段的,malloc不能找到足够大的块来满足您的要求。这很容易在你达到你的4尺极限之前。

#2


6  

The virtual memory limit on Win 32 is 2Gb. On Win 64, it's much bigger.

win32的虚拟内存限制是2Gb。在win64上,它要大得多。

malloc doesn't throw an exception - it returns NULL. NULL return, or exception, the memory manager can fail well before the 2Gb limit is reached if

malloc不会抛出异常——它返回NULL。如果达到2Gb的限制,那么内存管理器可能会在很长一段时间内失败

  • The paging file isn't big enough. If the paging file is limited either by policy, or by lack of room to expand: If memory allocations can't be met by page file availability then they will fail.

    分页文件不够大。如果分页文件受到策略或扩展空间不足的限制:如果页文件可用性无法满足内存分配,那么它们将失败。

  • Fragmentation. The underlying memory manager allocates memory in 4Kb chunks. Its quite possible, through patterns of allocations and deallocations to end up with a small amount of allocated memory, but a fragmented virtual memory meaning that there is no contiguous area large enough to meet a particular request.

    碎片。底层内存管理器将内存分配到4Kb的块中。很有可能,通过分配和释放模式,最终获得少量的分配内存,但是碎片化的虚拟内存意味着没有足够大的连续区域来满足特定的请求。

#3


1  

For full chapter and verse on Windows virtual memory check out this post on Mark Russinovich's Blog (lots of other great stuff here too):

有关Windows虚拟内存的完整章节,请查看Mark Russinovich博客上的这篇文章(这里还有很多其他很棒的东西):

Pushing the Limits of Windows: Virtual Memory

突破Windows的极限:虚拟内存

If memory fragmentation is your problem and writing custom allocators isn't your thing you could consider enabling the low fragmentation heap:

如果内存碎片是你的问题,编写自定义分配器不是你的事,你可以考虑启用低碎片堆:

Low Fragmentation Heap (Windows)

低碎片堆(Windows)

This is on by default these days mind you.

请注意,这是默认设置。

#4


0  

how about allocating a few smaller areas, if a huge one isn't available?

如果一个巨大的区域无法提供,那么如何分配一些更小的区域呢?

#1


11  

Yes, it's possible. Remember that memory can be fragmented and that malloc won't be able to find a sufficiently large chunk to serve the size you requested. This can easily be way before you hit your 4 GiB limit.

是的,这是可能的。记住,内存可以是分段的,malloc不能找到足够大的块来满足您的要求。这很容易在你达到你的4尺极限之前。

#2


6  

The virtual memory limit on Win 32 is 2Gb. On Win 64, it's much bigger.

win32的虚拟内存限制是2Gb。在win64上,它要大得多。

malloc doesn't throw an exception - it returns NULL. NULL return, or exception, the memory manager can fail well before the 2Gb limit is reached if

malloc不会抛出异常——它返回NULL。如果达到2Gb的限制,那么内存管理器可能会在很长一段时间内失败

  • The paging file isn't big enough. If the paging file is limited either by policy, or by lack of room to expand: If memory allocations can't be met by page file availability then they will fail.

    分页文件不够大。如果分页文件受到策略或扩展空间不足的限制:如果页文件可用性无法满足内存分配,那么它们将失败。

  • Fragmentation. The underlying memory manager allocates memory in 4Kb chunks. Its quite possible, through patterns of allocations and deallocations to end up with a small amount of allocated memory, but a fragmented virtual memory meaning that there is no contiguous area large enough to meet a particular request.

    碎片。底层内存管理器将内存分配到4Kb的块中。很有可能,通过分配和释放模式,最终获得少量的分配内存,但是碎片化的虚拟内存意味着没有足够大的连续区域来满足特定的请求。

#3


1  

For full chapter and verse on Windows virtual memory check out this post on Mark Russinovich's Blog (lots of other great stuff here too):

有关Windows虚拟内存的完整章节,请查看Mark Russinovich博客上的这篇文章(这里还有很多其他很棒的东西):

Pushing the Limits of Windows: Virtual Memory

突破Windows的极限:虚拟内存

If memory fragmentation is your problem and writing custom allocators isn't your thing you could consider enabling the low fragmentation heap:

如果内存碎片是你的问题,编写自定义分配器不是你的事,你可以考虑启用低碎片堆:

Low Fragmentation Heap (Windows)

低碎片堆(Windows)

This is on by default these days mind you.

请注意,这是默认设置。

#4


0  

how about allocating a few smaller areas, if a huge one isn't available?

如果一个巨大的区域无法提供,那么如何分配一些更小的区域呢?