kurryt kurryt - 7 months ago 37
Java Question

Why do Java Enums uses more memory than Interfaces?

I tested class, enum, interface these 3 ways to encapsulate constant String.

public class Company {
public final static String CAPITAL_ONE = "Capital_One";
public interface ICompany {
public final static String CAPITAL_ONE = "Capital_One";
public enum ECompany {

After compile, they generated 330 bytes, 181 bytes and 818 bytes bytecode which means interface ICompany will cost less memory when loaded into jvm. Why is this?


Use the javap utility to examine the 3 ".class" files and compare the outputs. For example:

$ javap -c Company.class 

The short answer is there are some standard methods (values(), valueOf(String), toString()) that the enum class has to implement, but the other classes don't have to.

Having said that, the size of a ".class" file is not necessarily an accurate predictor of the memory used when a class has been loaded and JIT compiled.