实例化和启动 Java 线程时(大致)分配了多少内存?
这是一个代码示例:
// Definition of the thread class
class BasicThread extends Thread {
// This method is called when the thread runs
public void run() {
}
}
.
.
.
// Create and start the thread
Thread thread = new BasicThread();
thread.start();
最佳答案
好吧,线程(即对象)本身需要一些空间——它确实有十几个变量和对象(我懒得正确计算它们)但它应该比 200 字节多一点(您基本上必须计算所有基元和引用[微不足道,它们具有固定大小 - 但引用取决于您的 VM],然后计算由类分配的所有对象的大小[热点 VM 的开销为 2 个字每个对象(如果对象中没有局部变量则为 3)并在 8 字节边界上分配])
真正占用空间的是线程本地堆栈,它会受到 VM 的 -Xss 标志的影响(尽管请注意,每个操作系统对最大堆栈空间都有一些限制,您可以在 linux 中使用 -ulimit 来影响它,当然不知何故在Windows中也是如此)。
热点的默认值如下:
In Java SE 6, the default on Sparc is 512k in the 32-bit VM, and 1024k in the 64-bit VM. On x86 Solaris/Linux it is 320k in the 32-bit VM and 1024k in the 64-bit VM.
On Windows, the default thread stack size is read from the binary (java.exe). As of Java SE 6, this value is 320k in the 32-bit VM and 1024k in the 64-bit VM.
关于java - 创建 Java 线程时的内存使用量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6347474/