How to distinguish between 32 and 64-bit JVM
来源:互联网 发布:1949台湾借尸还魂 知乎 编辑:程序博客网 时间:2024/05/20 07:13
There's no public API that allows you to distinguish between 32 and 64-bit operation. Think of 64-bit as just another platform in the write once, run anywhere tradition. 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".
the System Property sun.arch.data.model
String dataModel = System.getProperty("sun.arch.data.model");if("32").equals(dataModel){ // load 32-bit DLL}else if("64").equals(dataModel){ // load 64-bit DLL}else{ // error handling}
We can also use System.getProperty("os.arch")
if ("x86".equals(System.getProperty("os.arch"))) { // 32 bit} else if ("x64".equals(System.getProperty("os.arch"))) { // 64 bit}But either System.getProperty("sun.arch.data.model") or System.getProperty("os.arch") can get if the jvm (not the host) is 32 or 64-bit.
Getting the jvm which is 32 or 64-bit information is usefull in some scenarios. For example, you would like to set the chunk size depending on whether jvm is 32 or 64-bit. Here is a sample in Lucene FSDirectory class which set default chunk size.
public abstract class FSDirectory extends Directory { /** * Default read chunk size. This is a conditional default: on 32bit JVMs, it defaults to 100 MB. On 64bit JVMs, it's * <code>Integer.MAX_VALUE</code>. * * @see #setReadChunkSize */ public static final int DEFAULT_READ_CHUNK_SIZE = Constants.JRE_IS_64BIT ? Integer.MAX_VALUE : 100 * 1024 * 1024;Constants.JRE_IS_64BIT code is following:
/** True iff running on a 64bit JVM */ public static final boolean JRE_IS_64BIT; static { boolean is64Bit = false; try { final Class<?> unsafeClass = Class.forName("sun.misc.Unsafe"); final Field unsafeField = unsafeClass.getDeclaredField("theUnsafe"); unsafeField.setAccessible(true); final Object unsafe = unsafeField.get(null); final int addressSize = ((Number) unsafeClass.getMethod("addressSize") .invoke(unsafe)).intValue(); //System.out.println("Address size: " + addressSize); is64Bit = addressSize >= 8; } catch (Exception e) { final String x = System.getProperty("sun.arch.data.model"); if (x != null) { is64Bit = x.indexOf("64") != -1; } else { if (OS_ARCH != null && OS_ARCH.indexOf("64") != -1) { is64Bit = true; } else { is64Bit = false; } } } JRE_IS_64BIT = is64Bit;
- How to distinguish between 32 and 64-bit JVM
- How to distinguish between strings in heap or literals?
- distinguish between variable and field
- Differences Between 32-bit and 64-bit Systems
- Distinguish between pointers and references in C++
- 条款一:Distinguish between pointers and references
- Item 1: distinguish between pointers and references
- Item7 Distinguish between () and {} when creating objects
- File System Redirector and Sysnative - how to call 64 bit program from 32 bit application
- how to share register and bit field definitions between a device driver and the FPGA it controls
- How to convert between TDes8 and TDes16
- How to distinguish a human and a robot in the future?
- Bridge the Gap Between 32- and 64-bit Applications
- How to run 32-bit app in Ubuntu 64-bit?
- How to build zlib 1.2.3 using Visual Studio 2008 for 32-bit and 64-bit Windows
- Consider the Differences Between the 32-bit and 64-bit Data Models
- How to Share Session State Between Classic ASP and
- How To Pass Arrays Between Visual Basic and C
- C用宏实现C++继承的特性
- 单例(Singleton)设计模式
- BST的一个实现
- 图片的javascript延时加载
- linux c编程 多线程(中级)
- How to distinguish between 32 and 64-bit JVM
- c++名词解释
- hibernate——配置测试
- Shell 学习4(通配符)
- C#启动停止windows服务
- EMOS问题处理2- slockd 垃圾邮件拦截
- extern用法
- C#启动windows服务浅析
- hibernate——对象状态