finalization

PhantomReference with null queue

冷暖自知 提交于 2019-11-27 08:19:52
问题 Java allow to write: new PhantomReference(new Object(), null) At this case new Object() will be collected? As I understand, phantom reference is alternative of finalize() method usage. And after appearing reference in queue, I need to do some additional actions and then run clear() java doc stays: It is possible to create a phantom reference with a null queue, but such a reference is completely useless: Its get method will always return null and, since it does not have a queue, it will never

What is the purpose of finalization in Java?

北战南征 提交于 2019-11-27 04:29:03
问题 My understanding of finalization is this: To clean up or reclaim the memory that an object occupies, the Garbage collector comes into action. (automatically is invoked?) The garbage collector then dereferences the object. Sometimes, there is no way for the garbage collector to access the object. Then finalize is invoked to do a final clean up processing after which the garbage collector can be invoked. Is this an accurate description of finalization? 回答1: The garbage collector is working

Should “Dispose” only be used for types containing unmanaged resources?

五迷三道 提交于 2019-11-26 15:52:01
问题 I was having a discussion with a colleague recently about the value of Dispose and types that implement IDisposable . I think there is value in implementing IDisposable for types that should clean up as soon as possible, even if there are no unmanaged resources to clean up . My colleague thinks differently; implementing IDisposable if you don't have any unmanaged resources isn't necessary as your type will eventually be garbage collected. My argument was that if you had an ADO.NET connection