Here's a concrete example. First, the Java class: public class JvmtiSize {
public static native long getObjectSize(Object o); public static void main(String[] args) { String s = "Hello, world!"; String[] sa = { s }; System.out.printf(" s: %6d bytes\n", getObjectSize(s)); System.out.printf(" sa: %6d bytes\n", getObjectSize(sa)); System.out.printf("class: %6d bytes\n", getObjectSize(JvmtiSize.class)); } } Then, run javah to generate JvmtiSize.h (omitted). Next, the C code (jvmti_size.c): #include "jvmti.h" #include "JvmtiSize.h" JavaVM *global_jvm = NULL; JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { global_jvm = jvm; return 0; } JNIEXPORT jlong JNICALL Java_JvmtiSize_getObjectSize(JNIEnv *env, jclass class, jobject object) { jint status; jvmtiEnv *jvmti; jvmtiError err; jlong object_size; status = (*global_jvm)->GetEnv(global_jvm, (void **)&jvmti, JVMTI_VERSION); if (status != JNI_OK) { fprintf(stderr, "ERROR: Unable to create jvmtiEnv (GetEnv failed), error=%d\n", status); exit(1); } err = (*jvmti)->GetObjectSize(jvmti, object, &object_size); if (err != JVMTI_ERROR_NONE) { fprintf(stderr, "ERROR: GetObjectSize failed, error=%d\n", err); exit(1); } return object_size; } Then, generate the shared lib (Solaris SPARC): cc -G -I${JAVA_HOME}/include -Isrc -I${JAVA_HOME}/include/solaris src/jvmti_size.c -o libjvmti_size.so Finally, give it a run: java -agentlib:jvmti_size -cp classes JvmtiSize s: 24 bytes sa: 16 bytes class: 288 bytes Quoting Kris Schneider <[EMAIL PROTECTED]>: > Not too surprising that it's available through a native programming > interface > (JVMTI) since it's really an implementation-dependent metric: > > http://java.sun.com/j2se/1.5.0/docs/guide/jvmti/jvmti.html#GetObjectSize > > Quoting Brian Lee <[EMAIL PROTECTED]>: > > > If you run this from a simple console test app, the JVM won't allocate any > > > extra objects between 2 and 4. > > > > Unfortunatly, this is the most exact way to find out memory usage > > (serialization size doesn't necessarily mean in memory size). > > > > Just wait til those slackers at Sun at a Object.sizeof() method in jdk1.9 > or > > > > something lame. > > > > BAL > > > > >From: Navjot Singh <[EMAIL PROTECTED]> > > >To: Struts Users Mailing List <[EMAIL PROTECTED]> > > >Subject: Re: [OT] how to calculate the size of an object > > >Date: Thu, 08 Jul 2004 22:16:12 +0530 > > > > > >hi, > > > > > >Thanks for the link but this is very naive way of doing it. I am leaving > it > > > > >to the mercy of gc. > > > > > >What this method is doing > > > > > >1. run gc() manually (AND hope it wont run automatically again soon.) > > >2. free memory > > >3. create and object. > > >4. free memory > > > > > >and now just wish that JVM wont allocate any memory in it's heap between > > >steps 2 & 4. so that one can assume that whatsoever output comes belongs > to > > > > >my object. I am at something better. > > > > > >Jim you are absolutely right, this technique may return a negative > number. > > > > > >navjot singh > > > > > > > > >[EMAIL PROTECTED] wrote: > > > > > >> > > >>http://www.google.com/search?hl=en&lr=&ie=UTF-8&q=size+java+object > > >> > > >>The first one looks promising. > > >> > > >>Dennis > > >> > > >> > > >> > > >> > > >>*Navjot Singh <[EMAIL PROTECTED]>* > > >> > > >>07/08/2004 11:57 AM > > >>Please respond to > > >>"Struts Users Mailing List" <[EMAIL PROTECTED]> > > >> > > >> > > >> > > >>To > > >> Struts Users Mailing List <[EMAIL PROTECTED]> > > >>cc > > >> > > >>Subject > > >> [OT] how to calculate the size of an object > > >> > > >> > > >> > > >> > > >> > > >> > > >> > > >> > > >>hi, > > >> > > >>I use SAX parser to load an LDIF file into memory. Whatsoever data i > > >>read, i fill into an object. > > >> > > >>I need to know *the size of LDIFData object* at runtime. How to do that? > > >> > > >>Well the class structure is something like this > > >> > > >>public class LDIFData{ > > >> ArrayList cards; // collection of Card > > >> String filename; > > >> long lastLoadedTime; > > >>} > > >> > > >>public class Card{ > > >> String name; > > >> String email > > >> String mobile; > > >>} > > >> > > >>-- > > >>regards > > >>Navjot Singh > > >> > > >>When you jump for joy, beware that no-one moves the ground from beneath > > >>your feet. -- Stanislaw Lem, "Unkempt Thoughts" > > >> > > >>--------------------------------------------------------------------- > > >>To unsubscribe, e-mail: [EMAIL PROTECTED] > > >>For additional commands, e-mail: [EMAIL PROTECTED] > > >> > > >> > > >> > > >>------------------------------------------------------------------------ > > >> > > >>--------------------------------------------------------------------- > > >>To unsubscribe, e-mail: [EMAIL PROTECTED] > > >>For additional commands, e-mail: [EMAIL PROTECTED] > > > > > >-- > > >regards > > >Navjot Singh > > > > > >When you jump for joy, beware that no-one moves the ground from beneath > > >your feet. -- Stanislaw Lem, "Unkempt Thoughts" > > -- > Kris Schneider <mailto:[EMAIL PROTECTED]> > D.O.Tech <http://www.dotech.com/> -- Kris Schneider <mailto:[EMAIL PROTECTED]> D.O.Tech <http://www.dotech.com/> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]