Linux中堆栈内存在物理上是连续的吗? [英] Is stack memory contiguous physically in Linux?

查看:954
本文介绍了Linux中堆栈内存在物理上是连续的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,堆栈内存在虚拟内存地址中是连续的,但是堆栈内存在物理上也是连续的?这与堆栈大小限制有关吗?

As far as I can see, stack memory is contiguous in virtual memory address, but stack memory is also contiguous physically? And does this have something to do with the stack size limit?

我曾经认为堆栈内存不必在物理上是连续的,但是为什么我们认为堆栈内存总是比堆内存快?如果它在物理上不是连续的,那么堆栈如何利用缓存的更多优势呢?还有另一件事总是让我感到困惑,cpu在数据段中执行指令,该指令不在虚拟内存中的堆栈段附近,我认为操作系统不会使堆栈段和数据段在物理上彼此靠近,因此这可能会损害缓存效果,您怎么看?

I used to believe that stack memory doesn't has to be contiguous physically, but why do we think that stack memory is always quicker than heap memory? If it's not physically contiguous, how can stack take more advantage of cache? And there is another thing that always confuse me, cpu executes directives in data segment, which is not near the stack segment in virtual memory, I don't think the operating system will make stack segment and data segment close to each other physically, so this might do harm to the cache effect, what do you think?

再次 也许我应该举个例子来更好地表达自己,如果我们想对大量数字进行排序,则使用数组存储数字要比使用列表更好,因为每个列表节点都可以由malloc构造,因此它可能没有充分利用缓存,这就是为什么我说堆栈内存比堆内存快.

Edit again: Maybe I should give an example to express myself better, if we want to sort a large amount of numbers, using array to store the numbers is better than using a list, because every list node may be constructed by malloc, so it may not take good advantage of cache, that's why I say stack memory is quicker than heap memory.

推荐答案

据我所知,堆栈内存在虚拟内存中是连续的 地址,但堆栈内存在物理上也连续吗?并做到这一点 与堆栈大小限制有关吗?

As far as I can see, stack memory is contiguous in virtual memory address, but stack memory is also contiguous physically? And does this have something to do with the stack size limit?

否,堆栈存储器在物理地址空间中不一定是连续的.它与堆栈大小限制无关.这与操作系统如何管理内存有关.仅当首次访问相应的虚拟页面(或自从虚拟页面调出到磁盘以来的第一次)时,操作系统才分配物理页面.这称为需求分页,它有助于节省内存使用量.

No, stack memory is not necessarily contiguous in the physical address space. It's not related to the stack size limit. It's related to how the OS manages memory. The OS only allocates a physical page when the corresponding virtual page is accessed for the first time (or for the first time since it got paged out to the disk). This is called demand-paging, and it helps conserve memory usage.

为什么我们认为堆栈存储器总是更快 比堆内存?如果在物理上不连续,如何堆叠 充分利用缓存?

why do we think that stack memory is always quicker than heap memory? If it's not physically contiguous, how can stack take more advantage of cache?

它与缓存无关.从堆栈分配和取消分配内存比堆快得多.这是因为从堆栈分配和取消分配仅需要一条指令(递增或递减堆栈指针).另一方面,从堆分配和/或取消分配内存涉及更多的工作.有关更多信息,请参见文章.

It has nothing to do with the cache. It's just faster to allocate and deallocate memory from the stack than the heap. That's because allocating and deallocating from the stack takes only a single instruction (incrementing or decrementing the stack pointer). On the other hand, there is a lot more work involved into allocating and/or deallocating memory from the heap. See this article for more information.

现在,一旦分配了内存(从堆或堆栈),访问分配的内存区域所花费的时间就不再取决于堆栈还是堆内存.这取决于内存访问行为以及它是否对缓存和内存体系结构友好.

Now once memory allocated (from the heap or stack), the time it takes to access that allocated memory region does not depend on whether it's stack or heap memory. It depends on the memory access behavior and whether it's friendly to the cache and memory architecture.

如果我们要对大量数字进行排序,请使用数组存储 数字比使用列表更好,因为每个列表节点都可以 由malloc构造,因此可能无法充分利用缓存, 这就是为什么我说堆栈内存比堆内存快.

if we want to sort a large amount of numbers, using array to store the numbers is better than using a list, because every list node may be constructed by malloc, so it may not take good advantage of cache, that's why I say stack memory is quicker than heap memory.

使用数组更快,不是因为数组是从堆栈中分配的.可以从任何内存(堆栈,堆或任何地方)分配数组.由于数组通常一次可连续访问一个元素,因此速度更快.当访问第一个元素时,包含该元素和其他元素的整个缓存行将从内存中提取到L1缓存中.因此,可以非常高效地访问该缓存行中的其他元素,但是访问缓存行中的第一个元素仍然很慢(除非缓存行为

Using an array is faster not because arrays are allocated from the stack. Arrays can be allocated from any memory (stack, heap, or anywhere). It's faster because arrays are usually accessed contiguously one element at a time. When the first element is accessed, a whole cache line that contains the element and other elements is fetched from memory to the L1 cache. So accessing the other elements in that cache line can be done very efficiently, but accessing the first element in the cache line is still slow (unless the cache line was prefetched). This is the key part: since cache lines are 64-byte aligned and both virtual and physical pages are 64-byte aligned as well, then it's guaranteed that any cache line fully resides within a single virtual page and a single physical page. This what makes fetching cache lines efficient. Again, all of this has nothing to do with whether the array was allocated from the stack or heap. It holds true either way.

另一方面,由于链表的元素通常不连续(甚至不在虚拟地址空间中),因此包含元素的缓存行可能不包含任何其他元素.因此,获取每个元素可能会更昂贵.

On the other hand, since the elements of a linked list are typically not contiguous (not even in the virtual address space), then a cache line that contains an element may not contain any other elements. So fetching every single element can be more expensive.

这篇关于Linux中堆栈内存在物理上是连续的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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