Obtaining memory available to JVM at runtime

前端 未结 4 1843
北恋
北恋 2020-12-21 10:01

I\'m trying to sort a bunch of data such that that the size of data input to the program can be larger than the memory available to the JVM, and handling that requires exter

4条回答
  •  再見小時候
    2020-12-21 10:51

    Check out these methods on the java.lang.Runtime class:

    freeMemory

    totalMemory

    maxMemory

    Example

    Runtime rt = Runtime.getRuntime();
    System.err.println(String.format("Free: %d bytes, Total: %d bytes, Max: %d bytes",
      rt.freeMemory(), rt.totalMemory(), rt.maxMemory()));
    

    Also note that if the total amount of memory is being exhausted you can always start the JVM with a larger amount of heap allocated using the -Xmx JVM argument; e.g.

    java -Xmx256M MyClass
    

提交回复
热议问题