Javatpoint Logo
Javatpoint Logo

Size of Empty Class in Java

In Java, the size of an object is determined through the sum of the sizes of its instance variables. However, when it comes to an empty magnificence, i.e., a category with no example variables, there is no obvious answer to the query of ways plenty reminiscence it occupies. It is because the JVM (Java Virtual Machine) has a minimum item size, that's usually eight bytes, and this is the dimensions of the smallest feasible item that can be allocated in Java. To recognize the idea of the size of an empty class in Java, allow us to first discuss the memory allocation process in Java. When an item is created in Java, the JVM allocates memory at the heap to keep the object's instance variables. The size of the allotted memory relies upon on the type and wide variety of example variables in the object.

However, the JVM has a minimal object size, that's typically 8 bytes, and that is the dimensions of the smallest possible item that may be allocated in Java. This approach that even though an object has no instance variables, it'll still occupy 8 bytes of memory.

Let's test an example program to understand this idea better:

It is an empty class without a instance variables. Let's use the following code to decide the scale of this elegance in Java:

Size.java

Output:

Size of EmptyClass object: 16

In the above code, we first create an instance of the EmptyClass and then use the Runtime elegance to calculate the memory occupied by way of the JVM. The totalMemory() approach returns the whole amount of memory that the JVM has allotted for the application, and the freeMemory() approach returns the quantity of loose memory within the JVM. By subtracting the free memory from the whole memory, we will get the quantity of reminiscence occupied via the EmptyClass item. As mentioned in advance, the minimum object size in Java is normally eight bytes. However, in this case, the dimensions of the EmptyClass item is 16 bytes. This is due to the fact the JVM provides some extra metadata to each item it allocates. In the case of an empty magnificence, this metadata consists of a connection with the class's metadata and a connection with the class's remarkable metadata. Thus, despite the fact that the EmptyClass has no instance variables, it still occupies sixteen bytes of memory. It's essential to be aware that the real size of an item in Java might also range depending at the JVM implementation, the working device, and the hardware.

Let's take a look at some other example to apprehend how the scale of an object varies primarily based on the instance variables it consists of:

In this example, we have a class called MyClass with three instance variables of type int, char, and boolean. Let's use the following program to determine the size of this class in Java:

SizeOfClass.java

Output:

Size of MyClass object: 16

As we are able to see from the output, the scale of the MyClass item is likewise 16 bytes. This is because every int variable occupies 4 bytes, the char variable occupies 2 bytes, and the boolean variable occupies 1 byte. Therefore, the entire length of the instance variables is 7 bytes, but the JVM adds 9 bytes of metadata to the object, ensuing in a complete length of sixteen bytes. It's crucial to note that the size of an item in Java can also be tormented by padding. Padding is the technique of including more bytes to an item to make certain that its instance variables are aligned on a memory boundary. This can enhance overall performance by using making it simpler and faster for the CPU to study and write reminiscence.

For example, let's say we have a class with two int variables:

In this example, the two int variables together occupy 8 bytes (4 bytes each). However, the JVM might also add greater padding bytes to the item to ensure that the int variables are aligned on a reminiscence boundary (commonly 4 bytes). This may additionally result in the item occupying more than eight bytes of memory.

In Summary, the size of an empty elegance in Java isn't zero but usually 8 bytes (the minimum item length in Java). This is because the JVM provides a few more metadata to each item it allocates, such as a connection with the elegance's metadata and a reference to the elegance's extremely good metadata. The size of an object in Java can also be tormented by the instance variables it consists of and padding. It's important to be aware that the actual length of an item in Java can also vary relying at the JVM implementation, the operating gadget, and the hardware.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA