Horje
How to Calculate Size of Object in Java?

In Java, an object is an instance of a class that encapsulates data and behavior. Calculating the size of an object can be essential for memory management and optimization. This process involves understanding the memory layout of the object, including its fields and the overhead introduced by the JVM.

In this article, we will learn to calculate size of object in Java.

Declaration:

public static long getObjectSize(Object object);

Return Value: Size of the specified object in bytes as a long.

Program to Calculate Size of Object in Java

Below is the implementation to Calculate Size of Object in Java:

Java
// Java program to calculate the size of an object
// Importing required classes

import java.lang.instrument.Instrumentation;

// Main Class
public class SizeCalculate {
    // Volatile variable to hold the global Instrumentation instance
    private static volatile Instrumentation globalInstrumentation;

    // premain method to initialize the Instrumentation instance
    public static void premain(String agentArgs, Instrumentation inst) 
    {
        globalInstrumentation = inst;
    }

    // Method to get the size of an object
    public static long getObjectSize(Object object) {
        if (globalInstrumentation == null) {
            throw new IllegalStateException("Agent not initialized.");
        }
        return globalInstrumentation.getObjectSize(object);
    }

    // Main driver method
    public static void main(String[] args) {
        String example = "GeeksforGeeks";
        System.out.println("Size of the object: " + getObjectSize(example) + " bytes");
    }
}

Executing the Program:

Step 1: First compile the Java code using the below command in the IDE terminal:

javac SizeCalculate.java

Step 2: Once the code is compiled, we can run the application by executing the below command in the terminal.

java -javaagent:SizeCalculate.jar SizeCalculate

The console output should display the size of the object:

Note: Output may vary slightly depending on the Java version and implementation you are using.

Output:

Console Output


Explanation of the above Program:

  • The premain method initializes the globalInstrumentation variable with the provided Instrumentation instance.
  • The getObjectSize method uses globalInstrumentation to get the size of an object.
  • The main method demonstrates object size calculation for a sample string “GeeksforGeeks“.

Complexity of the Above Method:

Time Complexity: O(1)
Auxiliary Space: O(1), as no extra space is used




Reffered: https://www.geeksforgeeks.org


Java

Related
Java Program to Search an Element in Vector Java Program to Search an Element in Vector
Java Program to Solve the Fractional Knapsack Problem Java Program to Solve the Fractional Knapsack Problem
Prim's Algorithm with a Java Implementation Prim's Algorithm with a Java Implementation
Red Black Tree Java Red Black Tree Java
How to Draw a Car using Java Applet? How to Draw a Car using Java Applet?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17