简体   繁体   English

PermGen堆的空间

[英]PermGen space of heap

As shown in the image , Permgen is further divided into several parts. 由于在所示的图像 ,PermGen的被进一步划分成几个部分。

在此输入图像描述

Runtime constant pool stores constants pertaining to each type that is loaded by class loader. 运行时常量池存储与类加载器加载的每种类型相关的常量。

Method area stores method information such as method return type, method name. 方法区域存储方法返回类型,方法名称等方法信息。 ( correct me if I am wrong here. ) 如果我错了,请纠正我。

And Reserved area is the part which is reserved if more memory is required by permgen. 如果permgen需要更多内存,则保留区域保留的部分。

But what I don't understand is, what is code area in the image? 但我不明白的是,图像中的代码区域是什么? Any code is stored in this space(seems vague to me)? 任何代码都存储在这个空间中(对我来说似乎含糊不清)?

Any code is stored in this space(seems vague to me)?

Any specific reason for that ? 有什么具体原因吗?

The possible answer could be : Code area stores the byte code of the classes loaded into your memory. 可能的答案可能是:代码区存储加载到内存中的类的字节代码。

But then the question comes, Why class is not loaded directly in RAM ? 但问题来了, 为什么类没有直接加载到RAM中?

Because we have a JVM to provide interoperability, Since JVM is an intermediary between java code and the machine , we need some place to store the code statements until JVM is scheduled by OS to execute its code.(for OS JVM is a process). 因为我们有一个JVM来提供互操作性,因为JVM是java代码和机器之间的中介,我们需要一些地方来存储代码语句,直到操作系统调度JVM来执行其代码。(对于OS JVM是一个过程)。 So, It loads the byte code in Code area(if i am right) and when scheduled, further interprets code(.class) into underlying machine instructions. 因此,它在代码区域中加载字节代码(如果我是正确的),并且在调度时,进一步将代码(.class)解释为底层机器指令。

The answer to me is "Code area holds the byte code of the classes". 我的答案是“代码区域保存类的字节代码”。

To back the idea mentioned above ., here are some concepts copied as it is from Oracle blog which says: 为了支持上面提到的想法,这里有一些概念被复制,因为它来自Oracle博客 ,它说:

So the Java classes are stored in the permanent generation. 因此Java类存储在永久代中。 What all does that entail? 这一切意味着什么? Besides the basic fields of a Java class there are: 除了Java类的基本字段外,还有:

  1. Methods of a class (including the bytecodes) 类的方法(包括字节码)
  2. Names of the classes (in the form of an object that points to a string also in the permanent generation) 类的名称(以永久代中也指向字符串的对象的形式)
  3. Constant pool information (data read from the class file, see chapter 4 of the JVM specification for all the details). 常量池信息(从类文件读取的数据,有关所有详细信息,请参阅JVM规范的第4章)。
  4. Object arrays and type arrays associated with a class (eg, an object array containing references to methods). 与类关联的对象数组和类型数组(例如,包含对方法的引用的对象数组)。
  5. Internal objects created by the JVM (java/lang/Object or java/lang/exception for instance) JVM创建的内部对象(例如java / lang / Object或java / lang / exception)
  6. Information used for optimization by the compilers (JITs) 用于编译器优化的信息(JIT)

Hope it clears. 希望它清除。

From an interesting article on the problems of PermGen: Will Java 8 Solve PermGen OutOfMemoryError? 来自一篇关于PermGen问题的有趣文章: Java 8会解决PermGen OutOfMemoryError吗? :

Jon Masamitsu, JVM developer at Oracle, explained 2006 in his blog the purpose of the permanent generation: The permanent generation contains information about classes, such as bytecode, names and JIT information . Oracle的JVM开发人员Jon Masamitsu在他的博客中解释了2006年永久代的目的:永久代包含有关类的信息,例如字节码,名称和JIT信息 It is stored in a separate space, because it is mostly static and garbage collection can be much more optimized by separating it. 它存储在一个单独的空间中,因为它主要是静态的,垃圾收集可以通过分离来更加优化。

Actually the PermGen store all your static code. 实际上PermGen存储了所有静态代码。 i think this makes sense to you why there is a code area in PermGen. 我认为这对你来说是有道理的,为什么PermGen中有代码区域。

I will venture to guess , based on the following article , by Jon Masamitsu, from which the following quote is taken, that the figure above is a misrepresentation (or rephrased - a misleading representation): 根据以下文章 ,我将冒险猜测 ,以下引用的是Jon Masamitsu,上图是误传(或改写 - 误导性表示):

So the Java classes are stored in the permanent generation. 因此Java类存储在永久代中。 What all does that entail? 这一切意味着什么? Besides the basic fields of a Java class there are 除了Java类的基本字段外

Methods of a class (including the bytecodes) 类的方法(包括字节码)

Names of the classes (in the form of an object that points to a string also in the permanent generation) 类的名称(以永久代中也指向字符串的对象的形式)

Constant pool information (data read from the class file, see chapter 4 of the JVM specification for all the details). 常量池信息(从类文件读取的数据,有关所有详细信息,请参阅JVM规范的第4章)。

Object arrays and type arrays associated with a class (eg, an object array containing references to methods). 与类关联的对象数组和类型数组(例如,包含对方法的引用的对象数组)。

Internal objects created by the JVM (java/lang/Object or java/lang/exception for instance) JVM创建的内部对象(例如java / lang / Object或java / lang / exception)

Information used for optimization by the compilers (JITs) 用于编译器优化的信息(JIT)

The bytecode of all classes that have been resolved live in permgen. 解析的所有类的字节码都存在于permgen中。 Just because a library has 1.2MB of classes doesn't they will be loaded by the JVM from the JAR. 仅仅因为一个库有1.2MB的类,它们不会被JVM从JAR加载。 It it possible, even likely, that only a small fraction of those classes are used by a particular application. 甚至可能的是,特定应用程序只使用这些类中的一小部分。

You can run many large application servers whose sum total JAR size is >1GB using only 64MB permgen, because only a fraction of the classes are ever used. 您可以运行许多大型应用程序服务器,其总和JAR大小> 1GB,仅使用64MB permgen,因为只使用了一小部分类。

Also take this example: 还举个例子:

class A {
   // ... code
}

class B {
   void method1() {
      // something
   }

   void method2() {
      A a = new A();
   }
}

While these classes may reside in the same JAR, merely creating an instance of B does not cause class A to be loaded. 虽然这些类可能驻留在同一个JAR中,但仅创建B的实例不会导致加载A类。 If you never call method2() , class A will never be loaded by the JVM. 如果你从不调用method2() ,那么JVM永远不会加载类A. Additionally, contrary to popular belief, permgen can be garbage collected, and if space gets low, and there are no instances on the heap referring to class A anymore, then class A can be removed from permgen. 另外,与流行的看法相反,permgen 可以被垃圾收集,并且如果空间变低,并且堆上没有实例引用class A类,则可以从permgen中移除A类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM