Java -Xmx,系统上的最大内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1190837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java -Xmx, Max memory on system
提问by Amandeep Grewal
My Java application runs another Java application, by running the process "java -jar j.jar". J.jar is known to use a LOT of memory depending on the dataset it is given, and often gets an OutOfMemoryError heap. So I want to use -Xmx on it, so that I can allocate as much memory as possible (or close to). I was thinking of getting the total memory on the system, then specifying 80-90% of that in -Xmx.
我的 Java 应用程序通过运行进程“java -jar j.jar”运行另一个 Java 应用程序。众所周知,J.jar 会根据给定的数据集使用大量内存,并且经常会得到 OutOfMemoryError 堆。所以我想在它上面使用-Xmx,这样我就可以分配尽可能多的内存(或接近)。我正在考虑获取系统上的总内存,然后在 -Xmx 中指定其中的 80-90%。
Is there any solution to my problem? And, how does my solution sound?
我的问题有什么解决办法吗?而且,我的解决方案听起来如何?
Edit: I cant reduce the memory consumption as the memory being used is by Java's built-in pack200 compression, which I am using to pack some JAR files.
编辑:我无法减少内存消耗,因为正在使用的内存是 Java 的内置 pack200 压缩,我用它来打包一些 JAR 文件。
采纳答案by joe p
Depending on your OS, this might work for getting the free and available memory size:
根据您的操作系统,这可能适用于获取可用和可用内存大小:
java.lang.management.OperatingSystemMXBean mxbean = java.lang.management.ManagementFactory.getOperatingSystemMXBean();
com.sun.management.OperatingSystemMXBean sunmxbean = (com.sun.management.OperatingSystemMXBean) mxbean;
long freeMemory = sunmxbean.getFreePhysicalMemorySize();
long availableMemory = sunmxbean.getTotalPhysicalMemorySize();
From there, you can figure out 80-90% and launch your jar with the max memory size you want.
从那里,您可以计算出 80-90% 并以您想要的最大内存大小启动您的 jar。
I don't know that this works with all OS's (i.e. Windows), but it worked when I tested it with both OSX and Linux.
我不知道这适用于所有操作系统(即 Windows),但是当我在 OSX 和 Linux 上对其进行测试时它可以工作。
回答by Bill K
Well, one thing I can tell you is don't let your app get close to filling up ram. Java apps don't swap gracefully at all. I think because of Garbage Collection, java constantly pulls its memory from swap.
好吧,我可以告诉你的一件事是不要让你的应用程序接近填满内存。Java 应用程序根本无法正常交换。我认为由于垃圾收集,java 不断地从交换中提取内存。
I ran into a deadlock where I think the system was asking java for memory which would cause a GC and pull stuff out of the swapfile--at this point the system would just spin until I reset it.
我遇到了一个死锁,我认为系统正在向 java 请求内存,这会导致 GC 并从交换文件中提取内容——此时系统将一直旋转,直到我重置它。
This was with a LOT of ram and a LOT of swap space (for the time) and an older Java VM, so your mileage may vary.
这是使用大量内存和大量交换空间(当时)和较旧的 Java VM,因此您的里程可能会有所不同。
Also, depending on how you are starting that other app, you may have to specify -Xms for your app instead of the other one. If you are giving it a full command, give it the -Xms, but if you are simply calling the main class in the jar, then your app needs the -Xms. (Oh, you specified, yeah you need to pass it into the "Java" command you are calling. )
此外,根据您启动其他应用程序的方式,您可能必须为您的应用程序指定 -Xms 而不是另一个应用程序。如果你给它一个完整的命令,给它 -Xms,但如果你只是调用 jar 中的主类,那么你的应用程序需要 -Xms。(哦,您指定了,是的,您需要将其传递给您正在调用的“Java”命令。)
回答by Lorin
Is there a reason you are using the OS to execute the program in the jar? If you don't need it to execute in a separate process from your application, you could just invoke the main method directly from your code, and start your application with whatever -Xmx you want.
您是否有理由使用操作系统来执行 jar 中的程序?如果您不需要它在与您的应用程序分开的进程中执行,您可以直接从您的代码调用 main 方法,并使用您想要的任何 -Xmx 启动您的应用程序。
回答by Geo
There is a complete blog post on how to troubleshoot java applications with jconsole and other tools in the following blog. Keep in mind that the lack of control of memory use is most probably a memory leak, but it could be due to other reasons as well. Take a look to the post, try to replicate that scenario, and see if that solved your issue.
有在下列与JConsole的和其他工具如何解决Java应用程序的完整的博客文章博客。请记住,缺乏对内存使用的控制很可能是内存泄漏,但也可能是由于其他原因。查看帖子,尝试复制该场景,看看是否解决了您的问题。
http://www.kiragiannis.com/cloud-computing/debug-a-java-application-in-the-cloud/
http://www.kiragiannis.com/cloud-computing/debug-a-java-application-in-the-cloud/
回答by rob
If you haven't done so already, you need to run your program through a memory profiler. You might find that certain data structures aren't being disposed of, even though they're no longer being used.
如果您还没有这样做,您需要通过内存分析器运行您的程序。您可能会发现某些数据结构并没有被处理掉,即使它们不再被使用。
JProfiler is pretty nifty, but you can get the same information using HPROF, which was introduced in Java 5: http://java.sun.com/developer/technicalArticles/Programming/HPROF.html
JProfiler 非常漂亮,但您可以使用 Java 5 中引入的 HPROF 获取相同的信息:http: //java.sun.com/developer/technicalArticles/Programming/HPROF.html
Keep in mind also that different platforms have different maximum heap sizes, based on architecture (32-bit vs 64-bit), OS, and even the JVM.
还要记住,基于体系结构(32 位与 64 位)、操作系统甚至 JVM,不同的平台具有不同的最大堆大小。
If you have a lot of values that may be reused (such as strings you're reading in from an XML file), you could get a huge decrease in memory requirements by pooling your objects.
如果您有许多可以重用的值(例如您从 XML 文件中读取的字符串),则可以通过池化对象来大幅减少内存需求。
回答by Tim Williscroft
The limit for -XmX is -Xmx1500m on 32 bit windows. Shared libraries get in the way of a bigger heap. You'll need about 2Gb of RAM to do this.
-XmX 在 32 位窗口上的限制是 -Xmx1500m。共享库阻碍了更大的堆。您需要大约 2Gb 的 RAM 才能执行此操作。
On non-windows OSes you can go bigger, and 64Bit JVM's are capable of a LOT more.
在非 Windows 操作系统上,您可以做得更大,而 64 位 JVM 则能够做得更多。
Windows XP will not let you have more than 3Gb of RAM ( doesn't care if you have 4Gb physical, ever since XP SP3) Vista may be different YMMV.
Windows XP 不会让您拥有超过 3Gb 的 RAM(从 XP SP3 开始,您不关心是否有 4Gb 物理内存)Vista 可能与 YMMV 不同。
I've tried -Xmx4000M on a 64 bit JVM on 64 bit Linux and it was fine. considering I had 6Gb of physical ram, it was not a big request.
我已经在 64 位 Linux 上的 64 位 JVM 上尝试过 -Xmx4000M,它很好。考虑到我有 6Gb 的物理内存,这不是一个很大的要求。
Your 80% idea is interesting, but my test systems run higher percentages than that without ill effect. (As long as you don't try doing anything else.)
你的 80% 的想法很有趣,但我的测试系统运行的百分比高于没有不良影响的百分比。(只要你不尝试做任何其他事情。)
And the other commenter is right, paging out your JVM's in-memory image is not quick. Later JVM's are better at doing this less messily ( but they have better garbage collectors too)
另一个评论者是对的,分页出 JVM 的内存中映像并不快。后来的 JVM 更擅长做这件事,不那么混乱(但他们也有更好的垃圾收集器)
If you can't reduce your memory consumption – and I know how hard that is – then have lots of physical ram and allocate most of it.
如果你不能减少你的内存消耗——我知道这有多难——那就拥有大量的物理内存并分配大部分。