Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the total memory allocated by an object [duplicate]

Possible Duplicate:
In Java, what is the best way to determine the size of an object?

If we create instance variables and instance methods inside a class then how to find out the total memory allocated for each Object of this class, which as shown as below eg, i think it is some of variables (a,b) size and method area size(display). is it true? what about main method area which is written inside a class.

for example:

class A {  
    int a;
    float b;
    void display()
    {  
       ---
    }

    public static void main(String a[])
    { 
        A obj=new A();
    }
}
like image 625
user1335578 Avatar asked May 05 '12 07:05

user1335578


2 Answers

The short answer is to you don't need to know unless you know its a problem.

The longer answer is that it will use about 16 bytes of the header of the object, 4 bytes for each field (and rounding the total to an 8 byte boundary) i.e. 24 bytes.

local variables e.g. references are allocated on the stack and usually are not counted.

The reason you don't usually bother with the size is that 1 MB is worth about 2 seconds of your time (comparing retail price for memory with the minimum wage) This means the time it takes you to ask the question is worth far more than the value of the memory you could possibly save (in this case)

[A 4 GB module costs £18 and minimum wage is £8.50 so 1 GB is worth about 0.5 hours. 1 MB is worth about 2 seconds.]

Put another way, if a simple change doesn't save you 1 MB of memory you have a better use for, you could be wasting your time.

The same applies for mobile devices but to a lesser degree (memory costs more and if its deployed to thousands of devices the total cost of saving a few hundred bytes can matter)

A consequence of this is that the time it takes you to write long instead of int or double instead of float (They both have one extra character) can be worth thousands of times more than the extra memory it uses. However I would still suggest almost always using double instead of float and often long instead of int, because if it saves one bug it will more than make up for the extra characters you had to type.


The simplest way to measure how much space an object uses (or how much memory is used to create an object) is to turn the TLAB off -XX:-UseTLAB and measure the amount of memory before and after the object is created with Runtime.totalMemory - freeMemory.

However, I prefer to wait until a data structure shows up in a profiler like YourKit (VisualVM is free) before I consider it a problem.

like image 116
Peter Lawrey Avatar answered Sep 21 '22 04:09

Peter Lawrey


You can use the java.lang.instrumentation package: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/Instrumentation.html

Compile and put this class in a JAR:

import java.lang.instrument.Instrumentation;

public class ObjectSizeFetcher {
    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

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

Add the following to your MANIFEST.MF:

Premain-Class: ObjectSizeFetcher

Use getObjectSize():

public class C {
    private int x;
    private int y;

    public static void main(String [] args) {
        System.out.println(ObjectSizeFetcher.getObjectSize(new C()));
    }
}

invoke the file using java -javaagent:ObjectSizeFetcherAgent.jar C

like image 39
Vicky Avatar answered Sep 18 '22 04:09

Vicky