Java Integer needs 128 bits

Recently, I found a Youtube video posted by Oracle Learning which discussed efficiency of Java heap memory. I found it significantly useful and really encourage you guys to take a look at the video. But, in order to give you guys an insight about what you will be expecting to find in this video read this post.

Well, if you’re a Java programmer you have probably ran into memory problems so many times. Usually much before the time you expected. This is because most builtin classes of Java such as ArrayList and Collections or any other kind of data structure that you might be using is actually taking up much more memory than you expect them to do. Therefore, I believe it is important fore every Java programmer to know how does Java exactly use memory. For example, when were programming we expect any integer to use up 4 bytes which is 32bits in 32bit JVM. When it comes to Objects we probably expect to use 32 more bits for storing the pointer to the object (obviously in 32 bit JVM). However, in Java things are not as we expected. An simple Java Integer objects requires up to x4 more than a simple int primitive which means exactly 128 bits. This is because java stores other information such as locks and other flags for each Object.

Therefore, when we are using any generic Java class, we are probably using much more memory than we expected. This video thoroughly explain how much memory different Java classes such as String or Lists or any other common data type use and describes the logic behind them. Therefore, I strongly advise every Java programmer to watch this video.

Leave a reply:

Your email address will not be published.

Site Footer