什么是默认的堆栈大小,它可以增长,它如何与垃圾收集一起使用? [英] What is the default stack size, can it grow, how does it work with garbage collection?

查看:152
本文介绍了什么是默认的堆栈大小,它可以增长,它如何与垃圾收集一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,每个线程都有自己的堆栈。原始类型和引用保留在堆栈中,并且堆栈中没有任何对象。我的问题是:

I understand that each thread has its own stack. Primitive types and references are kept on the stack, and that no object is kept on the stack. My questions are:


  • 堆栈增长多少? (比如参数 - Xms 和 - Xmx

  • 我们可以吗?限制其增长?

  • 堆栈是否具有默认的最小值和最大值?

  • 垃圾收集如何在堆栈上工作?

  • How much can a stack grow? (like with the paramters -Xms and -Xmx)
  • Can we limit its growth?
  • Does the stack have a default minimum value and maximum value?
  • How does garbage collection work on the stack?

推荐答案

筹码增加多少?

您可以使用名为 ss 的VM选项来调整最大堆栈大小。 VM选项通常使用-X {option}传递。因此,您可以使用 java -Xss1M 将堆栈大小的最大值设置为1M。

You can use a VM option named ss to adjust the maximum stack size. A VM option is usually passed using -X{option}. So you can use java -Xss1M to set the maximum of stack size to 1M.

每个线程至少有一个堆栈。一些Java虚拟机(JVM)将Java堆栈(Java方法调用)和本机堆栈(VM中的本机方法调用)放入一个堆栈中,并使用Managed to Native Frame(称为M2NFrame)执行堆栈展开。一些JVM分别保留两个堆栈。在大多数情况下, Xss 设置Java Stack的大小。

Each thread has at least one stack. Some Java Virtual Machines(JVM) put Java stack(Java method calls) and native stack(Native method calls in VM) into one stack, and perform stack unwinding using a Managed to Native Frame, known as M2NFrame. Some JVMs keep two stacks separately. The Xss set the size of the Java Stack in most cases.

对于许多JVM,他们在不同平台上为堆栈大小设置了不同的默认值。

For many JVMs, they put different default values for stack size on different platforms.

我们可以限制这种增长吗?

当发生方法调用时,将在该线程的堆栈上创建新的堆栈帧。堆栈将包含局部变量,参数,返回地址等。在java中,您永远不能将对象放在堆栈上,只有对象引用可以存储在堆栈中。由于数组也是java中的对象,因此数组也不会存储在堆栈中。因此,如果通过将参数分组为对象来减少本地原始变量,参数的数量,则可以减少堆栈上的空间。实际上,我们不能将对象放在java堆栈上的事实会影响性能一段时间(缓存未命中)。

When a method call occurs, a new stack frame will be created on the stack of that thread. The stack will contain local variables, parameters, return address, etc. In java, you can never put an object on stack, only object reference can be stored on stack. Since array is also an object in java, arrays are also not stored on stack. So, if you reduce the amount of your local primitive variables, parameters by grouping them into objects, you can reduce the space on stack. Actually, the fact that we cannot put objects on java stack affects the performance some time(cache miss).

堆栈是否有一些默认最小值或默认最大值?

As我之前说过,不同的虚拟机是不同的,可能会改变版本。请参见此处

As I said before, different VMs are different, and may change over versions. See here.

垃圾收集如何在堆栈上工作?

Java中的垃圾收集是一个热门话题。垃圾收集旨在收集堆中无法访问的对象。因此需要定义'可达'。堆栈中的所有内容都构成了GC中根集引用的一部分。每个线程的每个堆栈都可以访问的所有内容都应该被认为是实时的。还有一些其他根集引用,如Thread对象和一些类对象。

Garbage collections in Java is a hot topic. Garbage collection aims to collect unreachable objects in the heap. So that needs a definition of 'reachable.' Everything on the stack constitutes part of the root set references in GC. Everything that is reachable from every stack of every thread should be considered as live. There are some other root set references, like Thread objects and some class objects.

这只是在GC上使用堆栈非常模糊。目前大多数JVM都使用世代GC。 本文简要介绍了Java GC。最近我读了一篇非常好的文章在.net上谈论GC。 oracle jvm上的GC非常相似,所以我认为这也可以帮到你。

This is only a very vague use of stack on GC. Currently most JVMs are using a generational GC. This article gives brief introduction about Java GC. And recently I read a very good article talking about the GC on .net. The GC on oracle jvm is quite similar so I think that might also help you.

这篇关于什么是默认的堆栈大小,它可以增长,它如何与垃圾收集一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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