finalize()如何在java中工作? [英] How does finalize() work in java?

查看:172
本文介绍了finalize()如何在java中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我最近发现了Java中的finalize方法(不知道为什么我之前错过了它,但它在那里)。 似乎可能是我正在处理的许多问题的答案,但我希望先获得更多信息。



在线上,我发现这个图表说明了垃圾收集和定稿的过程:


几个问题:


  1. 这发生在单独的线程中,是否正确?

  2. 如果在finalize期间实例化新对象,会发生什么情况?是否允许?

  3. 如果我从finalize调用静态方法会发生什么情况?

  4. 如果我建立对象的新引用,会发生什么?在最终确定?






我想我应该解释我为什么感兴趣。我使用LWJGL很多,看来如果我可以使用finalize来使Java对象自动清理OpenGL资源,那么我可以在API方面做一些非常好的事情。

解决方案

我不认为有什么线程将被使用的任何保证。新对象可能会被实例化,并且可能会调用静态方法。建立对你的对象的新引用将阻止它被垃圾回收,但是 finalize 方法不会被再次调用 - 你不需要



清理资源正是 finalize 方法的用处,所以你应该很好那里。但是有一些警告:

该方法不保证被调用。 如果您绑定的资源在程序停止时不会自动释放不依赖于 finalize



调用该方法时不能保证。记忆力紧张,这将更快。有了大量的可用内存,它将会在稍后。这可能适合你:有很多内存,你可能并不关心释放资源。 (虽然挂在它们上面可能会干扰同时运行的其他软件,在这种情况下,你会担心的。)



我的ususal解决方案是使用某种 dispose 方法进行清理。如果可以的话,我会在某些时候明确地称呼它,并且尽可能早。然后我添加一个只调用了 dispose 方法的finalize方法。 (请注意,当多次调用时, dispose 方法必须表现良好!实际上,通过这种编程,我可能会多次调用 dispose finalize 之外,不确定以前的调用是否成功,但是希望尽快有效地调用它。)现在,理想情况下,我的资源尽快释放我不再需要它们。但是,如果我忘记了资源的对象,当内存不足时, finalize 方法会让我失望,我需要帮助。


So, I recently discovered the finalize method in Java (not sure why I missed it before, but there it is). This seems like it could be the answer to a lot of the issues I'm working with, but I wanted to get a bit more information first.

Online, I found this diagram illustrating the process of garbage collection and finalize:

A couple of questions:

  1. This takes place in a separate thread, correct?
  2. What happens if I instantiate a new object during finalize? Is that allowed?
  3. What happens if I call on a static method from finalize?
  4. What happens if I establish a new reference to the object from within finalize?


I suppose I should explain why I'm interested. I work with LWJGL a lot, and it seems that if I could use finalize to cause Java objects to automatically clean up OpenGL resources, then I could do some really nice things in terms of an API.

解决方案

I don't think there are any guarantees about what thread will be used. New objects may be instantiated and static methods may be called. Establishing a new reference to your object will prevent it from being garbage collected, but the finalize method will not be called again--you don't want to do this.

Cleaning up resources is precisely what the finalize method is for, so you should be good there. A couple of warnings, though:

The method is not guaranteed to be called. If you have tied up resources that will not automatically be freed when your program stops do not depend on finalize.

When the method is called is not guaranteed. With memory tight, this will be sooner. With lots of free memory, it will be later if at all. This may suit you fine: with lots of memory you may not be all that concerned about freeing up the resources. (Though hanging on to them may interfere with other software running at the same time, in which case you would be concerned.)

My ususal solution is to have some kind of dispose method that does the clean up. I call it explicitly at some point if I can, and as soon as I can. Then I add a finalize method that just calls the dispose method. (Note that the dispose method must behave well when when called more than once! Indeed, with this kind of programming I might call dispose several times outside finalize, not being sure if previous calls were made successfully and yet wanting it to be called effectively as soon as possible.) Now, ideally, my resources are freed as soon as I no longer need them. However, if I lose track of the object with the resources, the finalize method will bail me out when memory runs short and I need the help.

这篇关于finalize()如何在java中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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