现在的位置: 首页 > 综合 > 正文

java -Xmxn 虚拟机内存使用上限的上限

2019年08月09日 ⁄ 综合 ⁄ 共 1687字 ⁄ 字号 评论关闭

设计开发软件,对于运行期系统资源的使用需要精打细算。java 程序在引入开发简易性的同时,也消耗了大量的系统资源。在开发 server 端程序时需要根据系统特点进行优化。

最近的一个“自有平台” (java) 上开发的 web 项目需要处理大量的数据。
由于时间紧任务重(一贯如此),没有充分的时间优化代码(如此一贯),造成了几个简单的功能,诸如:上传文件、数据库查询结果转存等操作巨费内存。
所以就从简单的手工设置划拨内存的方向解决问题。大家都知道 java 命令的 -Xmx n 参数 [1] 可以设置虚拟机可以使用的内存上限,默认值为 64M 。
由于是开发 server 端项目,按照以往的经验,开始时设置到 -Xmx 512M 。遇到内存溢出异常 OutOfMemoryError 。先是设置成于 1024M ,看来是程序太差,还是溢出。
于是想当然的就 -Xmx 2048M ,结果 vm 直接没法启动。
原来,虚拟内存的设置还是有上限的。而这个东西在 java 文档上并没有特别的指出。
通过一番艰苦卓绝的 G 路学习,得到一个大概地结论:在 32 位 windows 系统上,基于 sun 的虚拟机,基本就是 1.3 G [2] 是个比较合理的上限的样子。

首先,作为 32 位 windows 系统,2G 的用户内存寻址空间,是个上限。然后刨去其他 Object Heap 之类的开销,大概是 1.3 G 剩余可用。

从前,一直看 windows 版的参数说明[1]。其中,到目前为止,都没有提及这个事儿。今天看了 solaris 版的才知道,原来还是有说到一点点地。

On Solaris 7 and Solaris 8 SPARC platforms, the upper limit for this value is approximately 4000m minus overhead amounts. On Solaris 2.6 and x86 platforms, the upper limit is approximately 2000m minus overhead amounts. On Linux platforms, the upper limit is approximately 2000m minus overhead amounts. 

毕竟,sun 的 jvm 是免费的啦。大多数应用应该是够了。其他的 jvm 因为没了解,所以无从说起了。也许,差不了太多的。

 

看来,内存使用效率的问题,仍旧是程序员的问题。

 

参考:

[1]
java - the Java application launcher
http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html
http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/java.html

[2]
http://www.unixville.com/~moazam/

Why can't I allocate 2GB of heap to the JVM on Windows?
http://www.unixville.com/~moazam/2004/06/03.html
Why can't I allocate 2GB of heap to the JVM on Windows, Part 2
http://www.unixville.com/~moazam/2005/03/13.html
 

[3]
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4435069
Hotspot on linux is restricted to a 2GB heap
06-APR-2001

[4]
http://www.velocityreviews.com/forums/t132683-which-os-can-i-use-for-a-heap-larger-than-2g-linux-failed.html
Java - which OS can I use for a heap larger than 2G (linux failed)

 

抱歉!评论已关闭.