Obtaining memory available to JVM at runtime

前端 未结 4 1824
北恋
北恋 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:48

    Using the methods of Runtime, as the others suggested, is fine, as long as you take some things into consideration:

    1) freeMemory() is a lower bound on the actual available memory, because memory that is unreferenced and ready for GC is considered as used. Running System.gc() before the call may return a more accurate result.

    2) totalMemory() can change - it only indicates the current total heap size, and the heap can expand/shrink by the JVM during runtime, depending on its usage. You can use maxMemory() to get the actual maximum.

提交回复
热议问题