Java: All about 64-bit programming

Overview

I found this article series very interesting All about 64-bit programming in one place which collects "a lot of links on the topic of 64-bit C/C++ software development."

However some of these issues are relevant to Java and can make a difference.

The size_t in C++ is 64-bit

Java uses the int type for sizes and this doesn't change in a 64-bit JVM. This gives backward compatibility but limits arrays, collections and ByteBuffer's to this size. Generally 2 billion is enough, but as files sizes and memory sizes get larger, the number of situations where it is not will grow over time.

As of mid 2011, for £1K you can buy a PC with 24 GB of memory and for £21K you can buy a server with 512 GB of memory.

This is already a problem for memory mapping a file larger than 2 GB. This can only be done by mapping in a 2 GB window of memory (2^31-1 bytes at a time). This is rather ugly and removes one of memory mapping's key features; transparency.

BTW: I have tried using the underlying library directly using reflection which supports `long` lengths and I could get this working for reading files larger than 2 GB, but not writing.

x64 has more registers than x86

This is be a small advantage. If you are performing a lot of operations using long I have seen about 10% performance improvement.

64-bit JVM can access more memory

This is essential if you need more than 1.2-3.5 GB of memory (depending on the OS). The downside is 64-bit references can increase memory usage and slow performance.

One way to use a 64-bit JVM efficiently is to use the -XX:+UseCompressedOops which uses 32-bit references in a way which can still access 32 GB of memory. It can do this because every object in the Sun/Oracle JVM is allocated on a 8-byte boundary (i.e. the lower 3 bits of the address are 000) By shifting the bits of the 32-bit reference, it can access 4GB * 8 or 32 GB in total. It has been suggested that this should be the default for Java 7.

Link: What 64-bit systems are.

Support for 32-bit programs

Programs written and compiled for a 32-bit JVM will work without re-compilation. However any native libraries used will not. A 64-bit JVM can only load 64-bit native libraries.

Switching between int and long

The int and long types work the same on a 32-bit and 64-bit JVM however if you switch code between int and long you can get some issues.

Long literals may need an ell

With all int literals you can just use the number
int i = 123456789;
long j = 123456789;
However for large numbers an L is required.
long i = 111111111111111L; // a long literal.
long j = 111111111111111l; // using `l` is discouraged as it looks like a 1.

If you have a calculated values you may need to use L
long i = 1 << 40; // i = 256!
long i = 1L << 40; // i = 2^40
The shift operator only takes the lower 5 bits of the shift for 32-bit numbers, and 6 bits for 64-bit values. So the first line is like
long i = 1 << (40 % 32);

Shifting puzzle

A puzzle for you. ;) What does this do for a 32-bit and 64-bit for x?
sign = x >> -1;

64-bit references and Object size

When using 64-bit references, the size of a reference doubles. In the Sun/Oracle JVM the header includes a reference. From the Source for Map.Entry
class MapEntry implements Map.Entry, Cloneable {
       K key;
       V value;
In a 32-bit JVM, the header takes 8 bytes, with each reference, this takes 16 byte total. In a 64-bit JVM with 32-bit references, the header takes 8 + 4 bytes and each reference takes 4 bytes. 8-bytes aligned means it uses (12+4+4 bytes + 4 padding) total: 24 bytes. With 64-bit references, the header uses 16-bytes and each references uses 8 bytes. (16 + 8 + 8) total: 32 bytes. For more details read Java: Getting the size of an Object

Conclusion

Migrating from 32-bit to a 64-bit in Java is relatively easy compared with C/C++ however there are some issues you may need to consider when migrating.

Links to this page

http://java.dzone.com/articles/java-all-about-64-bit

http://www.codeproject.com/News/15990/Java-All-about-64-bit-programming.aspx

'+U+"";break a}g=U}l.body=g;l.timestamp=Date.parse(c.published.$t)+"";c.author&&c.author.constructor===Array&&(g=c.author[0])&&(l.author={name:g.name? g.name.$t:void 0,profileUrl:g.uri?g.uri.$t:void 0,avatarUrl:g.gd$image?g.gd$image.src:void 0});c.link&&(c.link[2]&&(l.link=l.permalink=c.link[2].href),c.link[3]&&(g=/.*comments\/default\/(\d+)\?.*/.exec(c.link[3].href))&&g[1]&&(l.parentId=g[1]));l.deleteclass="item-control blog-admin";if(c.gd$extendedProperty)for(var B in c.gd$extendedProperty)"blogger.itemClass"==c.gd$extendedProperty[B].name?l.deleteclass+=" "+c.gd$extendedProperty[B].value:"blogger.displayTime"==c.gd$extendedProperty[B].name&& (l.displayTime=c.gd$extendedProperty[B].value);w.push(l)}f=50>w.length?null:parseInt(w[w.length-1].timestamp,10)+1;k(w);window.bloggercomments=null};var F=Q();F.type="text/javascript";P(F,V(h+"&callback=bloggercomments"));document.getElementsByTagName("head")[0].appendChild(F)}},hasMore:function(){return!!f},getMeta:function(k,h){return"iswriter"==k?h.author&&h.author.name==d.authorName&&h.author.profileUrl==d.authorUrl?"true":"":"deletelink"==k?d.baseUri+"/delete-comment.g?blogID="+d.blogId+"&postID="+ h.id:"deleteclass"==k?h.deleteclass:""},onReply:function(k,h){null==e&&(e=document.getElementById("comment-editor"),null!=e&&(n=e.style.height,e.style.display="block",r=e.src.split("#")));e&&k&&k!==t&&(document.getElementById(h).insertBefore(e,null),h=r[0]+(k?"&parentID="+k:""),r[1]&&(h=h+"#"+r[1]),e.src=h,e.style.height=n||e.style.height,t=k,e.removeAttribute("data-resized"),e.dispatchEvent(new Event("iframeMoved")))},rendered:!0,initComment:S,initReplyThread:R,config:{maxDepth:d.maxThreadDepth}, messages:b};a=function(){if(window.goog&&window.goog.comments){var k=document.getElementById("comment-holder");window.goog.comments.render(k,aa)}};window.goog&&window.goog.comments?a():(window.goog=window.goog||{},window.goog.comments=window.goog.comments||{},window.goog.comments.loadQueue=window.goog.comments.loadQueue||[],window.goog.comments.loadQueue.push(a))},X=["blogger","widgets","blog","initThreadedComments"],Y=m;X[0]in Y||"undefined"==typeof Y.execScript||Y.execScript("var "+X[0]); for(var Z;X.length&&(Z=X.shift());)X.length||void 0===W?Y=Y[Z]&&Y[Z]!==Object.prototype[Z]?Y[Z]:Y[Z]={}:Y[Z]=W;}).call(this);

Post a Comment

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Unusual Java: StackTrace Extends Throwable