如何判断我是在64位JVM还是32位JVM(从程序中运行)?

本文翻译自:How can I tell if I'm running in 64-bit JVM or 32-bit JVM (from within a program)?

How can I tell if the JVM in which my application runs is 32 bit or 64-bit? 如何判断运行我的应用程序的JVM是32位还是64位? Specifically, what functions or properties I can used to detect this within the program? 具体来说,我可以用什么功能或属性来检测程序中的这个?


#1楼

参考:https://stackoom.com/question/8eQO/如何判断我是在-位JVM还是-位JVM-从程序中运行


#2楼

On Linux, you can get ELF header information by using either of the following two commands: 在Linux上,您可以使用以下两个命令之一获取ELF标头信息:

file {YOUR_JRE_LOCATION_HERE}/bin/java

o/p: ELF 64-bit LSB executable , AMD x86-64, version 1 (SYSV), for GNU/Linux 2.4.0, dynamically linked (uses shared libs), for GNU/Linux 2.4.0, not stripped o / p:用于GNU / Linux 2.4.0的ELF 64位LSB可执行文件 ,AMD x86-64,版本1(SYSV),动态链接(使用共享库),用于GNU / Linux 2.4.0,不剥离

or 要么

readelf -h {YOUR_JRE_LOCATION_HERE}/bin/java | grep 'Class'

o/p: Class: ELF 64 o / p:等级:ELF 64


#3楼

You retrieve the system property that marks the bitness of this JVM with: 您可以使用以下命令检索标记此JVM位数的系统属性

System.getProperty("sun.arch.data.model");

Possible results are: 可能的结果是:

  • "32" – 32-bit JVM "32" - 32位JVM
  • "64" – 64-bit JVM "64" - 64位JVM
  • "unknown" – Unknown JVM "unknown" - 未知的JVM

As described in the HotSpot FAQ : HotSpot FAQ中所述

When writing Java code, how do I distinguish between 32 and 64-bit operation? 编写Java代码时,如何区分32位和64位操作?

There's no public API that allows you to distinguish between 32 and 64-bit operation. 没有公共API允许您区分32位和64位操作。 Think of 64-bit as just another platform in the write once, run anywhere tradition. 将64位视为一次写入中的另一个平台,随处运行。 However, if you'd like to write code which is platform specific (shame on you), the system property sun.arch.data.model has the value "32", "64", or "unknown". 但是,如果您要编写特定于平台的代码(对您有羞耻感),则系统属性sun.arch.data.model的值为“32”,“64”或“unknown”。

An example where this could be necessary is if your Java code depends on native libraries, and you need to determine whether to load the 32- or 64-bit version of the libraries on startup. 这可能是必要的一个示例是,如果您的Java代码依赖于本机库,并且您需要确定是否在启动时加载32位或64位版本的库。


#4楼

I installed 32-bit JVM and retried it again, looks like the following does tell you JVM bitness, not OS arch: 我安装了32位JVM并再次重试,看起来下面的内容告诉你JVM的位数,而不是OS arch:

System.getProperty("os.arch");
#
# on a 64-bit Linux box:
# "x86" when using 32-bit JVM
# "amd64" when using 64-bit JVM

This was tested against both SUN and IBM JVM (32 and 64-bit). 这是针对SUN和IBM JVM(32位和64位)进行测试的。 Clearly, the system property is not just the operating system arch. 显然,系统属性不仅仅是操作系统。


#5楼

For certain versions of Java, you can check the bitness of the JVM from the command line with the flags -d32 and -d64 . 对于某些版本的Java,您可以使用标志-d32-d64从命令行检查JVM的-d64

$ java -help
...
    -d32          use a 32-bit data model if available
    -d64          use a 64-bit data model if available

To check for a 64-bit JVM, run: 要检查64位JVM,请运行:

$ java -d64 -version

If it's not a 64-bit JVM, you'll get this: 如果它不是64位JVM,你会得到这个:

Error: This Java instance does not support a 64-bit JVM.
Please install the desired version.

Similarly, to check for a 32-bit JVM, run: 同样,要检查32位JVM,请运行:

$ java -d32 -version

If it's not a 32-bit JVM, you'll get this: 如果它不是32位JVM,你会得到这个:

Error: This Java instance does not support a 32-bit JVM.
Please install the desired version.

These flags were added in Java 7, deprecated in Java 9, removed in Java 10, and no longer available on modern versions of Java. 这些标志在Java 7中添加,在Java 9中已弃用 ,在Java 10中已删除,在Java的现代版本中不再可用。


#6楼

如果您使用的是JNA,则可以检查com.sun.jna.Native.POINTER_SIZE == 4 (32位)或com.sun.jna.Native.POINTER_SIZE == 8 (64位)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值