Where can I find the program MemoryConsumer.java for checking memory consumption?

Where can I find the program MemoryConsumer.java for checking memory consumption? I know that such a thing already exists, since I see links through Google to such things. For example, this Oracle site belongs to "ConsumeHeap.java", but I do not know where to find this source code.

hotspot 1.6 parameters

Does anyone know where to find or how to create such a thing?

+3
source share
4 answers

You can simply create a huge number of instances of objects and save them in scope.

ArrayList<SomeObject> listOfObjects = new ArrayList<SomeObject>;
for (int i = 0; i < aBigNumber; i++) {
  listOfObjects.add(new SomeObject());
}
+2
source

I used this class ObjectSizerfor a good effect:

http://www.javapractices.com/topic/TopicAction.do?Id=83

, , .

+2

:

public class ConsumeHeap {
    public static void main(String[] args) {
        int[] a = new int[2000000000];
    }
}

OutOfMemoryError 32- . , 16 * 10 ^ 18 :

public class ConsumeHeap {
    public static void main(String[] args) {
        int[][] a = new int[2000000000][2000000000];
    }
}
+1

, . / java.lang.Runtime.getRuntime();

, . .

package test;

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 a 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));
  }
} 
0

Source: https://habr.com/ru/post/1790055/


All Articles