by Mikhail Vorontsov
This article will give you an overview of some popular Java data types memory consumption. This article follows An overview of memory saving techniques in Java article earlier published in this blog. I strongly recommend you to read the previous article before reading this one.
Enum, EnumMap, EnumSet
Enums were added to Java 1.5. Technically speaking, each
enum is an
Object and has all memory consumption properties of objects described in the previous article. Luckily, they have several useful properties:
enum objects are singletons. This is guaranteed by Java language – you can create an instance of
enum only by its definition. This means that you pay for
enum object once and then you only spend 4 bytes per its reference (in this article I will assume that object references occupy 4 bytes). But what is the benefit of enums if they consume as much memory as
int variables and 4 times more than
byte variables, besides type safety and better support in IDE?
The answer is the
ordinal() method implemented by every
enum. It is an increasing number starting from 0 and ending at the number of values in the given
enum minus 1. Such
enum property allows us to use arrays for
enum to any other object mapping: a value related to the first enum value will be stored in
array, second enum will be mapped to
array and so on according to
Enum.ordinal() method result. By the way, it was a short description of JDK
EnumMap class implementation.
If you need to implement a map from
enum, you won’t have a tailored JDK implementation at hand. Of course, you can use any JDK
Object map, but it wouldn’t be that efficient. The easy way here is to use Trove
TObjectIntMap in rare cases when you have more than 128 values in your enum) and map from
Object key into
Enum.ordinal() value. You will need a decoding method for getters in order to convert
byte into an
enum. This method will require 1 byte per entry, which is the best we can do without paying CPU algorithmic penalty (of course, we can use less than 1 byte per
enum, if there are less than 128, 64, 32, etc elements in the
enum, but it may make your code more complicated for a very little memory gain).
With all this knowledge at hand, you may now realize that
EnumSet is implemented similarly to
BitSet. There are 2
EnumSet implementations in JDK:
JumboEnumSet. The former is used for enums having less than 65 values (it covers 99,9999% of real-world enums), the latter is used for larger enumerations.
RegularEnumSet utilizes the knowledge of the fact that there are less or equal to 64 values in the
enum. It allows
RegularEnumSet to use a single
long to store all “enum present in the set” flags, rather than using
long (utilized by
BitSet). Using a single
long instead of
long allows to save 4 bytes on
long reference and 16 bytes on
long value (
long occupies 8 bytes,
long needs 12 bytes for header, 8 bytes for a single
long and 4 bytes for alignment).