Class 对象什么时候被垃圾回收?

发布于 2024-12-03 06:15:02 字数 730 浏览 0 评论 0 原文

我想知道是否有人可以告诉我 Java Class 对象何时被垃圾收集。我的用例是一个缓存(Map, Class[]>),它保存对象的类层次结构。

例如:

String.class 的(短)层次结构将是(降序):String.class -> Object.class。此类型的有效缓存条目为 [KEY: String.class, VALUE: {String.class, Object.class}]

我猜 String.class 是一个坏例子,因为 String.class 应该被垃圾收集......

我正在处理的序列化项目需要这个缓存。编写对象时,我的系统需要该对象的层次结构来选择正确的“编解码器(序列化器)”。收集每个对象的层次结构会导致一些不必要的开销。但后来我想到了内存泄漏。也许类对象可以被垃圾收集(我不知道),这在我的缓存中使用强引用时不起作用。

你认为 WeakHashMap 就足够了吗?或者我必须使用类似这样的内容:

Map<WeakReference<Class<?>>, WeakReference<Class<?>>[]> ?

您对这个问题有何看法?

I was wondering if somebody could tell me when a Java Class object gets garbage collected. My use case is a cache (Map<Class<?>, Class<?>[]>) which holds the class-hierarchy of objects.

For instance:

The (short) hierarchy of String.class would be (descending): String.class -> Object.class. A valid cache-entry for this type would be [KEY: String.class, VALUE: {String.class, Object.class}].

I guess String.class is a bad example since the String.class should be garbage-collected....

I need this cache for a serialization project I'm working on. When writing an object my system needs the hierarchy of this object for choosing the correct "Codecs (Serializers)". Collecting the hierarchy for each object would cause some overhead which is not necessary. But then I though about memory-leaks. Probably class-objects can be garbage-collected (Which i don't know) which would not work when using strong-references in my cache.

Do you think a WeakHashMap would be enough? Or do I have to use something like:

Map<WeakReference<Class<?>>, WeakReference<Class<?>>[]> ?

What do you think about this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

删除会话 2024-12-10 06:15:02

在不深入了解 Java 中类垃圾收集的细节的情况下,我认为您根本不需要担心:不需要收集类对象本身来防止内存泄漏来完成您想要的事情。毕竟,java.lang.String 只会有一个 Class 实例。因此,如果将对 String.class 的引用放入 Map (或任何其他数据结构)中,则不会创建 String 类的新实例,而只是创建一个引用到现有的。

一旦您的 Map 超出范围,整个 Map 将有资格进行垃圾回收。

Without getting into the details of Class garbage collection in Java, I don't think you need to worry at all: The Class Objects themselves don't need to be collected to prevent a memory leak to accomplish what you want. After all, there will only ever be a single Class instance for java.lang.String. So if you put a reference to String.class into a Map (or any other data structure) you aren't creating a new instance of the String class, just a reference to the existing one.

As soon as your Map goes out of scope, the entire Map will be eligible for garbage collection.

池木 2024-12-10 06:15:02

类与其类加载器同时被垃圾收集。因此,如果您希望代码在存在瞬态类加载器的情况下工作,非强引用非常重要。

Map<WeakReference<Class<?>>, WeakReference<Class<?>>[]>

这几乎有效。您不能拥有泛型类型的数组。我建议改用List

这是一种实习而不是缓存。在这种情况下,这并不重要,因为与类相比,每个键使用的内存量很小。如果您确实想要一个缓存,则需要使用 ReferenceQueue 进行驱逐,例如:(

Map<SoftReference<WeakReference<Class<?>>>, List<WeakReference<Class<?>>>>

如果您愿意,可以通过引入可用于键的类[具有行为]来清理角度和价值。)

Classes are garbage collected at the same time as their class loader. So non-strong references are important if you want your code to work in the presence of transient class loaders.

Map<WeakReference<Class<?>>, WeakReference<Class<?>>[]>

This almost works. You can't have an array of a generic type. I suggest using a List instead.

This is kind of interning rather the caching. It shouldn't really matter in this case as the amount of memory used per key is tiny in comparison to the class. If you really wanted a cache you would need to use ReferenceQueue for eviction and something like:

Map<SoftReference<WeakReference<Class<?>>>, List<WeakReference<Class<?>>>>

(If you want, the angles could be cleaned up by introducing classes [with behaviour] that can be used for the key and value.)

¢蛋碎的人ぎ生 2024-12-10 06:15:02

即使调用垃圾收集器 (Runtime.getRuntime().gc();),您也无法知道对象被垃圾收集的确切时间。

此外,使用 Wea​​kHashMap 进行缓存通常是一个糟糕的选择,因为弱引用仅用于键而不用于值。只要谷歌,你就会发现很多有用的文章,例如

最后,您应该检查是否真的需要缓存(例如使用像 JVisualVM 这样的探查器),因为您使用的是 String 和 Class 对象......

You can't tell the exact time an object is garbage collected, not even when calling the garbage collector (Runtime.getRuntime().gc();).

Furthermore, using WeakHashMap for caching is usually a bad choice since weak references are used only for the keys and not for the values. Just google, you'll find a lot of helpful articles, e.g.

Finally, you should check whether you really need caching (e.g. with a profiler like JVisualVM) since you are using String and Class objects...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文