[英]How do I figure out that a particular object in an application is no longer in use?
How could I figure out that an object(or objects of a class) is/are not in use and ready to be collected by GC. 我怎么能弄清楚一个对象(或一个类的对象)没有被使用并且准备被GC收集。 or how could i get to know that object has no reference while the application is running. 或在应用程序运行时如何得知该对象没有引用。 (before it gets GCed) (在被GC之前)
I assume you mean detecting an object is no longer in use at run time, rather than something you can check staticly. 我假设您的意思是检测到对象在运行时不再使用,而不是可以静态检查的对象。
The simplest way to be notified that an object is about to be GCed is to override the finalize() method. 通知对象即将被GC的最简单方法是覆盖finalize()方法。 Note: you have to be careful what you do in this method. 注意:您必须谨慎使用此方法。 eg It is single threaded and blocking will result in no objects being cleaned up. 例如,它是单线程的,阻塞将导致没有对象被清除。
Another approach is to use Weak or Soft References and monitor a ReferenceQueue. 另一种方法是使用弱引用或软引用并监视ReferenceQueue。 This is a way to monitor when an object has been detected as available to be cleaned up. 这是一种监视何时检测到对象可清理的方法。 See the source for WeakHashMap for an example. 有关示例,请参见WeakHashMap的源。
Note: there is no simple way to detect an object is no longer in use without a GC, and if you don't have a GC for a long time, you have no way of knowing in the meantime. 注意:没有简单的方法来检测没有GC就不再使用的对象,如果很长一段时间没有GC,您将无法同时知道。
在Eclipse中,您可以右键单击您的类,然后选择“ References > Project
I use the UCDetector (Unnecessary Code Detector) Eclipse plugin. 我使用UCDetector(不必要的代码检测器) Eclipse插件。 It will show you public classes, methods or fields which have no references, and allow you to delete them easily. 它将向您显示没有引用的公共类,方法或字段,并允许您轻松删除它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.