c - 编程上下文中的虚拟地址空间

标签 c memory-management

我对虚拟地址空间的含义感到困惑。在 32 位机器中,一个进程可以寻址 2^32 个内存位置。这是否意味着每个进程的虚拟地址空间都是 2^32 (4GB) ?

以下是进程虚拟地址空间的快照。这可以增长到 4GB 吗?这样的系统有进程数限制吗?

enter image description here

最佳答案

这可以增长到 4GB 吗?

The size of the address space is capped by the number of unique pointer values. For a 32-bit processor, a 32-bit value can represent 2 ^ 32 distinct values. If you allow each such value to address a different byte of memory, you get 2 ^ 32 bytes, which equals four gigabytes.

所以,是的,一个进程的虚拟地址空间理论上可以增长到 4 GB。然而实际上,这也可能取决于系统和处理器。可以看出:

This theoretical maximum cannot be achieved on the Pentium class of processors, however. One reason is that the lower bits of the segment value encode information about the type of selector. As a result, of the 65536 possible selector values, only 8191 of them are usable to access user-mode data. This drops you to 32TB.

请注意,有两种方法可以从系统分配内存,当然,您可以使用 C 的 malloc 为您的进程隐式分配内存(您的问题已标记 ),但显式映射文件字节。

这样的系统有进程数限制吗?

a process includes one or more threads that actually execute the code in the process (technically, processes don’t run, threads do) and that are represented with kernel thread objects.

根据进行的一些测试here , 一个默认地址空间为2GB的32位Windows XP系统可以创建大约2025个线程:

Thread Limit of 2025

然而,在分配了 4GB 地址空间的 64 位 Windows XP 上运行的 32 位测试限制 创建接近 3204 个线程:

32bit test limit on 64 bit XP created 3204 threads

然而,确切的线程和进程限制是非常多变的,它取决于很多因素。线程指定其堆栈大小的方式、进程指定其最小工作集的方式、可用物理内存量和系统提交限制。在任何情况下,在现代系统上您通常不必担心这一点,因为如果您的应用程序真的超过了线程限制,您应该重新考虑您的设计,因为几乎总是有替代方法来完成具有合理数量的相同目标。

关于c - 编程上下文中的虚拟地址空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9511982/

相关文章:

c - 空指针是否间接指向数组类型 UB?

c - 如何使用单词 'int' ,但作为字符串而不是 C 中的数据类型?

编译C代码字符串

调用空闲 block

arrays - Swift Struct 实例在更新它的值时发生变化

c++ - 使用新的删除技巧防止内存碎片

c - 在C编程中如何索引一组元素并返回索引数组

c - 结构声明中需要 typedef

c# - 避免数组重复

objective-c - 我应该释放我创建的NSThread吗?