简体   繁体   English

java类驻留在内存中的时间

[英]How much time a java class resides in memory

I am thinking about how much a class resides in memory in case it is not being accessed by other objects in memory? 我正在考虑一个类驻留在内存中的多少,以防内存中的其他对象没有访问它?

for example suppose I have some class like this: 例如,假设我有一些这样的类:

public class OrderNumber {
 private static long counter = 0;

 public static long getOrderNumber(){
          if (counter >= 100) {
            return counter = 1;
          }
          return ++counter;
 }
}

And I call its static method from another class: 我从另一个类调用它的静态方法:

long number = OrderNumber.getOrderNumber(); 

each time I call it, it returns an incremental number, 1, 2, 3, 4, ... 每次我调用它,它都会返回一个增量数字,1,2,3,4 ......

So, My question is What is the probability of this method to return the initial value where it is supposed to return the sequenced value ?? 所以,我的问题是这个方法返回初始值的概率是多少,它应该返回序列值?

Right now, the answer is forever. 现在,答案是永远的。 Class definitions are put into the permanent generation , and remain there until the program terminates. 类定义被放入永久代 ,并保持在那里直到程序终止。

Note, this is a problem for dynamic languages on the JVM, like JRuby, which create classes on the fly, because these classes waste space. 注意,这是JVM上的动态语言的问题,如JRuby,它可以动态创建类,因为这些类浪费了空间。 I believe there are changes in the works that will resolve this problem - for example, the G1 collector 我相信可以解决这个问题的工作有所改变 - 例如G1收藏家

It is defined that there is effectively a strong reference from a class to its class loader instance, and from a class loader instance to every class it has ever loaded. 定义了从类到其类加载器实例,从类加载器实例到它曾经加载的每个类的有效引用。 So a class can only be unloaded at the same time as a class loader (give or take the odd finaliser resurrection trick). 所以一个类只能在类加载器的同时卸载(给出或采取奇怪的终结者复活技巧)。 Every class loaded by a class loader becomes eligible for collectible at the same time. 由类加载器加载的每个类都有资格同时收集。

In practice class data lives in the "permanent generation" (in HotSpot at least). 在实践中,类数据存在于“永久代”中(至少在HotSpot中)。 This is typically garbage collected less frequently than the main heap, as it should have less churn. 这通常比主堆更少地收集垃圾,因为它应该有更少的流失。 It's also not uncommon for class loaders to "leak". 类加载器“泄漏”的情况并不少见。 Most applications have code that last until the process is restarted. 大多数应用程序的代码都会持续到重新启动进程。 Development app servers will often reload "apps" whilst maintaining the core server/container and some library code. 开发应用服务器通常会重新加载“应用”,同时维护核心服务器/容器和一些库代码。

IIRC, JDK 1.1 unloaded individual classes, which caused problems (Java 2 came out in 1998). IIRC,JDK 1.1卸载个别类,这引起了问题(Java 2于1998年问世)。

The java documentation indeed says forever: http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html java文档确实永远地说: http//java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

They are associated with the class, rather than with any object. 它们与类相关联,而不是与任何对象相关联。 Every instance of the class shares a class variable, which is in one fixed location in memory 该类的每个实例共享一个类变量,该变量位于内存中的一个固定位置

There's lots of information on class loading. 有关类加载的大量信息。 Here, for example, is one article: http://www.ibm.com/developerworks/java/library/j-dyn0429/ 例如,这里有一篇文章: http//www.ibm.com/developerworks/java/library/j-dyn0429/

The rules for loading classes are spelled out in detail in the JVM specification. 加载类的规则在JVM规范中详细说明。 The basic principle is that classes are only loaded when needed (or at least appear to be loaded this way -- the JVM has some flexibility in the actual loading, but must maintain a fixed sequence of class initialization). 基本原则是类只在需要时加载(或者至少看起来像这样加载 - JVM在实际加载中具有一定的灵活性,但必须保持固定的类初始化序列)。 Each class that gets loaded may have other classes that it depends on, so the loading process is recursive. 每个加载的类可能都有其它依赖的类,因此加载过程是递归的。

Here's the JVM specification on loading. 这是关于加载的JVM规范。 http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html

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

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