Is there a way to set heap size from a running Java program?
是否有一种方法可以从正在运行的Java程序中设置堆大小?
8 个解决方案
#1
57
No.
不。
What you can do with an app that has very variable heap requirements is to set your max heap size very high with -Xmx
and tune -XX:MaxHeapFreeRatio
and -XX:MinHeapFreeRatio
so that the app will not hang on to a lot of memory when the heap shrinks (it does that with default settings).
与一个应用程序,你能做什么非常变量堆需求设置最大值堆大小非常高- xmx和调优- xx:MaxHeapFreeRatio和- xx:MinHeapFreeRatio以便应用程序时不会留住大量的内存堆收缩与默认设置(它)。
But note that this may cause performance problems when the memory actually used by the app varies both strongly and quickly - in that case you're better off having it hang on to all the memory rather than give it back to the OS only to claim it again a second later. You might also want to fiddle with the GC options to ensure that the GC doesn't leave too much unclaimed objects lying around, which it tends to do when there's a lot of room for the heap to grow, and which would defeat the goal of wanting the heap size to adjust to the app's needs.
但请注意,这可能会导致性能问题时实际使用的内存的应用程序不同强烈和迅速——在这种情况下你最好把它挂在所有的记忆而不是还给操作系统只要求一遍不一会儿。您可能还想摆弄GC选项确保GC不会留下太多的周围无人认领的物品,它倾向于做有很多的堆空间增长,并将失败的目标希望堆大小适应应用的需要。
#2
12
According to http://www.dreamincode.net/forums/showtopic96263.htm, you can't do this at runtime, but you can spawn another process with a different heap size.
根据http://www.dreamincode.net/forums/showtopic96263.htm,您不能在运行时执行此操作,但您可以生成另一个具有不同堆大小的进程。
#3
7
You can tweak those settings when you start your application but once the JVM is up and running those values cannot be changed. Something like this:
您可以在启动应用程序时调整这些设置,但是一旦JVM启动并运行这些值时,就不能更改这些设置。是这样的:
java -Xms32m -Xmx512m FooBar
java -Xms32m -Xmx512m FooBar
will set the minimum heap size to 32MB and the maximum heap size to 512MB. Once these are set, you cannot change them within the running program.
将最小堆大小设置为32MB,最大堆大小设置为512MB。一旦设置好了,就不能在运行程序中更改它们。
#4
2
The consensus may indeed be that this is not possible, but we should be looking at the JVM source to see how it can be ergonomically controlled. It would be very nice to be able to have a JVMTI agent be able to adjust the heap/perm/tenured/new/&c sizing online/at runtime.
大家一致认为这是不可能的,但是我们应该看看JVM源代码,看看它是如何被人体工程学控制的。如果JVMTI代理能够在运行时在线调整堆/perm/保留区/新/&c大小,那就太好了。
What would this do? it would allow agents to infer sizing adjustments based upon performance or footprint goals which will be important when moving JVMs into the Cloud.
这怎么办?它允许代理根据性能或内存占用目标推断大小调整,这在将jvm转移到云中时非常重要。
#5
2
You can use the -mx option on startup (also known as -Xmx) This is maximum size you should ever need in which cause you shouldn't need to set it to more than the maximum size you will ever need.
您可以在启动时使用-mx选项(也称为-Xmx),这是您需要的最大大小,因为您不需要将其设置为超过所需的最大大小。
However, a work around is to have the main() check the maximum size and restart the java if the maximum size is not as desired. i.e. start another java program and die.
但是,一个工作是让main()检查最大大小,如果最大大小不理想,则重新启动java。即启动另一个java程序,然后死亡。
#6
0
I asked same question to myself. And unlike answers above there is something I can do about my app increasing max heap JVM size. If app is a web server in cluster mode, I could start a new instance with changed min/max heap size and than shutdown initial instance. That shall be especially simple in GlassFish where you have management instance separated from nodeAgent(app server clustered instance) JVM.
我问了自己同样的问题。与上面的答案不同的是,我可以为我的应用程序增加最大堆JVM大小。如果app是集群模式下的web服务器,我可以用更改的最小/最大堆大小和关闭初始实例来启动一个新的实例。这在GlassFish中尤其简单,因为您的管理实例与nodeAgent(应用服务器集群实例)的JVM分离。
Since many JVM applications are web apps, I think it worth to preserve in this blog.
由于许多JVM应用程序都是web应用程序,所以我认为值得在这个博客中保存它。
#7
-1
java -Xmx500m
on startup only
只在启动时使用java - xmx500。
#8
-1
If I understand your question correctly, you're trying to change the heap size at runtime. I don't see any reason why this should be possible. Set the heap size at startup using the -Xmx
JVM option. I also advise you to set the -Xms
option only if you absolutely need to. This option sets the initial amount of head memory that is allocated for the JVM.
如果我理解正确,您正在尝试在运行时更改堆大小。我看不出这有什么理由。使用-Xmx JVM选项在启动时设置堆大小。我还建议您只在绝对需要时才设置-Xms选项。此选项设置为JVM分配的初始内存数量。
You should know how your application behaves in terms of memory. Set the the value of -Xmx
wisely. If your app is some kind of a server app you can set a higher value, otherwise compromise your choice with other possible apps running in client machines and of course available memory.
您应该了解应用程序在内存方面的行为。明智地设置-Xmx的值。如果你的应用程序是某种服务器应用程序,你可以设置更高的值,否则就会让你的选择与其他可能运行在客户端机器上的应用程序相妥协,当然还有可用的内存。
#1
57
No.
不。
What you can do with an app that has very variable heap requirements is to set your max heap size very high with -Xmx
and tune -XX:MaxHeapFreeRatio
and -XX:MinHeapFreeRatio
so that the app will not hang on to a lot of memory when the heap shrinks (it does that with default settings).
与一个应用程序,你能做什么非常变量堆需求设置最大值堆大小非常高- xmx和调优- xx:MaxHeapFreeRatio和- xx:MinHeapFreeRatio以便应用程序时不会留住大量的内存堆收缩与默认设置(它)。
But note that this may cause performance problems when the memory actually used by the app varies both strongly and quickly - in that case you're better off having it hang on to all the memory rather than give it back to the OS only to claim it again a second later. You might also want to fiddle with the GC options to ensure that the GC doesn't leave too much unclaimed objects lying around, which it tends to do when there's a lot of room for the heap to grow, and which would defeat the goal of wanting the heap size to adjust to the app's needs.
但请注意,这可能会导致性能问题时实际使用的内存的应用程序不同强烈和迅速——在这种情况下你最好把它挂在所有的记忆而不是还给操作系统只要求一遍不一会儿。您可能还想摆弄GC选项确保GC不会留下太多的周围无人认领的物品,它倾向于做有很多的堆空间增长,并将失败的目标希望堆大小适应应用的需要。
#2
12
According to http://www.dreamincode.net/forums/showtopic96263.htm, you can't do this at runtime, but you can spawn another process with a different heap size.
根据http://www.dreamincode.net/forums/showtopic96263.htm,您不能在运行时执行此操作,但您可以生成另一个具有不同堆大小的进程。
#3
7
You can tweak those settings when you start your application but once the JVM is up and running those values cannot be changed. Something like this:
您可以在启动应用程序时调整这些设置,但是一旦JVM启动并运行这些值时,就不能更改这些设置。是这样的:
java -Xms32m -Xmx512m FooBar
java -Xms32m -Xmx512m FooBar
will set the minimum heap size to 32MB and the maximum heap size to 512MB. Once these are set, you cannot change them within the running program.
将最小堆大小设置为32MB,最大堆大小设置为512MB。一旦设置好了,就不能在运行程序中更改它们。
#4
2
The consensus may indeed be that this is not possible, but we should be looking at the JVM source to see how it can be ergonomically controlled. It would be very nice to be able to have a JVMTI agent be able to adjust the heap/perm/tenured/new/&c sizing online/at runtime.
大家一致认为这是不可能的,但是我们应该看看JVM源代码,看看它是如何被人体工程学控制的。如果JVMTI代理能够在运行时在线调整堆/perm/保留区/新/&c大小,那就太好了。
What would this do? it would allow agents to infer sizing adjustments based upon performance or footprint goals which will be important when moving JVMs into the Cloud.
这怎么办?它允许代理根据性能或内存占用目标推断大小调整,这在将jvm转移到云中时非常重要。
#5
2
You can use the -mx option on startup (also known as -Xmx) This is maximum size you should ever need in which cause you shouldn't need to set it to more than the maximum size you will ever need.
您可以在启动时使用-mx选项(也称为-Xmx),这是您需要的最大大小,因为您不需要将其设置为超过所需的最大大小。
However, a work around is to have the main() check the maximum size and restart the java if the maximum size is not as desired. i.e. start another java program and die.
但是,一个工作是让main()检查最大大小,如果最大大小不理想,则重新启动java。即启动另一个java程序,然后死亡。
#6
0
I asked same question to myself. And unlike answers above there is something I can do about my app increasing max heap JVM size. If app is a web server in cluster mode, I could start a new instance with changed min/max heap size and than shutdown initial instance. That shall be especially simple in GlassFish where you have management instance separated from nodeAgent(app server clustered instance) JVM.
我问了自己同样的问题。与上面的答案不同的是,我可以为我的应用程序增加最大堆JVM大小。如果app是集群模式下的web服务器,我可以用更改的最小/最大堆大小和关闭初始实例来启动一个新的实例。这在GlassFish中尤其简单,因为您的管理实例与nodeAgent(应用服务器集群实例)的JVM分离。
Since many JVM applications are web apps, I think it worth to preserve in this blog.
由于许多JVM应用程序都是web应用程序,所以我认为值得在这个博客中保存它。
#7
-1
java -Xmx500m
on startup only
只在启动时使用java - xmx500。
#8
-1
If I understand your question correctly, you're trying to change the heap size at runtime. I don't see any reason why this should be possible. Set the heap size at startup using the -Xmx
JVM option. I also advise you to set the -Xms
option only if you absolutely need to. This option sets the initial amount of head memory that is allocated for the JVM.
如果我理解正确,您正在尝试在运行时更改堆大小。我看不出这有什么理由。使用-Xmx JVM选项在启动时设置堆大小。我还建议您只在绝对需要时才设置-Xms选项。此选项设置为JVM分配的初始内存数量。
You should know how your application behaves in terms of memory. Set the the value of -Xmx
wisely. If your app is some kind of a server app you can set a higher value, otherwise compromise your choice with other possible apps running in client machines and of course available memory.
您应该了解应用程序在内存方面的行为。明智地设置-Xmx的值。如果你的应用程序是某种服务器应用程序,你可以设置更高的值,否则就会让你的选择与其他可能运行在客户端机器上的应用程序相妥协,当然还有可用的内存。