java -Xmx1G是指10 ^ 9还是2 ^ 30个字节? [英] Does java -Xmx1G mean 10^9 or 2^30 bytes?

查看:354
本文介绍了java -Xmx1G是指10 ^ 9还是2 ^ 30个字节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,用于 -Xmx -Xms -Xmn 选项(k,M和G,或标准可能性较小的K,m或g)二进制前缀倍数(即1024的幂),或者它们是1000的幂?

And in general, are the units used for the -Xmx, -Xms and -Xmn options ("k", "M" and "G", or the less standard possibilities "K", "m" or "g") Binary prefix multiples (i.e. powers of 1024), or are they powers of 1000?

手册说它们代表千字节(kB),兆字节(MB)和千兆字节(GB),表明它们是原始 SI 系统。我的非正式测试(我不是很自信)建议他们真的是 kibibytes(kiB) mebibytes(MiB) gibibytes(GiB),1024的所有权力。

The manuals say they represent kilobytes (kB), megabytes (MB) and gigabytes (GB), suggesting they are powers of 1000 as defined in the original SI system. My informal tests (that I'm not very confident about) suggest they are really kibibytes (kiB), mebibytes (MiB) and gibibytes (GiB), all powers of 1024.

那么哪个是对的?例如。什么Java代码会显示当前大小?

So which is right? E.g. what Java code would show the current size?

使用1024的倍数对RAM大小来说并不奇怪,因为RAM通常是通过加倍硬件模块来物理布局的。但是,随着我们获得越来越大的权力,以明确和标准的方式使用单位变得越来越重要,因为混乱的可能性会增加。单位t也被我的JVM接受,1 TiB比1 TB大10%。

Using multiples of 1024 is not surprising for RAM sizes, since RAM is typically physically laid out by doubling up hardware modules. But using units in a clear and standard way is ever more important as we get to bigger and bigger powers, since the potential for confusion grows. The unit "t" is also accepted by my JVM, and 1 TiB is 10% bigger than 1 TB.

注意:如果这些真的是二进制倍数,我建议更新文档和用户界面非常清楚,例如附加字母k或K表示kibibytes(1024字节),或m或M表示mebibytes(1048576字节)。这就是采用的方法,例如在Ubuntu中: UnitsPolicy - Ubuntu Wiki

Note: if these really are binary multiples, I suggest updating the documentation and user interfaces to be very clear about that, with examples like "Append the letter k or K to indicate kibibytes (1024 bytes), or m or M to indicate mebibytes (1048576 bytes)". That is the approach taken, e.g., in Ubuntu: UnitsPolicy - Ubuntu Wiki.

注意:有关选项用途的更多信息,请参阅例如 java - 启动JVM时的Xms和Xmx参数是什么?

Note: for more on what the options are used for, see e.g. java - What are the Xms and Xmx parameters when starting JVMs?.

推荐答案

简答:JVM命令行参数使用的所有内存大小都在传统的二进制文件中指定单位,其中千字节为1024字节,其他单位的增加功率为1024.

Short answer: All memory sizes used by the JVM command line arguments are specified in the traditional binary units, where a kilobyte is 1024 bytes, and the others are increasing powers of 1024.

长答案:

命令行参数的此文档页面说明以下内容适用于所有接受内存大小的参数:

This documentation page on the command line arguments says the following applies to all the arguments accepting memory sizes:


例如,要将大小设置为8 GB,您可以指定 8g 8192m 8388608k ,或 8589934592 作为参数。

对于 -Xmx ,它给出了这些具体的例子:

For -Xmx, it gives these specific examples:

以下示例显示如何使用各种单位将分配的内存的最大允许大小设置为80 MB:

The following examples show how to set the maximum allowed size of allocated memory to 80 MB using various units:

-Xmx83886080

-Xmx81920k

-Xmx80m

在我考虑检查文档之前(我假设你已经有了?),我检查了HotSpot的来源和发现内存值在 src中解析/share/vm/runtime/arguments.cpp 由函数 atomull (似乎代表ASCII到内存,unsigned long long):

Before I thought to check the documentation (I assumed you already had?), I checked the source of HotSpot and found the memory values are parsed in src/share/vm/runtime/arguments.cpp by the function atomull (which seems to stand for "ASCII to memory, unsigned long long"):

// Parses a memory size specification string.
static bool atomull(const char *s, julong* result) {
  julong n = 0;
  int args_read = sscanf(s, JULONG_FORMAT, &n);
  if (args_read != 1) {
    return false;
  }
  while (*s != '\0' && isdigit(*s)) {
    s++;
  }
  // 4705540: illegal if more characters are found after the first non-digit
  if (strlen(s) > 1) {
    return false;
  }
  switch (*s) {
    case 'T': case 't':
      *result = n * G * K;
      // Check for overflow.
      if (*result/((julong)G * K) != n) return false;
      return true;
    case 'G': case 'g':
      *result = n * G;
      if (*result/G != n) return false;
      return true;
    case 'M': case 'm':
      *result = n * M;
      if (*result/M != n) return false;
      return true;
    case 'K': case 'k':
      *result = n * K;
      if (*result/K != n) return false;
      return true;
    case '\0':
      *result = n;
      return true;
    default:
      return false;
  }
}

这些常量 K M G SRC /共享/ VM /公用事业/ globalDefinitions.hpp

const size_t K                  = 1024;
const size_t M                  = K*K;
const size_t G                  = M*K;

所有这些都证实了文档,但支持 T terabytes的后缀显然是后来添加的,根本没有记录。

All this confirms the documentation, except that support for the T suffix for terabytes was apparently added later and is not documented at all.

使用单位乘数不是强制性的,所以如果你想要十亿字节你可以写 -Xmx1000000000 。如果你使用乘数,它们是二进制的,所以 -Xmx1G 意味着2个 30 字节,或者一个oRAM。

It is not mandatory to use a unit multiplier, so if you want one billion bytes you can write -Xmx1000000000. If you do use a multiplier, they're binary, so -Xmx1G means 230 bytes, or one stick o' RAM.

(这并不奇怪,因为Java早于IEC试图追溯重新定义现有单词的尝试。如果IEC仅建议消除内存单元的歧义,则可以节省混淆。限定词二进制和十进制偶尔出现其含义不清楚。例如,二进制千兆字节(GB 2 )= 1024 3 字节,十进制千兆字节(GB 10 )= 1000 3 字节。但不,他们重新定义了每个人都在使用的单词,不可避免地爆炸困惑,让我们坚持这些小丑术语gibibyte,tebibyte和其他人。哦,上帝饶了我们。)

(Which is not really surprising, because Java predates the IEC's attempt to retroactively redefine existing words. Confusion could have been saved if the IEC had merely advised disambiguating the memory units with the qualifiers "binary" and "decimal" the occasional times their meaning wasn't clear. E.g., binary gigabytes (GB2) = 10243 bytes, and decimal gigabytes (GB10) = 10003 bytes. But no, they redefined the words everyone was already using, inevitably exploding confusion, and leaving us stuck with these clown terms "gibibyte", "tebibyte" and the rest. Oh God spare us.)

这篇关于java -Xmx1G是指10 ^ 9还是2 ^ 30个字节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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