简体   繁体   English

使用Xmx操纵JVM的内存限制

[英]Manipulating memory limit for JVM using Xmx

Having such simple app 拥有如此简单的应用

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int n = reader.nextInt();
        reader.close();
    }
}

I run it with different Xmx parameters (keep in mind the JConsole has been injected to the process) 我使用不同的Xmx参数运行它(请记住,JConsole已注入到进程中)

  • -Xmx1M won't run -Xmx1M无法运行
  • -Xmx2M error -Xmx2M错误
  • -Xmx4M nothing prints -Xmx4M什么都不打印
  • -Xmx6M and bigger - will run -Xmx6M及更大-将运行

The JConsole shows me strange memory usage for the app JConsole向我显示了该应用程序的奇怪内存使用情况

If ran with -Xmx8M 如果与-Xmx8M一起运行

  • Used 3.0MB 已使用3.0MB
  • Commited 8.4MB 已投入8.4MB
  • Max 8.4MB 最大8.4MB

If ran with -Xmx16M 如果与-Xmx16M一起运行

  • Used 4.0MB 已使用4.0MB
  • Commited 16.8MB 已投入16.8MB
  • Max 16.8MB 最大16.8MB

And with -Xmx32M 并带有-Xmx32M

  • Used 11.0MB 已使用11.0MB
  • Commited 33.6MB 已投入33.6MB
  • Max 33.6MB 最大33.6MB

While using external software to measure the memory consumption may have some impact on these values, it still seems to Java will eat everything you let it eat even if it's not required. 尽管使用外部软件来测量内存消耗可能会对这些值产生一定的影响,但Java似乎仍然会吃掉您让它吃掉的所有东西,即使它不是必需的。 This is a part of Java I hate the most, but trying to understand. 我最讨厌这是Java的一部分,但是试图理解。

What happens with the memory that is not required by my program, but has been eaten by JVM because I allowed it to do so by changing Xmx parameter? 我的程序不需要但由于更改Xmx参数允许它而被JVM占用的内存会发生什么情况? Can I have impact the amount of "lost" memory using different way than Xmx parameter? 我可以使用与Xmx参数不同的方法来影响“丢失”的内存量吗?

Java will eat everything you let it eat even if it's not required. 即使不需要,Java也会吃掉您让它吃的所有东西。

Java doesn't aggressively clean up memory. Java不会主动清理内存。 If you give it memory it expects to use it to reduce CPU consumption. 如果您给它提供内存,它将期望使用它来减少CPU消耗。

Can I have impact the amount of "lost" memory using different way than Xmx parameter? 我可以使用与Xmx参数不同的方法来影响“丢失”的内存量吗?

You can reduce the minimum size, you can control how quickly it grows. 您可以减小最小尺寸,可以控制其增长速度。 In fact, there is over 500 parameters to control GCs, but in general, the advice is to set as few as possible unless you really know what you are doing to avoid running into issues which wouldn't have happened if you left them alone. 实际上,有超过500个参数可用于控制GC,但通常建议您设置尽可能少的参数,除非您真的知道自己在做什么,以避免碰到如果不理会它们不会发生的问题。

A key thing to remember is that your time is money, and memory costs money. 要记住的关键是您的时间就是金钱,而记忆力就是金钱。 At some point, it is not worth your time trying to save a little bit of memory. 在某些时候,浪费一点时间是不值得的。

Based on average wages for developers and average prices for PC memory (costs at your organisation will vary) 根据开发人员的平均工资和PC内存的平均价格(您组织的成本会有所不同)

1 day - 16 GB
1 hour - 2 GB
5 minutes - 160 MB
1 minute - 32 MB

If you spend more than one minute of your time saving less than 32 MB, it probably wasn't worth it. 如果您花费一分钟以上的时间来保存少于32 MB的内存,那可能是不值得的。

This is a part of Java I hate the most, 这是我最讨厌的Java的一部分,

I hate the misuse of units, but really I should learn to get over it 我讨厌滥用单位,但实际上我应该学会克服它

m - milli
M - Mega
b - bits
B - Bytes
1 MB = 8 Mb = 1000000000 mB = 8000000000 mb

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

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