4

From my understanding, when a process is under execution it has some amount of memory at it's disposal. As the stack increases in size it builds from one end of the process (disregarding global variables that come before the stack), while the heap builds from another end. If you keep adding to the stack or heap, eventually all the memory will be used up for this process.

How does the amount of memory the process is given get determined? I can only imagine it depends on a bunch of different variables, but an as-general-as-possible response would be great. If things have to get specific, I'm interested in linux processes written in C++.

4

2 回答 2

2

在您将遇到的大多数平台上,Linux 在启用虚拟内存的情况下运行。这意味着每个进程都有自己的虚拟地址空间,其大小仅由硬件和内核配置它的方式决定。

例如,在具有“3/1”拆分配置的 x86 架构上,每个用户空间进程都有 3GB 的可用地址空间,其中分配了堆和堆栈。这与系统中有多少可用物理内存无关。在 x86-64 架构上,每个用户空间进程通常可以使用 128TB 的地址空间。

物理内存被单独分配以支持该虚拟内存。进程可用的数量取决于系统的配置,但通常它只是“按需”提供 - 主要限制存在多少物理内存和交换文件空间,以及当前有多少用于其他目的.

于 2011-06-20T06:23:50.150 回答
-1

堆栈不会神奇地增长。它的大小是静态的,大小是在链接时确定的。因此,当您从堆栈中获取足够的空间时,它会溢出(堆栈溢出;)

另一方面,堆区域“神奇地”增长。这意味着当堆需要更多内存时,程序会要求操作系统提供更多内存。

编辑:正如 Mat 在下面指出的那样,堆栈实际上可以在现代操作系统的运行时增加。

于 2011-06-18T12:38:18.980 回答