简体   繁体   English

为什么Java应用程序占用大量内存?

[英]Why does java application take much memory?

I have a small Java application which is basically a command line utility which sleeps 99% of the time and runs once a day to copy a file in particular location. 我有一个小的Java应用程序,它基本上是一个命令行实用程序,它可以休眠99%的时间,每天运行一次,以在特定位置复制文件。 That app takes in memory 10 Mb. 该应用程序占用的内存为10 Mb。 Of course, that is not a big number, but it looks like that app takes more memory than it could. 当然,这并不是一个很大的数目,但是看起来该应用程序占用了更多的内存。 Could you suggest how to reduce the amount of memory for that app? 您能否建议如何减少该应用程序的内存量?

I run it like this: 我这样运行:

java -Xmx12m -jar c:\\copyFiles\\copyFiles.jar

You are allowing your application to use roughly 12 MB because you use the -Xmx12m parameter for the JVM. 您允许应用程序使用大约12 MB的空间,因为对JVM使用了-Xmx12m参数。 Java is a bit wasteful regarding memory. Java在内存方面有点浪费。 You can decrease the max amount of memory your application can use: 您可以减少应用程序可以使用的最大内存量:

-Xmx10k - Can use up to 10 KB of memory. -Xmx10k最多可以使用10 KB的内存。

-Xmx10m - Can use up to 10 MB of memory. -Xmx10m最多可以使用10 MB的内存。

-Xmx10g - Can use up to 10 GB of memory. -Xmx10g最多可以使用10 GB的内存。

Feel free to mix this setting however you want but be careful. 随意混合此设置,但要小心。 If you exceed the set amount an OutOfMemory-exception is thrown. 如果超过设置的数量,则会抛出OutOfMemory-exception。

AFAIK this heavily depends on the Garbage Collector that is used and the jvm version, but in general the VM is not very eager to give memory back to the OS, for numerous reason. AFAIK在很大程度上取决于所使用的垃圾收集器和jvm版本,但是由于许多原因,通常VM并不非常渴望将内存还给OS。 A much broader discussion is here about the same topic 这里关于同一主题的更广泛的讨论

Since jdk-12, there is this JEP : 从jdk-12开始,有这个JEP:

http://openjdk.java.net/jeps/346 http://openjdk.java.net/jeps/346

that will instruct the VM (with certain flags) to give memory back to the OS after a "planned" garbage collector cycle takes place - this is controlled by some newly introduced flags; 这将指示VM(带有某些标志)在“计划的”垃圾收集器周期发生后将内存还给OS-这是由一些新引入的标志控制的; read the JEP to find out more. 阅读JEP以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM