如何使对象在对象内部变得符合垃圾回收的条件?

huangapple go评论94阅读模式
英文:

How to make an object eligible for garbage collection from inside the object?

问题

我正在寻找一种在Java中删除对象并使其可供垃圾回收的方法。我有一个Java类,需要调用其delete()方法。

public class SomeObj {

    // 一些实现细节
    //...

    void delete() {
        // 从程序中的某些列表中移除自身
        //...

        this = null; // <- 这行是非法的

        // delete this; <- 如果我在C++中,我可以这样做
    }
}

我应该如何做呢?显然,我需要重构我的代码,因为这似乎是糟糕的设计。

英文:

I'm looking for a way to delete an object in Java, make it eligible for GC. I have a Java class that needs to have its delete() method called.

public class SomeObj {

    // some implementation stuff
    //...

    void delete() {
        //remove yourself from some lists in the program
        //...

        this = null; // <- this line is illegal

        //delete this; <- if I was in C++, I could do this
    }
}

How should I do this? Apparently, I'm going to have to refactor my code because this smells like bad design.

答案1

得分: 1

Java是一种运行在垃圾回收环境中的语言,只要对象可以通过引用访问,它就在应用程序中存在。一旦它不再可访问 - 即没有其他对象持有对它的引用 - 就从应用程序的角度而言被视为“已删除”。

对象在堆中仍然有一些残余存在是垃圾收集器负责的事情,而不是应用程序。依赖于能够控制无引用对象的存在的应用程序,在某种逻辑意义上是有问题的。

希望将未引用对象从堆中永久移除的通常的半合理原因是为了节省堆空间。有许多许多情况下,我比垃圾收集器更清楚对象何时真正完成。在方法范围内存储临时结果的对象就是一个很好的例子。我主要是C和C++开发者,真希望java.lang.Object上有一个叫做ImDoneWithYouNow()的方法。可惜,它并不存在,我们必须依赖于垃圾回收实现来处理内存管理。

英文:

For better or worse, Java is a language that runs in a garbage-collecting environment. An object has some kind of existence in an application so longer as it is reachable via references. Once it is no longer reachable -- when no other object holds a reference to it -- it is "deleted" so far as the application is concerned.

That the object still has some after-life in the heap is a matter for the garbage collector, not the application. An application that depends on being able to control the existence of objects to which there are no references is broken in some logical sense.

The usual, semi-legitimate reason for wanting to nudge an unreferenced object out of the heap for good is to conserve heap space. There have been many, many occasions when I've known when an object is really finished with better than the garbage collector ever could. Objects that store temporary results with method scope are a good example. I'm primarily a C and C++ developer, and I really want a method on java.lang.Object called ImDoneWithYouNow(). Sadly, it doesn't exist, and we have to rely on the GC implementation to take care of memory management.

答案2

得分: 0

你不需要(实际上也不应该有)一个“析构函数”。一旦没有其他对象引用所涉及的对象,它就有资格进行垃圾回收,并且将在垃圾收集器认为合适时将其移除。

英文:

You don't need (and really shouldn't have) a "destructor". Once no other object references the object in question, it becomes eligible for garbage collection, and will be removed by the garbage collector when it sees fit.

huangapple
  • 本文由 发表于 2020年9月4日 21:08:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63741844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定