如何在不使用任何工具的情况下在Java应用程序中查找活动对象的数量?

如何在不使用任何工具的情况下在Java应用程序中查找活动对象的数量?,java,memory-management,memory-leaks,garbage-collection,out-of-memory,Java,Memory Management,Memory Leaks,Garbage Collection,Out Of Memory,在运行中的应用程序中,是否有方法在任何时间点查找类的活动对象数?所谓活动/活动对象,我指的是那些不符合垃圾收集条件的对象。有没有办法不用任何工具找到它 假设整个应用程序都是个人编写的。因此,课程可以根据我们的需要定制。另外,假设我们要查找其活动实例计数的类是用户定义的类,而不是任何内置类 简单的答案是否定的-没有简单的类或方法调用来查找这些数据。然而,人们想出了很多方法。这取决于为什么需要数据和程序的结构。 关于这个话题,这里有很多很好的讨论。 试一试,看看哪个最适合你。是的 创建同步的基于类的

在运行中的应用程序中,是否有方法在任何时间点查找类的活动对象数?所谓活动/活动对象,我指的是那些不符合垃圾收集条件的对象。有没有办法不用任何工具找到它


假设整个应用程序都是个人编写的。因此,课程可以根据我们的需要定制。另外,假设我们要查找其活动实例计数的类是用户定义的类,而不是任何内置类

简单的答案是否定的-没有简单的类或方法调用来查找这些数据。然而,人们想出了很多方法。这取决于为什么需要数据和程序的结构。 关于这个话题,这里有很多很好的讨论。 试一试,看看哪个最适合你。

是的

创建同步的基于类的静态实例计数器

通过实例化的类方法中的一个或多个

然后u必须重写dispose方法以减少实例计数器

更新

这是一个模糊不清的班级。。可以用来追踪一些东西

package myclasses;

import java.util.Vector;

public class ClassA {

    private static int iCountInstances = 0;
    private static int iCountCleanups = 0;
    private static int iCountGCFinalize = 0;

    private String m_str1 = null;
    private Vector m_vct1 = null;


    public ClassA() {
        // bump the instance count
        incrementCountInstance();


    }

    private static synchronized void incrementCountInstance() {
        iCountInstances++;
    }

    private static synchronized void incrementCountCleanup() {
        iCountCleanups++;
    }

    private static synchronized void incrementGCFinalize() {
        iCountGCFinalize++;
    }

    /**
     * reportOut - you can change this up on how ever you like
     * 
     * an in control app in a perfect world will have all three counts THE SAME after a final 
     * GC and right before exist.
     * 
     * The True number of 'active' classes in an app is going to be
     *      ICountInstances - iCountGCFinalize.
     * 
     * The idea here is that if GC did not dispose of it.. its still in memory.. and still 
     * active.. even if your app thinks its no longer using it...
     * 
     * @return
     */
    public static String reportOut() {

        return "ClassA Counts: incnt:" + ClassA.iCountInstances +", clncnt:" + ClassA.iCountCleanups + ", gccnt:" + ClassA.iCountGCFinalize;
    }


    public void cleanup() {

        //
        // ok.. initialize all member variables here
        // do not worry about what other object refereneces this guy
        // you only care about what you have as member variables.
        // you only de-refrence what you point to .. 
        // if every class took care of what it referenced.. then all is well.
        // so.. clean up your object and help GC ...

        this.setM_str1(null);
        this.getM_vct1().removeAllElements();

        ClassA.incrementCountCleanup(); // Increment the cleanup count..

        //
        // feel free to write to a logger reporting out that programmer has cleaned up this instance..
        //

    }

    @Override
    protected void finalize() throws Throwable
    {
        // Incrementing means GC determined this guy is truly an Object Orphan and has been 
        // completely de-referenced.

         ClassA.incrementGCFinalize();

        //
        // feel free to write to a logger reporting out that GC is removing this instance..
        //


    }
    public String getM_str1() {
        return m_str1;
    }

    public void setM_str1(String m_str1) {
        this.m_str1 = m_str1;
    }


    public void setM_vct1(Vector m_vct1) {
        this.m_vct1 = m_vct1;
    }

    public Vector getM_vct1() {
        return m_vct1;
    }
}
下面是另一个类,可以用来帮助报告执行期间发生的事情。。等等

package myclasses;

public final class CheckCounts {


    // No create instance allowed..
    private CheckCounts() {

    }

    /**
     * Report out on interesting counts...
     */
    public static void reportOut() {

        /// Add all the reportouts here..
        System.out.println(ClassA.reportOut());


    }

}
你可以想象一下,创建一个后台线程监视器,它可以简单地报告你想要跟踪的类的统计信息。。并让它每隔30秒左右写入一次记录器

注意,我把所有的东西都数了起来。您可以使用数学来查看您的代码在自我清理方面的效果。。当你清理一个物体时。。您希望取消引用该对象所指向的内容,并清除任何列表、数组、哈希映射等。但要小心,不要发疯,并开始清理位于类的向量中的对象-只需清理向量本身


试试看。。它很容易实现。。它可以帮助您查看运行时环境中正在发生的事情,而不是您认为正在发生的事情,只需查看您的代码..

而不使用任何工具?不,如果你需要知道,你有一些架构问题。你为什么认为你需要知道?@b只是因为这是一个面试问题。即使经过几天的搜索,我也找不到任何合适的答案。作为一个面试问题也意味着它可以用几种方式来回答。但是我认为面试官想要的是有一种解释的方式,在这种方式中,应用程序可以被编码以实现目标(不使用任何额外的工具)。@DaveTheRave:override dispose method of which class?@VinceEmigh using finalize method似乎不符合逻辑,因为该方法只在垃圾收集时运行。不是吗?这限制了我们能够找到活物体数量的时间点。上述观察是正确的。我已经按照我的建议做了。只有我设计的课程。不是每个班级都有。我想知道程序运行时有多少类挂起。我们所有的类代码都有一个清理方法。开发人员确保在完成其使用时调用它。今天晚些时候我可以举出一个例子。我正在用我的iPhone去吃午饭。。这里的目标是确保对象在使用后确实从内存中消失。。Vs由于一些未知的引用而挂起。。例如运行你的应用程序。Thrn在最终关闭之前强制垃圾收集。然后转储实例计数,看看它们是否真的为零。它会告诉你很多关于你在用一些代码清理appupdated答案中的引用的情况。可能需要清理或修改以满足您的需要。有很多方法可以实现同步。。决定什么最适合你