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

问题描述

我知道每个线程都有自己的stack.原始类型和引用保存在堆栈中,并且没有对象保存在堆栈中.

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.

我的问题是:

  • 一个堆栈可以增长多少?(就像参数 -Xms 和 -Xmx)
  • 我们能否限制其增长?
  • 堆栈是否有默认的最小值和最大值?
  • 垃圾收集如何在堆栈上工作?

推荐答案

一个堆栈可以增长多少?

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

How much a stack can grow?

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 explicitly put objects on Java stack affects the performance some time (cache miss).

正如我之前所说,不同的虚拟机是不同的,并且可能会随着版本的变化而变化.请参阅此处.

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

Java 中的垃圾收集是一个热门话题.垃圾收集旨在收集中无法访问的对象.所以这需要一个可达"的定义.堆栈上的所有内容都构成了 GC 中根集引用的一部分.从每个线程的每个堆栈可访问的所有内容都应视为活动的.还有一些其他的根集引用,比如线程对象和一些类对象.

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 platform. The GC on Oracle JVM is quite similar so I think that might also help you.

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

10-27 23:37