简体   繁体   English

如何在类和jvm中获取活动对象?

[英]How to get active object in class and in jvm?

hello all my question is if we have 10 object of class and three of them passed to garbage collector. 你好,我所有的问题是我们是否有10个类的对象,其中三个对象传递给了垃圾收集器。 now we have seven active object. 现在我们有七个活动对象。 how can we get number of active object in class and in jvm. 我们如何在类和jvm中获取活动对象的数量。

您可以在类中使用static计数器,并在构造函数中对其进行递增,并在终结器中对其进行递减,但是我不确定您为什么在乎。

You can not assume something like that if you do object= null there is no guarantee that JVM will do garbage collection. 您不能假设这样的事情,如果您执行object= null ,则不能保证JVM会进行垃圾回收。 But since you don't have reference now you will not able to access it.

In java you don't have to care about memory till your program exits with OutOfMemoryException but standard programming practices still apply :) 在Java中,您不必关心内存,直到您的程序因OutOfMemoryException退出而已,但标准编程实践仍然适用:)

You can read more about The Reachability Lifecycle of Objects 您可以阅读有关对象的可达性生命周期的更多信息

You can use profiler to see active object count eg JProfiler 您可以使用探查器查看活动对象计数,例如JProfiler

As a solution you can use Object Pool Pattern But it is a anti-pattern 作为解决方案,您可以使用对象池模式,但这是一种反模式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM