本文介绍了重写Object类的finalize()方法有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在java中如果我们想手动调用垃圾收集器,我们可以做System.gc()。
1.我们在被覆盖的内部完成的操作是什么finalize )方法?

2.是否需要手动调用JVM垃圾收集器来覆盖finalize()方法?

As far as I know , in java if we want to manually call the garbage collector we can do System.gc().
1.What are the operations that we do inside our overriden finalize() method?
2.Is it required to override the finalize() method if we want to manually call the JVM garbage collector?

推荐答案

手动分配空闲内存(通过某些本机调用),即不由GC管理。这是一个非常罕见的情况。
有些人还在那里放了一张支票,与对象连接的其他资源已经被释放 - 但它仅用于调试目的,并不是很可靠。

您必须记住: p>

Free memory alocated manually (through some native calls) i.e. not managed by GC. It is a really rare situation.Some people put there also a check, that other resources connected with the object have already been released - but it's only for debugging purposes and it's not very reliable.
You must remember that:


  1. finalize 是关于内存和内存的。

  2. 您无法控制它何时会被调用,甚至是是否调用
  1. finalize is about memory and only memory.
  2. You have no control when it will be invoked and even whether it be invoked.

永远不会放在那里释放任何其他资源(如文件句柄,锁)。所有这些资源都必须手动发布。

重写finalize与调用 System.gc()无关。

这篇关于重写Object类的finalize()方法有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 23:50