Java 枚举需要多少内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/143285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 08:52:01  来源:igfitidea点击:

How much memory do Enums take?

javac++memoryenums

提问by

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

例如,如果我有一个包含两种情况的 Enum,它是否比布尔值占用更多内存?语言:Java、C++

采纳答案by coobird

In Java, an enumis a full-blown class:

在 Java 中, anenum是一个成熟的类

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 编程语言枚举类型比其他语言中的枚举类型强大得多。枚举声明定义了一个类(称为枚举类型)。枚举类主体可以包括方法和其他字段。

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

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

Let's say we have the following Constantsenum class:

假设我们有以下Constants枚举类:

public enum Constants {
  ONE,
  TWO,
  THREE;
}

Compiling the above enumand disassembling resulting classfile with javapgives the following:

编译上述内容enum并反汇编结果class文件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 enumis an instance of the Constantsenumclass. (Further analysis with javapwill reveal that each field is initialized by creating a new object by calling the new Constants(String)constructor in the static initialization block.)

反汇编表明,an 的每个字段enum都是Constantsenum该类的一个实例。(进一步分析 withjavap会发现每个字段都是通过调用new Constants(String)静态初始化块中的构造函数创建一个新对象来初始化的。)

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

因此,我们可以看出,enum我们创建的每个字段至少与在 JVM 中创建对象的开销一样多。

回答by Cody Brocious

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

不,枚举通常与 int 大小相同,与布尔值相同。

回答by Jeff Hubbard

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++ 中,这意味着枚举的默认类型将占用与 int 相同的空间。

回答by Patrick

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

回答by PhiLho

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.

如果您的枚举只有两种情况,那么实际上使用布尔值可能是一个更好的主意(内存大小、性能、使用/逻辑),在 Java 中甚至更多。
如果您想知道内存成本,这可能意味着您打算使用其中的很多。在 Java 中,您可以使用 BitSet 类,或者在较小的范围内,在两种语言中,您都可以使用按位操作来操作位。

回答by Mike Dimmick

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

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

回答by thenickdude

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 中,内存中枚举的每个值应该只有一个实例。对枚举的引用然后只需要该引用的存储。检查枚举的值与任何其他参考比较一样有效。

回答by Tom

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,在某些情况下您可以使用 EnumSet。它在内部使用一个位向量,非常节省空间且速度很快。

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

回答by anjanb

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) 取决于枚举中的内容。我最近试图找到具有默认构造函数参数且内部没有存储对象的 ArrayList() 的大小(这意味着存储容量为 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)

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

BR,
~A

BR,
~A

回答by Andrew Edgecombe

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++ 中,枚举的大小通常与int. 也就是说,编译器提供命令行开关以允许将枚举的大小设置为适合定义的值范围的最小大小的情况并不少见。