每个线程都有堆栈空间吗? [英] Is there a stack space for every thread?

查看:1133
本文介绍了每个线程都有堆栈空间吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我理解正确,那么堆栈是针对本地原始的,并且是对堆中对象的引用。那么如果你有多个线程会发生什么?

If I understand correctly the stack is for local primities and references to the objects in the heap. So what happens if you have more than one threads?

它们是否同时共享相同的堆栈空间(但不同的区域),或者JRE切换上下文并在线程之间切换时加载 - 卸载堆栈内容?

Do they share the same stack space at the same time (but different areas) or does the JRE switch contexts and load-deload the stack content when switching between threads?

或者JRE是否为每个线程分配单独的堆栈?

Or does the JRE allocate separate stacks for each threads?

推荐答案


或者JRE是否为每个线程分配单独的堆栈?

Or does the JRE allocate separate stacks for each threads?

概念上是。 (请参阅此 JVM规范链接,例如。)

Conceptually yes. (See this JVM spec link, for example.)

如何在特定的JVM中实现规范的概念化是......特定于实现的。但是,我的理解是当前生成(例如Hotspot)JVM将每个线程堆栈分配到从OS请求的单独的存储器块中;例如使用 mmap syscall 1

How the spec's conceptualization gets implemented in a particular JVM is ... implementation specific. However, my understanding is that current generation (e.g. Hotspot) JVMs allocate each thread stack in a separate block of memory requested from the OS; e.g. using a mmap syscall1.

当然没有批量复制堆栈内容当发生线程切换时。但是,线程上下文切换确实需要保存和加载寄存器,并且(间接地)对内存高速缓存和TLB条目进行额外加载。这可能很重要......这就是为什么过多的线程上下文切换(例如由锁争用或过度等待/通知引起)可能对性能不利。

There is certainly no wholesale copying of stack content when a thread switch occurs. However thread context switching does entail saving and loading registers, and (indirectly) to extra load on memory cache and TLB entries. This can be significant ... which is why excessive thread context switches (e.g. caused by lock contention or excessive wait/notify) can be bad for performance.

1 - 我的回忆是,一些JVM在每个堆栈段的末尾都包含一个只读的红区页面。 (这意味着线程堆栈溢出会触发内存错误,并且JVM不需要在每次方法调用时显式检查堆栈溢出,这将是一次重大的性能损失。)无论如何,我的理解是红区 页面需要使用mmap请求线程堆栈。

这篇关于每个线程都有堆栈空间吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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