垃圾收集器是否使用枚举类型运行? [英] Does Garbage Collector run on Enum type?

查看:111
本文介绍了垃圾收集器是否使用枚举类型运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 jls§8.9.2枚举身体声明


这是枚举声明的编译时错误宣布一个终结者。枚举类型的一个实例可能永远不会被最终确定。

It is a compile-time error for an enum declaration to declare a finalizer. An instance of an enum type may never be finalized.

由于finalizer在Garbage Collector运行之前执行,如果finalizer不存在意味着枚举类型始终保存在内存中,垃圾收集器不适用于枚举类型?

As finalizer executes just before Garbage Collector runs, if finalizer is not present does that means enum type always remains loaded in memory, and Garbage Collector is not applicable on enum type?

推荐答案

如果您编译一个枚举,如

If you compile an enum like

enum Suit {SPADES, HEARTS, CLUBS, DIAMONDS}

您将看到生成的字节码(即 javap -p Suit )对应于综合类:

You will see that the generated bytcodes (i.e. javap -p Suit) correspond to a synthetic class:

final class Suit extends java.lang.Enum<Suit> {
  public static final Suit SPADES;
  public static final Suit HEARTS;
  public static final Suit CLUBS;
  public static final Suit DIAMONDS;
  private static final Suit[] $VALUES;
  public static Suit[] values();
  public static Suit valueOf(java.lang.String);
  private Suit();
}

所以,枚举的实例是类本身的静态成员。然后,我认为这可能是垃圾回收的唯一方法是如果类本身是垃圾回收的,如果系统类加载器加载的话,这是非常不可能发生的。

So, instances of the enum are static members of the class itself. Then I think the only way in which this could be garbage collected would be if the class itself is garbage collected, which is very unlikely to happen if it was loaded by the system class loader.

这篇关于垃圾收集器是否使用枚举类型运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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