How to find the shared memory allocated by an object

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 shared memory allocated for each object of this class, which, as shown below, for example, I believe that these are some of the variables of size and method area (a,b)size (display). it's true? what about the main area of ​​the method that is written inside the class.

eg:

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

    public static void main(String a[])
    { 
        A obj=new A();
    }
}
+3
source share
2 answers

You do not need to know the short answer if you do not know its problems.

, 16 , 4 ( 8 ), 24 .

, . .

, , , 1 2 ( ). , , , , , , ( )

[ 4 18 , - 8,50 , 1 - 0,5 . 1 2 .]

, 1 , , .

, ( , , )

, , long int double float ( ), , , . double float int, , , , , .


, ( ), - TLAB -XX:-UseTLAB Runtime.totalMemory - freeMemory.

, , , MyKit (VisualVM ), .

+8

java.lang.instrumentation: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/instrument/Instrumentation.html

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);
    }
}

MANIFEST.MF :

Premain-Class: ObjectSizeFetcher

getObjectSize():

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

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

java -javaagent:ObjectSizeFetcherAgent.jar C

+1

All Articles