动态增加java堆空间
我编写了一个 java 程序,用于测试具有不同数量处理器的不同机器上的几个多线程算法的速度。
在某些机器上,合并排序*会失败,因为它需要相当大的堆空间才能处理非常大的数组。我可以在运行程序之前轻松地自己更改 java 堆空间,但我觉得更健壮且简单的方法是从程序本身内部完成此任务。
有没有办法在 java 程序运行过程中从虚拟机请求/获得更多堆空间?
注意:我确实知道我可以使用类似 java -Xmx1g Program
的脚本;我对这个主题的好奇心部分是出于学术目的。
*我的实现不会内联合并。它需要 O(n) 额外的内存。
I have written a java program that tests the speed of a couple of multi-threading algorithms on different machines with various numbers of processors.
On some machines, merge sort* fails because it requires a sizable heap space to work on very large arrays. I can easily change the java heap space myself before running the program, but I feel like a more robust and easy approach would be to do this task from within the program itself.
Is there a way to request/achieve more heap space from the virtual machine during the course of a java program?
Note: I do understand that I could execute the program with a script like java -Xmx1g Program
; my curiosity on this subject is in part academic.
*My implementation does NOT merge in-line. It requires O(n) extra memory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
据我所知,没有办法在运行时控制堆大小。
但这可能不是必需的:您可以分别使用 -Xms 和 -Xmx 开关提供最小和最大堆大小。 (例如 -Xms128m -Xmx512m)jvm 将管理这些范围内的实际堆大小。
As far as I know, there is no way of controlling the heap size at runtime.
It may not be necessary though: you can provide a minimum and maximum heap size with the -Xms and -Xmx switches respectively. (eg -Xms128m -Xmx512m) The jvm will manage the actual heap size within these bounds.
Java 的设计并不是为了能够动态管理内存,在本例中是“java 堆空间”,相反,它的设计目的是为了让程序员不必担心这一点。
简而言之,我不敢说 Java 中没有像
"malloc()"
或"setHeapSize(int byes)"
这样的东西。在 Java 上,当程序启动时,您会受到 JVM 可用的内存量的限制。就内存管理而言,这既是福也是祸。
对于这种动态内存分配,您应该尝试使用
C
和/或C++
等语言来实现您的算法。Java was not design to be able to dynamically manage memory, in this case "java heap space", all the opposite, it was designed in order to relieve the programmer from having to worry about that.
In short, I'm afraid to say that there is nothing like a
"malloc()"
or"setHeapSize(int byes)"
in Java.On Java you're constraint to the amout of memory available to the JVM when your program starts. In terms of memory management this is both a blessing and a curse.
For that kind of dynamic memory allocation you should try to use implement your algorithm using a language like
C
and/orC++
instead.最大值不是使用的内存大小,而是根据使用情况动态变化的。
最大堆大小应该是您宁愿程序失败也不愿使用更多内存的点。动态地改变这一点没有什么意义,即使是在学术上也是如此。
The maximum is not the size of memory used, this is dynamic based on usage.
The maximum heap size should be the point at which you would rather the program fail, than use more memory. It makes little sense to change this dynamically, even academicly.