java中的静态分配 - 堆,堆栈和永久生成 [英] static allocation in java - heap, stack and permanent generation

查看:119
本文介绍了java中的静态分配 - 堆,堆栈和永久生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在阅读很多有关java中内存分配方案的内容,而且我从各种来源阅读时都有很多疑问。我已经收集了我的概念,并且我会要求完成所有要点并对它们进行评论。我开始知道内存分配是特定于JVM的,所以我必须事先说,我的问题是Sun特定的。

I have been lately reading a lot on memory allocation schemes in java, and there have been many doubts as I have been reading from various sources. I have collected my concepts, and I would request to go through all of the points and comment on them. I came to know that memory allocation is JVM specific, so I must say beforehand, that my question is Sun specific.


  1. 类(由类加载器)进入堆上的特殊区域:永久生成

  2. 所有与类相关的信息,类的关联对象数组,JVM使用的内部对象(像java / lang / Object),优化信息进入永久生成区域。

  3. 所有静态成员变量再次保存在Permanent Generation区域。

  4. 对象进入不同的堆:年轻代

  5. 每个类只有一个方法的副本,无论是静态还是非静态方法。该副本放在永久生成区域。
    对于非静态方法,所有参数和局部变量都进入堆栈 - 只要对该方法进行具体调用,我们就会得到一个与之相关的新堆栈帧。
    我不确定静态方法的局部变量存储在哪里。他们是永久世代的堆吗?或者只是他们的引用存储在Permanent Generation区域,而实际的副本是在其他地方(Where?)

  6. 我也不确定方法的返回类型存储在哪里。

  7. 如果对象(在年轻一代中)需要使用静态成员(在永久代中),则会给出对静态成员&&的引用。它们有足够的内存空间来存储方法的返回类型等。

  1. Classes (loaded by the classloaders) go in a special area on heap : Permanent Generation
  2. All the information related to a class like name of the class, Object arrays associated with the class, internal objects used by JVM (like java/lang/Object) and optimization information goes into the Permanent Generation area.
  3. All the static member variables are kept on the Permanent Generation area again.
  4. Objects go on a different heap : Young generation
  5. There is only one copy of each method per class, be the method static or non-static. That copy is put in the Permanent Generation area. For non-static methods, all the parameters and local variables go onto the stack - and whenever there is a concrete invocation of that method, we get a new stack-frame associated with it. I am not sure where are the local variables of a static method are stored. Are they on the heap of Permanent Generation ? Or just their reference is stored in the Permanent Generation area, and the actual copy is somewhere else (Where ?)
  6. I am also unsure where does the return type of a method get stored.
  7. If the objects (in the young generation) needs to use a static member (in the permanent generation), they are given a reference to the static member && they are given enough memory space to store the return type of the method,etc.

感谢您仔细阅读!

推荐答案

首先,您现在应该清楚,很少有人可以从第一手知识中确认这些答案。很少有人在最近的HotSpot JVM上工作或研究它们到真正需要的深度。这里的大多数人(包括我自己)都是基于他们在其他地方写过的东西,或者他们所推断的东西来回答。通常,此处或各种文章和网页中所写的内容基于其他来源,这些来源可能是也可能不是明确的。通常它是简化的,不准确的或者只是简单的错误。

First, as should be clear to you by now that there are very few people who can confirm these answers from first hand knowledge. Very few people have worked on recent HotSpot JVMs or studied them to the depth needed to really know. Most people here (myself included) are answering based on things they have seen written elsewhere, or what they have inferred. Usually what is written here, or in various articles and web pages, is based on other sources which may or may not be definitive. Often it is simplified, inaccurate or just plain wrong.

如果你想要确切肯定你的答案,你真的需要下载OpenJDK源代码......和通过阅读和理解源代码来做自己的研究。询问关于SO的问题,或通过随机网络文章进行拖网搜索并不是一种合理的学术研究技术。

If you want definitive confirmation of your answers, you really need to download the OpenJDK sourcecode ... and do your own research by reading and understanding the source code. Asking questions on SO, or trawling through random web articles is not a sound academic research technique.

说完......


1)类(由类加载器加载)进入堆上的特殊区域:永久生成。

1) Classes (loaded by the classloaders) go in a special area on heap : Permanent Generation.

AFAIK,是的。 (更新:见下文。)

AFAIK, yes. (Update: see below.)


2)与班级名称相关的所有信息,与类关联的对象数组,JVM使用的内部对象(如java / lang / Object)和优化信息进入永久生成区域。

2) All the information related to a class like name of the class, Object arrays associated with the class, internal objects used by JVM (like java/lang/Object) and optimization information goes into the Permanent Generation area.

或多或少,是的。我不确定你的意思是什么。我猜测JVM使用的内部对象(如java / lang / Object)表示JVM内部类描述符。

More or less, yes. I'm not sure what you mean by some of those things. I'm guessing that "internal objects used by JVM (like java/lang/Object)" means JVM-internal class descriptors.


3 )所有静态成员变量再次保留在Permanent Generation区域。

3) All the static member variables are kept on the Permanent Generation area again.

变量本身是。这些变量(与所有Java变量一样)将包含原始值或对象引用。但是,虽然静态成员变量位于permgen堆中分配的帧中,但这些变量引用的对象/数组可以在任何堆中分配。

The variables themselves yes. These variables (like all Java variables) will hold either primitive values or object references. However, while the static member variables are in a frame that is allocated in the permgen heap, the objects/arrays referred to by those variables may be allocated in any heap.


4)对象进入不同的堆:年轻代

4) Objects go on a different heap : Young generation

不一定。大型对象可以直接分配到终身代。

Not necessarily. Large objects may be allocated directly into the tenured generation.


5)每种方法只有一个副本每个类,是静态或非静态的方法。该副本放在永久生成区域。

5) There is only one copy of each method per class, be the method static or non-static. That copy is put in the Permanent Generation area.

假设您指的是方法的代码,那么AFAIK是。但它可能有点复杂。例如,代码可能在JVM生命周期的不同时间以字节码和/或本机代码形式存在。

Assuming that you are referring to the code of the method, then AFAIK yes. It may be a little more complicated though. For instance that code may exist in bytecode and/or native code forms at different times during the JVM's life.


...对于非静态方法,所有参数和局部变量都进入堆栈 - 只要有一个具体的方法调用,我们就会得到一个与之相关的新堆栈框架。

... For non-static methods, all the parameters and local variables go onto the stack - and whenever there is a concrete invocation of that method, we get a new stack-frame associated with it.

是。


...我不确定静态方法的局部变量存储在哪里。他们是永久世代的堆吗?或者只是他们的引用存储在Permanent Generation区域,而实际的副本是在其他地方(Where?)

... I am not sure where are the local variables of a static method are stored. Are they on the heap of Permanent Generation ? Or just their reference is stored in the Permanent Generation area, and the actual copy is somewhere else (Where ?)

No。它们存储在堆栈中,就像非静态方法中的局部变量一样。

No. They are stored on the stack, just like local variables in non-static methods.


6)我也不确定方法的返回类型在哪里存储。

6) I am also unsure where does the return type of a method get stored.

如果您指的是(非空)方法调用返回的,则它将在堆栈或机器寄存器中返回。如果它在堆栈上返回,则需要1或2个单词,具体取决于返回类型。

If you mean the value returned by a (non-void) method call, then it is either returned on the stack or in a machine register. If it is returned on the stack, this takes 1 or two words, depending on the return type.


7)如果对象(在年轻一代使用静态成员(在永久世代),他们被赋予静态成员&&它们有足够的内存空间来存储方法的返回类型等。

7) If the objects (in the young generation) nees to use a static member (in the permanent generation), they are given a reference to the static member && they are given enough memory space to store the return type of the method,etc.

这是不准确的(至少,你是不清楚地表达自己。)

That is inaccurate (or at least, you are not expressing yourself clearly).

如果某个方法访问静态成员变量,它得到的是原始值或对象引用。这可以分配给(现有的)局部变量或参数,分配给(现有的)静态或非静态成员,分配给先前分配的数组的(现有)元素,或者简单地使用和丢弃。

If some method accesses a static member variable, what it gets is either a primitive value or an object reference. This may be assigned to an (existing) local variable or parameter, assigned to an (existing) static or non-static member, assigned to an (existing) element of a previously allocated array, or simply used and discarded.


  • 在任何情况下都不需要分配 new 存储来保存引用或原语值。

  • In no case does new storage need to be allocated to hold either a reference or a primitive value.

通常,存储一个对象或数组引用所需的内存,原始值通常占用一个或两个单词,取决于硬件架构。

Typically, one word of memory is all that is needed to store an object or array reference, and a primitive value typically occupies one or two words, depending on the hardware architecture.

在任何情况下,调用者都不需要分配空间来保存方法返回的某些对象/数组。在Java中,始终使用按值传递语义返回对象和数组...但返回的值是对象或数组引用。

In no case does space need to be allocated by the caller to hold some object / array returned by a method. In Java, objects and arrays are always returned using pass-by-value semantics ... but that value that is is returned is an object or array reference.

UPDATE

从Java 8开始,PermGen空间已被Metaspace取代。有关更多信息,请参阅以下资源:

As of Java 8, the PermGen space has been replaced with Metaspace. For more information, please refer to these resources:

  • What is the difference between PermGen and Metaspace?
  • Java 8: From PermGen to Metaspace
  • About G1 Garbage Collector, Permanent Generation and Metaspace

这篇关于java中的静态分配 - 堆,堆栈和永久生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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