Java -Xmx,系统上的最大内存 [英] Java -Xmx, Max memory on system

查看:178
本文介绍了Java -Xmx,系统上的最大内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Java应用程序通过运行java -jar j.jar进程运行另一个Java应用程序。众所周知,J.jar根据给定的数据集使用大量内存,并且经常会获得OutOfMemoryError堆。所以我想在它上面使用-Xmx,这样我就可以分配尽可能多的内存(或接近)。我想在系统上获取总内存,然后在-Xmx中指定80-90%的内存。

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.

我的问题是否有任何解决方案?并且,我的解决方案如何发声?

Is there any solution to my problem? And, how does my solution sound?

编辑:我无法减少内存消耗,因为正在使用的内存是Java的内置pack200压缩,我正在使用它打包一些JAR文件。

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.

推荐答案

根据您的操作系统,这可能有助于获得可用和可用的内存大小:

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();

从那里,你可以计算出80-90%并启动你最大内存大小的jar希望。

From there, you can figure out 80-90% and launch your jar with the max memory size you want.

我不知道这适用于所有操作系统(即Windows),但是当我用OSX和Linux测试时它都有用。

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.

这篇关于Java -Xmx,系统上的最大内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆