How much memory do Enums take?

JavaC++MemoryEnums

Java Problem Overview


For example if I have an Enum with two cases, does it make take more memory than a boolean? Languages: Java, C++

Java Solutions


Solution 1 - Java

In Java, an enum is a full-blown class:

> Java programming language enum types > are much more powerful than their > counterparts in other languages. The > enum declaration defines a class > (called an enum type). The enum class > body can include methods and other > fields.

In order to see the actual size of each enum, let's make an actual enum and examine the contents of the class file it creates.

Let's say we have the following Constants enum class:

public enum Constants {
  ONE,
  TWO,
  THREE;
}

Compiling the above enum and disassembling resulting class file with javap gives the following:

Compiled from "Constants.java"
public final class Constants extends java.lang.Enum{
    public static final Constants ONE;
    public static final Constants TWO;
    public static final Constants THREE;
    public static Constants[] values();
    public static Constants valueOf(java.lang.String);
    static {};
}

The disassembly shows that that each field of an enum is an instance of the Constants enum class. (Further analysis with javap will reveal that each field is initialized by creating a new object by calling the new Constants(String) constructor in the static initialization block.)

Therefore, we can tell that each enum field that we create will be at least as much as the overhead of creating an object in the JVM.

Solution 2 - Java

In Java, there should only be one instance of each of the values of your enum in memory. A reference to the enum then requires only the storage for that reference. Checking the value of an enum is as efficient as any other reference comparison.

Solution 3 - Java

You would only worry about this when storing large quantities of enums. For Java, you may be able to use an EnumSet in some cases. It uses a bit vector internally which is very space efficient and fast.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/EnumSet.html

Solution 4 - Java

bool might be implemented as a single byte, but typically in a structure it would be surrounded by other elements that have alignment requirements that would mean that the boolean would effectively be occupying at least as much space as an int.

Modern processors load data from main memory as a whole cache line, 64 bytes. The difference between loading one byte from L1 cache and loading four bytes is negligible.

If you're trying to optimise for cache lines in a very high-performance application, then you might worry about how big your enum is, but generally I'd say it's clearer to define an enum than to use a boolean.

Solution 5 - Java

In Java, it would take more memory. In C++, it would take no memory than required for a constant of the same type (it's evaluated at compile-time and has no residual significance at runtime). In C++, this means that the default type for an enum will occupy the same space as an int.

Solution 6 - Java

In ISO C++ there is no obligation for an enum to be larger than its largest enumerator requires. In particular, enum {TRUE, FALSE} may have sizeof(1) even when sizeof(bool)==sizeof(int). There is simply no requirement. Some compilers make the enums the same size as an int. That is a compiler feature, which is allowed because the standard only imposes a minimum. Other compilers use extensions to control the size of an enum.

Solution 7 - Java

printf("%d", sizeof(enum));

Solution 8 - Java

In C++ an enum is typically the same size as an int. That said it is not uncommon for compilers to provide a command line switch to allow the size of the enum to be set to the smallest size that fits the range of values defined.

Solution 9 - Java

No, an enum is generally the same size as an int, same as boolean.

Solution 10 - Java

If your enum will ever have only two cases, indeed using a boolean instead might be a better idea (memory size, performance, usage/logic), even more in Java.
If you are wondering about memory cost, it might imply you plan to use lot of them. In Java you can use BitSet class, or on a smaller scale, in both languages you can manipulate bits with bitwise operations.

Solution 11 - Java

sizeof(enum) depends upon what you have in the enum. I was recently trying to find the size of an ArrayList() with default constructor params and no objects stored inside (which means the capacity to store is 10). It turned out that ArrayList is not too big < 100 bytes.

So, sizeof(enum) for a very simple enum should be less than 10 bytes. you can write a small program, give it a certain amount of memory and then try allocating enums. you should be able to figure it out(that's how i found out the memory of ArrayList)

BR,
~A

Solution 12 - Java

In C/C++ an enum will be the same size as an int.

With gcc you can add attribute((packed)) to the enum definition to make it take the minimum footprint. If the largest value in the enum is < 256 this will be one byte, two bytes if the largest value is < 65536, etc.

typedef enum {
    MY_ENUM0,
    MY_ENUM1,
    MY_ENUM2,
    MY_ENUM3,
    MY_ENUM4,
    MY_ENUM5
} __attribute__((packed)) myEnum_e;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeloView Question on Stackoverflow
Solution 1 - JavacoobirdView Answer on Stackoverflow
Solution 2 - JavathenickdudeView Answer on Stackoverflow
Solution 3 - JavaTomView Answer on Stackoverflow
Solution 4 - JavaMike DimmickView Answer on Stackoverflow
Solution 5 - JavaJeff HubbardView Answer on Stackoverflow
Solution 6 - JavaMSaltersView Answer on Stackoverflow
Solution 7 - JavaPatrickView Answer on Stackoverflow
Solution 8 - JavaAndrew EdgecombeView Answer on Stackoverflow
Solution 9 - JavaSerafina BrociousView Answer on Stackoverflow
Solution 10 - JavaPhiLhoView Answer on Stackoverflow
Solution 11 - JavaanjanbView Answer on Stackoverflow
Solution 12 - JavaDGentryView Answer on Stackoverflow