boolean - fighting for Java memory ...

    Have you ever had such situations when your Java application is crashing at the seams? In my case, this happened due to the lack of available RAM. And, of course, there was a shortage at the most inopportune moment: the next long-awaited release was on the nose, one of the servers was stopped to update the code and data, and reincarnation of the old code was no longer possible, several meetings and interviews were planned in the coming days, which greatly distracted from the optimization process - in general, the incident did not go unnoticed.

    By the way, had I done the right backup and emergency work to restore vitality would have been much more calm, but that would have been a completely different story. So, I have at my disposal a code that lacks 15Gb of RAM for normal operation and a very long and expensive start-up process (about 5 hours), during which you can only sit with crossed fingers and hope that this time the cherished words OutOfMemoryException are not appear in the console of the remote server.
    I will not describe all the tricks that I had to do to restore a stopped server within three days, but I will share one of my mini discoveries - boolean - this is not the type of data that you want to use in highly loaded systems. Attention Question:

    What do you think, how much memory does boolean take for example on Ubuntu server x64?

    The correct answer would be: unknown and depends only on the implementation of the JVM.

    Consider the prevailing Sun JVM and read in the specification of the virtual machine that there is no boolean type in it as such, int is used instead! And this means that exactly 32 bits are used to store the yes / no value, regardless of the processor architecture. True, in the same section we see that optimization has been done to work with boolean arrays, which are converted to an array of bytes, which gives an increase in available memory by 4 times. And yet, paying for storing a zero or one with seven extra bits is sometimes just blasphemy and mockery of servers (especially when the size of arrays is 500 million elements).

    The salvation in such cases is the BitSet class, which behaves like a boolean array, but packs the data so that only one bit of memory is allocated for one bit (with low overhead for the entire array). BitSet stores an array of type long inside itself, and when requesting or setting the value of a specific bit, it calculates the index of the desired long and uses bitwise and shift operations to perform calculations on a single bit.
    There is an even more interesting BitSet implementation, OpenBitSet - Apache implementation, which is used for Lucene. It is much faster, but misses some checks carried out in the original BitSet. What to use is up to you.

    Also popular now: