Thread: Interview questions

Forum : New To Java   14 May, 2018 23:58
User Image


Joined: 11 Jul, 2017

Points: 100

Threads: 9

Posts: 1

Interview questions

| Quote Date : 14 May, 2018    Views:756    


Q.what is the size of object in java?
Q.what is minimum size of object in java? to find out size of object in java?
Q.what is the size of reference variable in java?


User Image


Joined : 15 Jul, 2016

Points : 1000

Threads: 645

Posts: 201

Re: Interview questions

Reply Date : 18 May, 2018

The size of an Object can be determined based on the instance variables defined as part of the Object as members don't share the object space. There is a way to do in Java using Instrumentation class.

Generally the java.lang.instrument.Instrumentation is used by Tools like the profilers, monitoring agents and code coverage analysis tools to instrument the bytecode of a Class.

In addition, there are methods in this class you can use for getting the size of an object for which in your class you need to write a method called premain that is called by the JVM before calling the main method of the class by passing Instrumentation object as a parameter. You cannot create Instrumentation object by your own.

You can use the object to fetch the size of the object as shown below.
In the below class we wrote a premain method that will be called by javaagent at the time of launching the application

package com.inst.analyzer;

import java.lang.instrument.Instrumentation;

public class ObjectAnalyzer {
private static Instrumentation instrumentation;

public static void premain(String args, Instrumentation instrumentation) {
System.out.println("args : "+ args);
ObjectAnalyzer.instrumentation = instrumentation;

public static long getObjectSize(Object obj) {
return instrumentation.getObjectSize(obj);

I took an empty class below to make you understand the size of an empty object.

package com.inst.beans;

 * No attributes are there in this class which you treat as an empty object
public class Calendar {
public String[] getHolidaysOfTheMonth(String month) {
return new String[] { "1", "10" };

Now create a main class in which instantiate the object of Calendar and pass to Object analyzer.

package com.inst.test;
public class ObjectTest {
public static void main(String[] args) {
Calendar calendar = new Calendar();
long size = ObjectAnalyzer.getObjectSize(calendar);
System.out.println("Object size : " + size);

We should run this code using javaagent and follow the steps below to run this code.

  1. Create a file like my.txt with the below command to include it as manifest file in Java
    • echo Premain-Class: com.h5b.entities.ObjectAnalyzer >> my.txt
  2. After compiling the above class package them as jar along with my.txt file created above as manifest file contents.
    • jar -cvmf my.txt agent.jar .
  3. Now run the Main Class in the jar using the below command.
    • java -javaagent:agent.jar com.h5b.entities.ObjectTest
?You should be able to see the default object size as 16.

User Image


Joined : 8 May, 2017

Points : 230

Threads: 57

Posts: 216

Re: Interview questions

Reply Date : 15 May, 2018

To find size of object we need to consider all the instance variable. We have predefined class called Instrumentation?  in lang package.

Minimum size of object depends on jvm size, and how many instance variable we have declared. 

Correct me if my answer is wrong. 

Load More

No More Comments