在运行时更新java线程的堆栈大小 [英] Update a java thread's stack size at runtime

查看:723
本文介绍了在运行时更新java线程的堆栈大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道是否有办法动态(运行时)增加主线程的堆栈大小?另外,我认为这是同一个问题,是否有可能在实例化后增加/更新 Thread 的堆栈大小?

Does anyone know if there is a way to dynamically (runtime) increase the stack size of the main Thread? Also, and I believe it is the same question, is it possible to increase / update the stack size of a Thread after its instantiation?

线程的CTOR允许定义其堆栈大小,但我找不到任何更新它的方法。实际上,我没有找到JDK中堆栈大小的任何管理(这往往表明它不可能),一切都在VM中完成。

Thread’s CTOR allows the definition of its stack size but I can’t find any way to update it. Actually, I didn’t find any management of the stack size in the JDK (which tends to indicate that it’s not possible), everything is done in the VM.

根据到 java语言规范可以在创建堆栈时设置堆栈大小,但有一个注释:

According to the java language specification it is possible to set the stack size ‘when stack is created’ but there is a note:

Java虚拟机实现可能提供程序员或用户控制Java虚拟机堆栈的初始大小,以及在动态扩展或收缩Java虚拟机堆栈的情况下,控制最大和最小大小。

A Java virtual machine implementation may provide the programmer or the user control over the initial size of Java virtual machine stacks, as well as, in the case of dynamically expanding or contracting Java virtual machine stacks, control over the maximum and minimum sizes.

IMO不是很清楚,这是否意味着某些VM处理的线程的最大(编辑)堆栈大小在给定范围内发展?我们可以用Hostpot做到这一点(我没有在Xss旁边找到任何与堆栈大小相关的选项)?

IMO that’s not very clear, does that mean that some VM handle Threads with max (edit) stack sizes evolving within a given range? Can we do that with Hostpot (I didn't find any stack size related options beside Xss) ?

谢谢!

推荐答案

堆栈大小在使用时会动态更新,因此您无需这样做。

The stack size dynamcally updates itself as it is used so you never need to so this.

您可以设置什么是 -Xss 的最大大小这是使用的虚拟内存大小,您可以在64位JVM上使其大小。使用的实际内存基于您使用的内存量。 ;)

What you can set is the maximum size it can be with -Xss This is the virtual memory size used and you can make it as large as you like on 64-bit JVMs. The actual memory used is based on the amount of memory you use. ;)

编辑:重要的区别是最大大小被保留为虚拟内存(堆btw也是如此)。即保留地址空间,这也是无法扩展的原因。在32位系统中,您的地址空间有限,这仍然是个问题。但在64位系统中,您通常拥有高达256 TB的虚拟内存(处理器限制),因此虚拟内存很便宜。实际内存以页面形式分配(通常为4 KB),并且仅在使用时分配。这就是为什么Java应用程序的内存似乎随着时间的推移而增长,即使在启动时分配了最大堆大小。线程堆栈也会发生同样的事情。仅分配实际触摸的页面。

The important distinction is that the maximum size is reserved as virtual memory (so is the heap btw). i.e. the address space is reserved, which is also why it cannot be extended. In 32-bit systems you have limited address space and this can still be a problem. But in 64-bit systems, you usually have up to 256 TB of virtual memory (a processor limitation) so virtual memory is cheap. The actual memory is allocated in pages (typically 4 KB) and they are only allocated when used. This is why the memory of a Java application appears to grow over time even though the maximum heap size is allocated on startup. The same thing happens with thread stacks. Only the pages actually touched are allocated.

这篇关于在运行时更新java线程的堆栈大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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