test memory consumption of a java program with eclipse [closed]

喜夏-厌秋 提交于 2019-12-08 17:33:44

问题


Is there a plugin in eclipse that I could use to test who much memory has my just run program cost?

I am thinking there may be a button from the plugin after I run the program, I could click on it, and it shows me a graph of sort of the peak memory consumption of my program just now.


回答1:


I personally like VisualVM (tutorial), included with the latest JDK releases.




回答2:


I agree with Mr. Nobody that VisualVM is nice. The Eclipse Memory Analyzer has some nice features as well.




回答3:


The total used / free memory of a program can be obtained in the program via java.lang.Runtime.getRuntime();

The runtime has several methods which relate to the memory. The following coding example demonstrates its usage.

import java.util.ArrayList;
import java.util.List;

public class PerformanceTest {
  private static final long MEGABYTE = 1024L * 1024L;

  public static long bytesToMegabytes(long bytes) {
    return bytes / MEGABYTE;
  }

  public static void main(String[] args) {
    // I assume you will know how to create an object Person yourself...
    List<Person> list = new ArrayList<Person>();
    for (int i = 0; i <= 100000; i++) {
      list.add(new Person("Jim", "Knopf"));
    }
    // Get the Java runtime
    Runtime runtime = Runtime.getRuntime();
    // Run the garbage collector
    runtime.gc();
    // Calculate the used memory
    long memory = runtime.totalMemory() - runtime.freeMemory();
    System.out.println("Used memory is bytes: " + memory);
    System.out.println("Used memory is megabytes: "
        + bytesToMegabytes(memory));
  }
} 


来源:https://stackoverflow.com/questions/5869432/test-memory-consumption-of-a-java-program-with-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!