java - 了解 Java 中的内存泄漏。在 Java 应用程序的整个生命周期中持续存在的类有哪些?

标签 java memory-leaks

从此IBM article on handling memory leaks in Java :

The garbage collector starts at the root nodes, classes that persist throughout the life of a Java application, and sweeps through all of the nodes that are referenced. As it traverses the nodes, it keeps track of which objects are actively being referenced. Any classes that are no longer being referenced are then eligible to be garbage collected. The memory resources used by these objects can be returned to the Java virtual machine (JVM) when the objects are deleted.

在 Java 应用程序的生命周期中持续存在的类有哪些?

最佳答案

您所问的也称为垃圾收集根。来自 Yourkit's documentation of GC roots :

There are several kinds of GC roots. One object can belong to more than one kind of root. The root kinds are:

Class - class loaded by system class loader. Such classes can never be unloaded. They can hold objects via static fields. Please note that classes loaded by custom class loaders are not roots, unless corresponding instances of java.lang.Class happen to be roots of other kind(s).

Thread - live thread

Stack Local - local variable or parameter of Java method

JNI Local - local variable or parameter of JNI method

JNI Global - global JNI reference

Monitor Used - objects used as a monitor for synchronization

Held by JVM - objects held from garbage collection by JVM for its purposes. Actually the list of such objects depends on JVM implementation. Possible known cases are: the system class loader, a few important exception classes which the JVM knows about, a few pre-allocated objects for exception handling, and custom class loaders when they are in the process of loading classes. Unfortunately, JVM provides absolutely no additional detail for such objects. Thus it is up to the analyst to decide to which case a certain "Held by JVM" belongs.

您也可以从 The Truth About Garbage Collection 阅读(已注明日期)。

关于java - 了解 Java 中的内存泄漏。在 Java 应用程序的整个生命周期中持续存在的类有哪些?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12242904/

相关文章:

c++ - 如何在设计实现层面避免内存泄漏

java - 如何用不同的方案创造更多的怪物

java - 如何从我的搜索查询结果中排除转推选项

java - MyBatis 生成器 setDistinct(true)

android - Activity 泄露了最初添加在这里的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@44f72ff0

C++ 是内存泄漏吗?指向在另一个函数中动态分配的数组的指针

java - 从 Android 调用 Soap Web 服务

java - java中如何防止SQL二阶注入(inject)(Spring应用)

c++ - 调用函数并使用 goto : memory leak? 将其转义

android - Sms BroadCastReceiver 泄漏内存