考虑以下两个命令
A:
$ java -Xms10G -Xmx10G myjavacode input.txt
B:
$ java -Xms5G -Xmx5G myjavacode input.txt
我有两个问题:
- 由于命令A使用其参数保留更多的内存,那么A比B运行得更快吗?
-Xmx
和-Xms
如何影响程序的运行过程和输出?
考虑以下两个命令
A:
$ java -Xms10G -Xmx10G myjavacode input.txt
B:
$ java -Xms5G -Xmx5G myjavacode input.txt
我有两个问题:
-Xmx
和-Xms
如何影响程序的运行过程和输出?-Xmx
参数定义了JVM的堆可以达到的最大内存大小。您必须熟悉您的程序,并查看它在负载下的表现,然后相应地设置此参数。如果值过低,可能会导致OutOfMemoryExceptions或者当程序的堆内存接近最大堆大小时,性能非常差。如果您的程序正在专用服务器上运行,您可以将此参数设置更高,因为它不会影响其他程序。
-Xms
参数为JVM设置初始堆内存大小。这意味着当您启动程序时,JVM将立即分配此数量的内存。如果您的程序从一开始就会消耗大量堆内存,这很有用。这避免了JVM不断增加堆内存,从而提高了性能。如果您不确定此参数是否会对您有所帮助,请不要使用它。
总之,这是一个妥协,您必须仅基于您的程序的内存行为来决定。
这取决于你的Java程序使用的垃圾回收器。并行垃圾回收器在更大的内存设置下可能效果更好,但我对此不是专家。
一般来说,如果你有更大的内存,垃圾回收的频率就会越低,因为有很多空间可以存放垃圾。但是,当涉及到垃圾回收时,垃圾回收需要处理更多的内存,这反过来可能会更慢。
> C:\java -X
-Xmixed mixed mode execution (default)
-Xint interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by ;>
set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by ;>
append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by ;>
prepend in front of bootstrap class path
-Xnoclassgc disable class garbage collection
-Xincgc enable incremental garbage collection
-Xloggc:<file> log GC status to a file with time stamps
-Xbatch disable background compilation
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
-Xprof output cpu profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs reduce use of OS signals by Java/VM (see documentation)
-Xcheck:jni perform additional checks for JNI functions
-Xshare:off do not attempt to use shared class data
-Xshare:auto use shared class data if possible (default)
-Xshare:on require using shared class data, otherwise fail.
-X
选项是非标准的,可能会在不事先通知的情况下更改。"(复制粘贴)
"当我在开发一个需要每个请求创建大量线程的应用程序时,这常常是我所面临的问题。
这是一个非常好的问题,有两个方面需要考虑: