enum有多少内存?

时间:2022-05-08 23:00:17

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

例如,如果我有一个包含两种情况的枚举,它会比布尔值占用更多的内存吗?语言:Java、c++

12 个解决方案

#1


41  

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

在Java中,enum是一个完整的类:

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.

Java编程语言枚举类型比其他语言中的其他类型更强大。enum声明定义了一个类(称为enum类型)。enum类主体可以包含方法和其他字段。

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.

为了查看每个enum的实际大小,让我们创建一个实际的enum并检查它创建的类文件的内容。

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

假设我们有以下常量enum类:

public enum Constants {
  ONE,
  TWO,
  THREE;
}

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

编译上面的enum并使用javap分解生成的类文件将给出以下内容:

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.)

反汇编表明枚举的每个字段都是常量枚举类的实例。(使用javap的进一步分析将揭示,每个字段都是通过在静态初始化块中调用新常量(String)构造函数来创建一个新对象来初始化的。)

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.

因此,我们可以知道,我们创建的每个enum字段至少与在JVM中创建对象的开销相同。

#2


17  

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.

在Java中,在内存中应该只有一个枚举值的实例。然后,对enum的引用只需要该引用的存储。检查enum的值与其他引用比较一样有效。

#3


8  

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.

在存储大量的枚举时,你只需要担心这个问题。对于Java,您可以在某些情况下使用枚举集。它在内部使用位向量,这是非常高效和快速的空间。

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

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

#4


7  

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.

bool可以实现为单个字节,但通常在结构中,它将被具有对齐要求的其他元素包围,这意味着布尔值将有效地占用至少与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.

现代处理器从主存中加载数据作为整个缓存线,64字节。从L1缓存加载一个字节和加载四个字节之间的区别是可以忽略的。

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.

如果您试图在一个非常高性能的应用程序中优化缓存行,那么您可能会担心枚举的大小,但是通常我认为定义enum比使用布尔值更清楚。

#5


4  

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.

在Java中,它需要更多的内存。在c++中,对于相同类型的常量,它不需要超过所需的内存(它在编译时进行计算,在运行时没有剩余意义)。在c++中,这意味着enum的默认类型将占用与int相同的空间。

#6


3  

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.

在ISO c++中,枚举值不需要超过其最大枚举数的要求。特别是,enum {TRUE, FALSE}可能具有sizeof(1),即使sizeof(bool)= sizeof(int)。根本没有要求。有些编译器将enums与int大小相同,这是一个编译器特性,这是允许的,因为标准只规定了最小值。其他编译器使用扩展来控制枚举的大小。

#7


1  

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

#8


1  

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.

在c++中,enum的大小通常与int类型相同,也就是说,编译器提供命令行开关以允许enum的大小设置为符合定义的值范围的最小大小,这种情况并不少见。

#9


0  

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

不,enum的大小通常与int类型相同,与boolean值相同。

#10


0  

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.

如果您的enum将只有两种情况,那么使用boolean可能是更好的主意(内存大小、性能、使用/逻辑),在Java中更是如此。如果您对内存成本感到疑惑,它可能意味着您计划使用它们。在Java中,您可以使用BitSet类,或者更小的规模,在两种语言中,您都可以使用位操作来操作位。

#11


0  

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.

sizeof(enum)取决于您在enum中的内容。我最近试图找到ArrayList()的大小,它具有默认的构造函数params,并且不存储对象(这意味着存储容量为10)。原来ArrayList并不是太大,小于100字节。

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)

因此,对于一个非常简单的enum来说,sizeof(enum)应该小于10字节。您可以编写一个小程序,给它一定的内存,然后尝试分配枚举。你应该能够找到它(这就是我发现ArrayList的内存的方法)

BR,
~A

BR,~

#12


0  

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

在C/ c++中,enum的大小将与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.

使用gcc,您可以向enum定义添加属性(打包),以使其占用最少的内存。如果enum中的最大值为< 256,则为一个字节,如果最大值为< 65536,则为两个字节。

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

#1


41  

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

在Java中,enum是一个完整的类:

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.

Java编程语言枚举类型比其他语言中的其他类型更强大。enum声明定义了一个类(称为enum类型)。enum类主体可以包含方法和其他字段。

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.

为了查看每个enum的实际大小,让我们创建一个实际的enum并检查它创建的类文件的内容。

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

假设我们有以下常量enum类:

public enum Constants {
  ONE,
  TWO,
  THREE;
}

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

编译上面的enum并使用javap分解生成的类文件将给出以下内容:

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.)

反汇编表明枚举的每个字段都是常量枚举类的实例。(使用javap的进一步分析将揭示,每个字段都是通过在静态初始化块中调用新常量(String)构造函数来创建一个新对象来初始化的。)

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.

因此,我们可以知道,我们创建的每个enum字段至少与在JVM中创建对象的开销相同。

#2


17  

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.

在Java中,在内存中应该只有一个枚举值的实例。然后,对enum的引用只需要该引用的存储。检查enum的值与其他引用比较一样有效。

#3


8  

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.

在存储大量的枚举时,你只需要担心这个问题。对于Java,您可以在某些情况下使用枚举集。它在内部使用位向量,这是非常高效和快速的空间。

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

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

#4


7  

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.

bool可以实现为单个字节,但通常在结构中,它将被具有对齐要求的其他元素包围,这意味着布尔值将有效地占用至少与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.

现代处理器从主存中加载数据作为整个缓存线,64字节。从L1缓存加载一个字节和加载四个字节之间的区别是可以忽略的。

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.

如果您试图在一个非常高性能的应用程序中优化缓存行,那么您可能会担心枚举的大小,但是通常我认为定义enum比使用布尔值更清楚。

#5


4  

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.

在Java中,它需要更多的内存。在c++中,对于相同类型的常量,它不需要超过所需的内存(它在编译时进行计算,在运行时没有剩余意义)。在c++中,这意味着enum的默认类型将占用与int相同的空间。

#6


3  

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.

在ISO c++中,枚举值不需要超过其最大枚举数的要求。特别是,enum {TRUE, FALSE}可能具有sizeof(1),即使sizeof(bool)= sizeof(int)。根本没有要求。有些编译器将enums与int大小相同,这是一个编译器特性,这是允许的,因为标准只规定了最小值。其他编译器使用扩展来控制枚举的大小。

#7


1  

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

#8


1  

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.

在c++中,enum的大小通常与int类型相同,也就是说,编译器提供命令行开关以允许enum的大小设置为符合定义的值范围的最小大小,这种情况并不少见。

#9


0  

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

不,enum的大小通常与int类型相同,与boolean值相同。

#10


0  

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.

如果您的enum将只有两种情况,那么使用boolean可能是更好的主意(内存大小、性能、使用/逻辑),在Java中更是如此。如果您对内存成本感到疑惑,它可能意味着您计划使用它们。在Java中,您可以使用BitSet类,或者更小的规模,在两种语言中,您都可以使用位操作来操作位。

#11


0  

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.

sizeof(enum)取决于您在enum中的内容。我最近试图找到ArrayList()的大小,它具有默认的构造函数params,并且不存储对象(这意味着存储容量为10)。原来ArrayList并不是太大,小于100字节。

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)

因此,对于一个非常简单的enum来说,sizeof(enum)应该小于10字节。您可以编写一个小程序,给它一定的内存,然后尝试分配枚举。你应该能够找到它(这就是我发现ArrayList的内存的方法)

BR,
~A

BR,~

#12


0  

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

在C/ c++中,enum的大小将与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.

使用gcc,您可以向enum定义添加属性(打包),以使其占用最少的内存。如果enum中的最大值为< 256,则为一个字节,如果最大值为< 65536,则为两个字节。

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