Skip to main content

Java is Disappointing Sometimes...

What's Java's number one problem?  If you said "efficiency," then you win!  Why do we still use C and C++?  If you said "efficiency," then you also win!

And so... yesterday, we learned about templates and generics and so forth, and I was able to ask a question that's been bugging me for quite some time.  "When you declare an ArrayList<Integer>, are Integer Objects being stored internally, or are primitive ints being stored?"

Prior to yesterday, I had given this quite a bit of thought, actually.  I'd say about a year ago, I determined that since the classes like Integer, Byte, Float were final, non-inheritable classes, that Java could actually optimize the language by artificially storing primitive ints, auto-unboxing and auto-boxing them, as appropriate.  But, as it turned out, I was wrong!

Java does not do that.  Instead, when you store a bunch of "primitive ints" in an ArrayList<Integer>, what actually happens is that the ints are wrapped by Integer Objects, and the Integer Objects are essentially up-casted to Object internally and stored in the ArrayList.  Then, when you grab something from the ArrayList, it is essentially down-casted to Integer again, and converted back to a primitive int, if necessary.  This solution provides backward compatibility, but it is VERY inefficient.

Observe...
If I have a normal Java array of int of 10,000 elements...
int[] foo = new int[10000];
The memory allocated here is (10,000 elements) * (4 bytes per element) + 16 byte array overhead = 40016 bytes
The array has a length (integer), and you should also count the pointer to the array, etc.

If I have an ArrayList<Integer> of 10,000 Integer Objects...
ArrayList<Integer> foo = new ArrayList<Integer> (10000);
The memory allocated here is (8 bytes for Object) + (4 bytes for 32-bit pointer to actual array) + (4 bytes for "size" variable) + (16 bytes of array overhead) + (4 bytes from parent class instance variable -- another size) + (4 bytes of crap overhead -- don't ask) + (4 bytes per Object reference * 10000 elements) = 40040 bytes... all to store null Integer Object references.  If you actually place integers into the ArrayList, then each Integer Object will take up 16 bytes -- (8 bytes for Object) + (4 bytes for int inside Object) + (4 bytes of crap overhead).  So, if you actually fill that ArrayList<Integer> with data, you will use 200,040 bytes.  That's roughly FIVE times more than what was really required!!

NOT TO MENTION THE ADDITIONAL INEFFICIENCY OF AUTO-BOXING/UNBOXING AND DE-REFERENCING THAT ACTUALLY GOES ON BEHIND THE SCENES!

So why is Java slow?  Hmm... disappointing.

Comments

Popular posts from this blog

Developing a lightweight WebSocket library

Late in 2016, I began development on a lightweight, isomorphic WebSocket library for Node.js called ws-wrapper .  Today, this library is stable and has been successfully used in many production apps. Why?  What about socket.io ?  In my opinion, socket.io and its dependencies are way too heavy .  Now that the year is 2018, this couldn't be more true.  Modern browsers have native WebSocket support meaning that all of the transports built into the socket.io project are just dead weight.  On the other hand, ws-wrapper and its dependencies weigh about 3 KB when minified and gzipped.  Similarly, ws-wrapper consists of about 500 lines of code; whereas, socket.io consists of thousands of lines of code.  As Dijkstra once famously said: "Simplicity is prerequisite for reliability." ws-wrapper also provides a few more features out of the box.  The API exposes a two-way, Promise-based request/response interface.  That is, clients can request dat...

JavaScript Sticky Footer and Scroll Effect

This post talks about two different HTML/JavaScript effects: How to keep a page footer stuck at the bottom of the browser window. How to create a scrolling <div> without using a scroll bar OK. So... you have a website. You want a header stuck at the top of your page and the footer stuck at the bottom of your page. The stuff in the middle, you want to be able to scrollable. But, you don't want those ugly scrollbars to the right of your scrollable text. Maybe, instead, you'll have up arrows and down arrows above and below your <div>. When you mouseover the arrows, the text in the <div> will move up or down and create a scrolling effect. Suppose your page looks like this... <html> <head> <title>Test</title> </head> <body> <div style="position: relative; width: 700px; margin-left: auto; margin-right: auto;"> <div id="header">Header</div> <div id="scrollUp...

Node.JS + MySQL + Transactions

If you're like me, then you are probably building web applications using Node.JS and MySQL (and maybe Redis, too).  If so, you're probably going to need transactions, and you've probably already noticed that the current version of node-mysql doesn't support transactions yet.  :( But that's OK because I have a solution for you.  Check out node-mysql-queues on github .  This project provides pretty good support for MySQL transactions with a fairly simple API.  There are a couple of things to remember, though.  For one, Node.JS is very "callback-centric," so when executing a series of queries, you would normally chain the queries together with a series of callbacks.  node-mysql sort of changes this model, by allowing you to place queries on a queue to be executed in order.  If you only care about doing something when all of your queries are done, you can simply put your callback in the final query.  node-mysql-queues allows you to do the same...