简体   繁体   English

如何确定应用程序中的特定对象已不再使用?

[英]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.

相关问题 如何找出Java中对象的标识符? - How do I figure out the identifier of an object in java? 尝试使用Gson的toJson方法时,如何确定将哪些字段标记为瞬态? - How do I figure out what fields to mark as transient when tryhing to use Gson's toJson method? 根据教授需要弄清楚如何操纵变异子以找出一个类,我对此感到困惑 - need to figure out how to manipulate the mutator to figure out a class according to professor, I am confusd on how to do that 如何判断一个对象是否是向量 - how to figure out if an object is a vector 如何确定我在哪里单击 JFrame? - How do I figure out where I am clicking on a JFrame? 我怎么弄清楚ehcache使用了多少内存? - How do I figure out how much memory by ehcache is using? 如何使用反射从休眠对象中获取数据? - How do I use reflection to get data out of a hibernate object? 我不知道如何使用comboBox删除单选按钮 - I can''t figure out how to use the comboBox to remove radiobuttons 我无法弄清楚如何使用Android PreferenceActivity - I can't figure out how to use an android PreferenceActivity 我无法弄清楚如何使用java gui / layouts? - I cant figure out how to use java gui / layouts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM