获取运行时JVM可用的内存

获取运行时JVM可用的内存

本文介绍了获取运行时JVM可用的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对一堆数据进行排序,以便输入到程序的数据大小可以大于JVM可用的内存,并且处理需要外部排序,这比Quicksort慢得多。

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 external sort which is much slower than Quicksort.

有没有办法在运行时获取JVM可用的内存,这样我就可以尽可能地使用就地排序,只在数据输入太大时切换到Mergesort ?

Is there any way of obtaining memory available to the JVM at runtime such that I could use in place sorting as much as possible, and only switch to Mergesort when data input is too large?

推荐答案

在 class:

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

示例

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

另请注意,如果内存总量耗尽,您始终可以使用更大的内存启动JVM使用 -Xmx JVM参数分配的堆量;例如

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

这篇关于获取运行时JVM可用的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:52