java JRE 32 位与 64 位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17285928/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:35:26  来源:igfitidea点击:

JRE 32bit vs 64bit

javax86x86-64jvm-hotspot

提问by SnakeDoc

I've been using Java for a while now, and my typical ritual of setting up a new dev machine requires the norm of downloading and installing the latest JDK from Oracle's site.

我已经使用 Java 有一段时间了,我设置新开发机器的典型习惯要求从 Oracle 的站点下载和安装最新的 JDK。

This prompted an unusual question today, does it matter if I use the 32bit or 64bit JRE bundle?

这在今天引发了一个不寻常的问题, does it matter if I use the 32bit or 64bit JRE bundle?

From thinking back on it, I've installed both versions before and my normal toolchain plugs happily in (Eclipse). In my day-to-day programming, I do not recall ever having to change something or think about something in a different way just because I was using the 64bit JRE (or targetting the 64bit JRE for that respect).

回想起来,我之前已经安装了这两个版本,并且我的正常工具链愉快地插入(Eclipse)。在我的日常编程中,我不记得仅仅因为我使用的是 64 位 JRE(或者在这方面以 64 位 JRE 为目标)而不得不改变某些事情或以不同的方式思考某些事情。

From my understanding of 64bit vs. 32bit - it really boils down to how the numbers are stored underneath the covers... and I do know that intis a 32 bits and longis 64 bits... same with floatbeing 32 bits and doubleis 64 bits -- so is it just that Java has abstracted even this subtlety away, and perhaps has been "64 bit compatible" all along?

根据我对 64 位与 32 位的理解 - 它实际上归结为数字如何存储在封面下......我确实知道这int是 32 位和long64 位......与float32 位和double64 位相同-- 那么是不是 Java 已经将这种微妙之处抽象化了,并且可能一直是“64 位兼容”的?

I'm sure I'm missing something here besides not being able to install a 64 bit JRE onto a 32 bit system.

除了无法在 32 位系统上安装 64 位 JRE 之外,我确定我在这里遗漏了一些东西。

回答by cambecc

64-bit vs. 32-bit really boils down to the size of object references, not the size of numbers.

64 位与 32 位真正归结为对象引用的大小,而不是数字的大小。

In 32-bit mode, references are four bytes, allowing the JVM to uniquely address 2^32 bytes of memory. This is the reason 32-bit JVMs are limited to a maximum heap size of 4GB (in reality, the limit is smaller due to other JVM and OS overhead, and differs depending on the OS).

在 32 位模式下,引用是四个字节,允许 JVM 唯一寻址 2^32 个字节的内存。这就是 32 位 JVM 被限制为 4GB 的最大堆大小的原因(实际上,由于其他 JVM 和操作系统开销,限制更小,并且因操作系统而异)。

In 64-bit mode, references are (surprise) eight bytes, allowing the JVM to uniquely address 2^64 bytes of memory, which should be enough for anybody. JVM heap sizes (specified with -Xmx) in 64-bit mode can be huge.

在 64 位模式下,引用是(令人惊讶的)8 个字节,允许 JVM 唯一寻址 2^64 个字节的内存,这对任何人来说都足够了。-Xmx64 位模式下的JVM 堆大小(用 指定)可能很大。

But 64-bit mode comes with a cost: references are double the size, increasing memory consumption. This is why Oracle introduced "Compressed oops". With compressed oops enabled (which I believe is now the default), object references are shrunk to four bytes, with the caveat that the heap is limited to four billion objects (and 32GB Xmx). Compressed oops are not free: there is a small computational cost to achieve this big reduction in memory consumption.

但是 64 位模式是有代价的:引用的大小增加了一倍,增加了内存消耗。这就是 Oracle 引入“压缩 oops”的原因。启用压缩 oops(我认为现在是默认设置)后,对象引用缩小到四个字节,但需要注意的是堆限制为 40 亿个对象(和 32GB Xmx)。压缩 oops 不是免费的:实现内存消耗的大幅减少需要很小的计算成本。

As a personal preference, I always run the 64-bit JVM at home. The CPU is x64 capable, the OS is too, so I like the JVM to run in 64-bit mode as well.

作为个人偏好,我总是在家里运行 64 位 JVM。CPU 支持 x64,操作系统也是如此,所以我也喜欢 JVM 以 64 位模式运行。

回答by Andy Thomas

As you note, primitive numeric types in Java are well-defined.

正如您所注意到的,Java 中的原始数字类型是明确定义的。

However, the choice between 32-bit and 64-bit JVMs canmatter if your Java application is using native-code libraries, which may be built for use in a 32-bit application, a 64-bit application, or both.

但是,如果您的 Java 应用程序使用本机代码库,则在 32 位和 64 位 JVM 之间的选择可能很重要,本机代码库可能是为在 32 位应用程序、64 位应用程序或两者中使用而构建的。

If you have native libraries that support only 32-bit applications, you either need to use a 32-bit JVM, or build 64-bit versions of the libraries.

如果您的本机库仅支持 32 位应用程序,则您需要使用 32 位 JVM,或者构建 64 位版本的库。

回答by Archimedes Trajano

Depending on context, for local development I will always use a 64-bit JDK. Primarily because I would likely need the whole memory space for builds and the IDE.

根据上下文,对于本地开发,我将始终使用 64 位 JDK。主要是因为我可能需要整个内存空间用于构建和 IDE。

That being said for integration to production, I would recommend 32-bit if it is possible. Why?

话虽如此,但如果可能的话,我会推荐 32 位。为什么?

For some Java EE servers that are licensed for production use, it would depend on some factors like which machine how many cores etc. For WebSphere Liberty Profile specifically, you are also limited to 2GB.

对于某些许可用于生产用途的 Java EE 服务器,这将取决于某些因素,例如哪台机器有多少内核等。特别是对于 WebSphere Liberty Profile,您也被限制为 2GB。

64-bit JREs would take up slightly more memory and if you're trying to constrain it to something like 2GB or better yet 2x 1GB cluster you would have more flex space to work around in without paying a cent.

64 位 JRE 会占用稍多的内存,如果您试图将其限制为 2GB 或更好的 2x 1GB 集群,您将有更多的灵活空间来解决,而无需支付一分钱。

From https://plumbr.eu/blog/java/should-i-use-32-or-64-bit-jvm

来自https://plumbr.eu/blog/java/should-i-use-32-or-64-bit-jvm

Problem 1: 30-50% of more heap is required on 64-bit. Why so? Mainly because of the memory layout in 64-bit architecture. First of all – object headers are 12 bytes on 64-bit JVM. Secondly, object references can be either 4 bytes or 8 bytes, depending on JVM flags and the size of the heap. This definitely adds some overhead compared to the 8 bytes on headers on 32-bit and 4 bytes on references. You can also dig into one of our earlier posts for more information about calculating the memory consumption of an object.

Problem 2: Longer garbage collection pauses. Building up more heap means there is more work to be done by GC while cleaning it up from unused objects. What it means in real life is that you have to be extra cautious when building heaps larger than 12-16GB. Without fine tuning and measuring you can easily introduce full GC pauses spanning several minutes. In applications where latency is not crucial and you can optimize for throughput only this might be OK, but on most cases this might become a showstopper.

问题 1:在 64 位上需要多 30-50% 的堆。为什么这样?主要是因为 64 位架构中的内存布局。首先——对象头在 64 位 JVM 上是 12 个字节。其次,对象引用可以是 4 字节或 8 字节,具体取决于 JVM 标志和堆的大小。与 32 位标头上的 8 个字节和引用上的 4 个字节相比,这肯定会增加一些开销。您还可以深入研究我们之前的一篇博文,了解有关计算对象内存消耗的更多信息。

问题 2:更长的垃圾收集暂停。建立更多的堆意味着在从未使用的对象中清理它的同时,GC 需要做更多的工作。这意味着在现实生活中,构建大于 12-16GB 的堆时必须格外小心。无需微调和测量,您就可以轻松引入跨越几分钟的完整 GC 暂停。在延迟不重要并且您可以优化吞吐量的应用程序中,这可能没问题,但在大多数情况下,这可能会成为一个阻碍。

To limit your impact for your Java EE environment, offload parts of it to other microservices such as ElasticSearch for search, Hazelcast for caching, your database for data storage and keep your Java EE server to host your application core itself rather than running the services inside it.

为了限制您对 Java EE 环境的影响,将其中的一部分卸载到其他微服务,例如用于搜索的 ElasticSearch、用于缓存的 Hazelcast、用于数据存储的数据库,并让您的 Java EE 服务器托管您的应用程序核心本身,而不是在内部运行服务它。

回答by Jaime

I think there are two main differences to consider. One has been mentioned here but not the other.

我认为有两个主要差异需要考虑。这里提到了一个,但没有提到另一个。

On the one hand, as other mentioned, the memory and data types. 32-bits and 64-bits JVMs use different native data type sizes and memory-address spaces.

一方面,正如其他人提到的,内存和数据类型。32 位和 64 位 JVM 使用不同的本机数据类型大小和内存地址空间。

  • 64-bits JVMs can allocate (can use) more memory than the 32-bits ones.
  • 64-bits use native datatypes with more capacity but occupy more space. Because that, the same Object may occupy more space too.
  • For JVMs which the Garbage Collector (GC) freezes the machine, the 64-bits versions may be slower because the GC must check bigger heaps/objects and it takes more time.
  • There is an IBM presentationexplaining these differences.
  • 64 位 JVM 可以分配(可以使用)比 32 位更多的内存。
  • 64 位使用容量更大但占用更多空间的本机数据类型。因为那样,同一个对象也可能占用更多空间。
  • 对于垃圾收集器 (GC) 冻结机器的 JVM,64 位版本可能会更慢,因为 GC 必须检查更大的堆/对象并且需要更多时间。
  • 有一个IBM 演示文稿解释了这些差异。

And on the other hand, the supported native libraries. Java programs that use JNI to access native libraries require different versions depending on the type of JVM.

另一方面,支持的本机库. 使用 JNI 访问本机库的 Java 程序需要不同的版本,具体取决于 JVM 的类型。

  • 32-bits JVMs use 32-bits native libraries and 64-bits JVMs use 64bits libraries.
  • That means that, if your program uses libraries that rely on native code such as SWT, you will need different versions of them. Note in the SWT download page, there are different versions for Linux/Windows 32- and 64-bits. Note that there are different versions of Eclipse(each one with a different version of SWT) for 32- and 64-bits.
  • Some applications, such as Alloy, are packaged with 32-bits native libraries. They fail with 64-bit JVMs. You can solve these problems just downloading the corresponding 64-bits native libraries and configuring the JNI appropriately.
  • 32 位 JVM 使用 32 位本机库,64 位 JVM 使用 64 位库。
  • 这意味着,如果您的程序使用依赖于本机代码的库,例如 SWT,您将需要它们的不同版本。请注意,在SWT 下载页面 中,Linux/Windows 32 位和 64 位有不同的版本。请注意,有用于 32 位和 64 位的不同版本的 Eclipse(每个版本都有不同的 SWT 版本)。
  • 某些应用程序(例如 Alloy)与 32 位本机库一起打包。它们在 64 位 JVM 上失败。您只需下载相应的 64 位本机库并适当配置 JNI 即可解决这些问题。

回答by Jim T

Do I need to understand the difference between 32-bit JVM and 64-bit JVM?

我需要了解 32 位 JVM 和 64 位 JVM 之间的区别吗?

If you aren't building a performance critical application, you don't have to understand the difference. The subtle difference between 32-bit JVM and 64-bit JVM wouldn't make much difference to your application. You can skip reading further

如果您不构建性能关键的应用程序,则不必了解其中的区别。32 位 JVM 和 64 位 JVM 之间的细微差别不会对您的应用程序产生太大影响。你可以跳过阅读进一步

Does 64-bit JVM perform better than 32-bit JVM?

64 位 JVM 是否比 32 位 JVM 性能更好?

Most of us think 64-bit is bigger than 32-bit, thus 64-bit JVM performance will be better than 32-bit JVM performance. Unfortunately, it's not the case. 64-bit JVM can have a small performance degradation than 32-bit JVM. Below is the excerpt from Oracle JDK documentation regarding 64-bit JVM performance:

我们大多数人都认为 64 位大于 32 位,因此 64 位 JVM 性能会优于 32 位 JVM 性能。不幸的是,事实并非如此。64 位 JVM 的性能下降比 32 位 JVM 小。以下是 Oracle JDK 文档中关于 64 位 JVM 性能的摘录:

“Generally, the benefits of being able to address larger amounts of memory come with a small performance loss in 64-bit VMs versus running the same application on a 32-bit VM.

“通常,与在 32 位虚拟机上运行相同的应用程序相比,能够处理大量内存的好处是在 64 位虚拟机中性能损失很小。

The performance difference comparing an application running on a 64-bit platform versus a 32-bit platform on SPARC is on the order of 10-20% degradation when you move to a 64-bit VM. On AMD64 and EM64T platforms this difference ranges from 0-15% depending on the amount of pointer accessing your application performs.”

当您迁移到 64 位虚拟机时,在 64 位平台上运行的应用程序与在 SPARC 上运行的 32 位平台的性能差异大约会降低 10-20%。在 AMD64 和 EM64T 平台上,此差异范围为 0-15%,具体取决于访问应用程序执行的指针数量。”

Does 32 bit JVM or 64 bit JVM matter anymore.

32 位 JVM 还是 64 位 JVM 不再重要了。

What are the things to consider when migrating from 32-bit JVM to 64-bit JVM? a. GC Pause times

从 32 位 JVM 迁移到 64 位 JVM 时需要考虑哪些事项?一个。GC 暂停次数

The primary reason to migrate from 32-bit JVM to 64-bit JVM is to attain large heap size (i.e. -Xmx). When you increase heap size, automatically your GC pause times will start to go high, because now there is more garbage in the memory to clear up. You need to do proper GC tuning before doing the migration, otherwise your application can experience several seconds to few minutes pause time.

从 32 位 JVM 迁移到 64 位 JVM 的主要原因是获得大堆大小(即 -Xmx)。当你增加堆大小时,你的 GC 暂停时间会自动开始变高,因为现在内存中有更多的垃圾需要清理。您需要在迁移之前进行适当的 GC 调整,否则您的应用程序可能会遇到几秒到几分钟的暂停时间。

b. Native Library

湾 本地库

If your application is using Java Native Interface (JNI) to access native libraries, then you need to upgrade the native libraries as well. Because 32-bit JVM can use only 32-bit native library. Similarly, 64-bit JVM can use only 64-bit native library.

如果您的应用程序使用 Java 本机接口 (JNI) 来访问本机库,那么您还需要升级本机库。因为 32 位 JVM 只能使用 32 位本机库。同样,64 位 JVM 只能使用 64 位本机库。