栈和堆分配 [英] Stack and Heap allocation

查看:109
本文介绍了栈和堆分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待到内存模型多一点,并正与理解的进程中有多少堆存在挣扎。

I'm looking into the memory model a little more and am struggling with understanding how many heap's exist in a process.

因此​​,如果我们有1进程,5个线程在里面,我在说,我们会有5栈和堆1纠正?

So if we have 1 process with 5 threads in it, am I correct in saying that we'd have 5 stacks and 1 heap?

如果是这样,可以在线程访问彼此的堆栈(或者这正是为什么他们有独立的堆栈,防止腐败),如果有只是1堆,那么很明显他们都访问该堆,因此需要进行多线程锁定?我是否正确理解这一点?

If so, can the threads access one another's stacks (or is this exactly why they have separate Stacks, to prevent corruption), and if there's just 1 heap, then obviously they all access this heap, hence the need for locking with multiple threads? Am I understanding this correctly?

推荐答案

是的,每个线程都有自己的堆栈。这是一个困难的必要性,堆栈跟踪其中方法返回它完成后,它存储的返回地址。因为每个线程执行自己的代码,他们需要自己的堆栈。局部变量和方法的参数存储有太多,这使得它们(通常)的线程安全

Yes, each thread has its own stack. That's a hard necessity, the stack keeps track of where a method returns after it finishes, it stores the return address. Since each thread executes its own code, they need their own stack. Local variables and method arguments are stored there too, making them (usually) thread-safe.

堆的数量是一个更复杂的细节。您是垃圾回收堆计数1。这不是从一个角度实现点完全正确的,这三个代堆加上大对象堆在逻辑上是不同的堆,还说最多四个。这实现细节开始,当您分配太多关系。

The number of heaps is a more involved detail. You are counting 1 for the garbage collected heap. That's not entirely correct from an implementation point of view, the three generational heap plus the Large Object Heap are logically distinct heaps, adding that up to four. This implementation detail starts to matter when you allocate too much.

另外一个,你不能在托管代码完全忽视的是,存储静态变量的堆。它与相关的AppDomain,静态变量只要AppDomain中生活居住。通常命名为装载机堆在.NET中的文学。它实际上包含了3堆(高频,低频和存根堆),即时编译代码和数据类型过于存储在那里但是这让到细节问题。

Another one that you can't entirely ignore in managed code is the heap that stores static variables. It is associated with the AppDomain, static variables live as long as the AppDomain lives. Commonly named "loader heap" in .NET literature. It actually consists of 3 heaps (high frequency, low frequency and stub heap), jitted code and type data is stored there too but that's getting to the nitty gritty.

另外下忽略列表是由本机代码使用的堆。其中两个是来自Marshal类容易看见。有一个默认的进程堆,从中Windows分配,也是如此Marshal.AllocHGlobal()。并有一个独立的堆在那里COM存储数据,Marshal.AllocCoTaskMem()从中分配。最后你互操作的本地代码都将拥有自己为它的运行时支持堆。通过那种代码中使用的堆数仅由得到加载到进程机DLL的数量限制。所有这些堆的存在,你几乎永远直接处理这些问题。

Further down the ignore list are the heaps used by native code. Two of them are readily visible from the Marshal class. There's a default process heap, Windows allocates from it, so does Marshal.AllocHGlobal(). And there's a separate heap where COM stores data, Marshal.AllocCoTaskMem() allocates from it. Lastly any native code you interop with will have its own heap for its runtime support. The number of heaps used by that kind of code is bounded only by the number of native DLLs that get loaded into your process. All of these heaps exist, you barely ever deal with them directly.

所以,10堆最低限度。

So, 10 heaps minimum.

这篇关于栈和堆分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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