限制 Java 堆空间,以便全局玩框架

2022-09-01 08:17:40

我有一个非常旧的Linux系统,并安装了java和play框架。当我运行java时,我得到:

java -version
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

所以我限制了 application.conf 中的 java 堆空间:

jvm.memory=-Xmx256M -Xms256M

有了这个设置,我可以运行播放测试,播放运行等...

但我不能运行:

play dependencies 
~        _            _ 
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/   
~
~ play! 1.2.1, http://www.playframework.org
~
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

是否有全局配置文件或环境变量,我可以在其中限制java堆空间全局以用于Play框架?

更新:此外,以下内容不起作用:

play dependencies -Xmx256M -Xms256M
~        _            _ 
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/   
~
~ play! 1.2.1, http://www.playframework.org
~
Error occurred during initialization of VM
Could not reserve enough space for object heap
Could not create the Java virtual machine.

更新 2

记忆:

ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
file size               (blocks, -f) unlimited
pending signals                 (-i) 1024
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 38912
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

限制:

cat /proc/meminfo 
MemTotal:      4139312 kB
MemFree:        332988 kB
Buffers:        105252 kB
Cached:        1705644 kB
SwapCached:          4 kB
Active:        2566216 kB
Inactive:       625032 kB
HighTotal:      786432 kB
HighFree:         1728 kB
LowTotal:      3352880 kB
LowFree:        331260 kB
SwapTotal:     4192956 kB
SwapFree:      4168224 kB
Dirty:             368 kB
Writeback:           0 kB
Mapped:        1672180 kB
Slab:           570864 kB
CommitLimit:   6262612 kB
Committed_AS:  4075144 kB
PageTables:      19884 kB
VmallocTotal:   303096 kB
VmallocUsed:     10400 kB
VmallocChunk:   292648 kB

奥拉

雷内


答案 1

Play似乎没有选择依赖项的jvm.memory设置,甚至没有测试命令。强制它使用特定 JVM 设置的一种方法是使用 _JAVA_OPTIONS。

例如:

export _JAVA_OPTIONS="-Xms800m -Xmx1500m -XX:PermSize=64m -XX:MaxPermSize=256m"
play test

play deps

你应该看到

~        _            _ 
~  _ __ | | __ _ _  _| |
~ | '_ \| |/ _' | || |_|
~ |  __/|_|\____|\__ (_)
~ |_|            |__/   
~
~ play! 1.2.3, http://www.playframework.org
~ framework ID is test
~
~ Running in test mode
~ Ctrl+C to stop
~ 
Picked up _JAVA_OPTIONS: -Xms800m -Xmx1500m -XX:PermSize=64m -XX:MaxPermSize=256m
Listening for transport dt_socket at address: 8000

请注意,这会将这些设置应用于在设置了_JAVA_OPTIONS的终端上运行的所有 java 程序。


答案 2

在谷歌搜索了更多之后,我发现了这个讨论。问题是,我的Linux系统在openvz容器中运行:

Java抱怨的原因是,在启动时,它看到机器有超过2 GB的RAM,因此它以服务器模式启动,该模式尝试分配所有内存,然后失败,因为它位于VPS内部。

我可以通过更改 /usr/java/jdk1.6.0_26/jre/lib/i386/jvm.cfg来解决 java 启动问题:

-client IF_SERVER_CLASS -server
-server KNOWN
-hotspot ALIASED_TO -client
-classic WARN
-native ERROR
-green ERROR

自:

#-client IF_SERVER_CLASS -server
-client KNOWN
-server KNOWN
-hotspot ALIASED_TO -client
-classic WARN
-native ERROR
-green ERROR

现在我可以运行任何播放命令。也许这有助于其他人遇到与基于容器的虚拟化相关的类似问题。

BR, 雷内


推荐