什么决定了可以分配多少内存?
这是我之前的问题关于为什么 size_t 是必要的的后续问题。
鉴于 size_t 保证足够大以表示您可以分配的内存块的最大大小(意味着仍然可以有一些大于 size_t 的整数),我的问题是......
什么决定了您一次可以分配多少内存?
This is a follow-up to my previous question about why size_t is necessary.
Given that size_t is guaranteed to be big enough to represent the largest size of a block of memory you can allocate (meaning there can still be some integers bigger than size_t), my question is...
What determines how much you can allocate at once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的机器的体系结构、操作系统(但两者是交织在一起的)和你的编译器/库集决定了你可以一次分配多少内存。
malloc
并不需要能够使用操作系统可以提供给他的所有内存。操作系统不需要需要提供计算机中存在的所有内存(例如,不同版本的 Windows Server 具有不同的最大内存出于许可原因)但请注意,操作系统可以提供比机器中现有的内存更多的内存,甚至比主板允许的内存更多的内存(假设主板有一个内存插槽,只接受 1GB 内存条,Windows 可以仍然让程序分配 2GB 内存)。这是通过使用虚拟内存、分页(你知道,交换文件,你的老朋友,速度慢的朋友:-) 来完成的,或者,例如,通过使用 NUMA。
The architecture of your machine, the operating system (but the two are intertwined) and your compiler/set of libraries determines how much memory you can allocate at once.
malloc
doesn't need to be able to use all the memory the OS could give him. The OS doesn't need to make available all the memory present in the machine (and various versions of Windows Server for example have different maximum memory for licensing reasons)But note that the OS can make available more memory than the one present in the machine, and even more memory than the one permitted by the motherboard (let's say the motherboard has a single memory slot that accepts only 1gb memory stick, Windows could still let a program allocate 2gb of memory). This is done throught the use of Virtual Memory, Paging (you know, the swap file, your old and slow friend :-) Or, for example, through the use of NUMA.
在实际代码中,我可以想到三个约束:
I can think of three constraints, in actual code: