即使有可用内存,小堆栈的基本原理是什么? [英] What is the rationale for small stack even when memory is available?

查看:87
本文介绍了即使有可用内存,小堆栈的基本原理是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在一次采访中被问到,当可用内存没有限制时,为什么会有一个较小的堆栈?即使您可能有4GB的物理内存,为什么还要在1KB的范围内使用它?这是标准的设计惯例吗?

解决方案

您的堆栈越小,可以容纳的堆栈越多. 1kB的堆栈几乎没有用,因为我想不出具有那么小的页面的体系结构.更典型的大小是128kB-1MB.

由于每个线程都有自己的堆栈,因此可以拥有的堆栈数是可以拥有的线程数的上限.有些人抱怨说,他们不能在32位Windows进程的标准2GB地址空间中创建2000个以上的线程,因此,有些人希望使用甚至更少的堆栈来允许更多的线程也就不足为奇了. >

此外,请考虑一下,如果必须提前完全保留堆栈,则它是在地址空间中刻出一块,直到不再使用该堆栈(即线程退出)之后,才能返回该块.然后,那部分保留的地址空间限制了您可以进行的连续分配的大小.

Recently, I was asked in an interview, why would you have a smaller stack when the available memory has no limit? Why would you have it in 1KB range even when you might have 4GB physical memory? Is this a standard design practice?

解决方案

The smaller your stacks, the more of them you can have. A 1kB stack is pretty useless, as I can't think of an architecture that has pages that small. A more typical size is 128kB-1MB.

Since each thread has its own stack, the number of stacks you can have is an upper limit on the number of threads you can have. Some people complain about the fact that they can't create more than 2000 threads in a standard 2GB address space of a 32-bit Windows process, so it's not surprising that some people would want even smaller stacks to allow even more threads.

Also, consider that if a stack has to be completely reserved ahead of time, it is carving a chunk out of your address space that can't be returned until the stack isn't used anymore (i.e. the thread exits). That chunk of reserved address space then limits the size of a contiguous allocation you can make.

这篇关于即使有可用内存,小堆栈的基本原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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