java - 创建 Java 线程时的内存使用量

标签 java multithreading

实例化和启动 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/

相关文章:

java - 加载 hibernate 实体时始终调用方法

java - 获取NoSuchBeanDefinitionException : No qualifying bean of type ServerRequest in Spring WebFlux

java - 在运行时将子报表添加到主报表(java)

JAVA多线程、内存泄漏、垃圾收集器

c - 多线程文件搜索

java - 在 Java/Android 中高效地过滤 ArrayList

java - 计算数组中元素的总和

C#多线程重复

C++ 服务器收到太多请求,我想一次接受一些请求

multithreading - OmniThreadLibrary:如何检测所有递归调度(=池化)线程何时完成?