我的程序使用Java有多少内存

Sum*_*khe 5 java memory time artificial-intelligence

我制作了一个程序,它使用A*搜索算法来解决8个游戏难题.我有兴趣看到我的程序从头到尾使用了多少内存.

到目前为止我已经完成了

在该计划的开始

static double totalMem = Runtime.getRuntime().totalMemory()/(1024*1024);
static double memoryMax = Runtime.getRuntime().maxMemory()/(1024*1024);
Run Code Online (Sandbox Code Playgroud)

并在程序结束时

        timeTaken-=System.currentTimeMillis();

        System.out.println("\n\n-------Total Time Taken = "+Math.abs(timeTaken)+
                " millisec ------\n ");

        System.out.println("-------Maximum Memory  = "+memoryMax+" MB------\n ");

        System.out.println("-------Total Memory = "+totalMem
                +" MB------\n ");

        currMem = Runtime.getRuntime().freeMemory()/(1024*1024);

        System.out.println("-------Free Memory  = "+currMem+" MB------\n ");

        double memUsed = (Runtime.getRuntime().totalMemory())/(1024*1024)-currMem;

        System.out.println("-------Total Used = "+memUsed
                +" MB------\n ");
Run Code Online (Sandbox Code Playgroud)

这似乎不对.当我用不同的数据集测试时.任何sugessitions

And*_*nin 6

为此目的使用分析器或类似软件会更好.您可以从jvisualvm开始,它包含在JDK或JProfiler中.