The Source for Java Technology Collaboration
User: Password:



Michael Nascimento Santos

Michael Nascimento Santos's Blog

Playing with the Tiger: Measuring the size of your objects

Posted by mister__m on February 05, 2004 at 07:14 PM | Comments (4)

As I said, I'm back with more on the new JDK 1.5.

There is a new package called java.lang.instrument that allows you to intercept a class before being loaded and modify its bytecode, for example (can I hear standard entry point for AOP support? :-P). Well, let's use it for something different: measuring the size of some objects. Here is the code:

import java.lang.instrument.*;
import java.util.*;

public class InstrumentationTest {
   private static Instrumentation inst;

   public static void premain(String options, Instrumentation inst) {
      InstrumentationTest.inst = inst;

      System.out.println("options= " + options);

      // Get all classes currently loaded by VM
      Class[] loaded = inst.getAllLoadedClasses();

      // Sort them by name
      Arrays.sort(loaded, new Comparator() {
         public int compare(Class c1, Class c2) {
            return c1.getName().compareTo(c2.getName());
         }
      });

      //And print them!
      for (Class clazz : loaded) {
         System.out.println(clazz);
      }
   }

   public static long sizeOf(Object o) {
      assert inst != null;

      return inst.getObjectSize(o);
   }

   public static void main(String[] args) {
      System.out.println("Size of Object: " + sizeOf(new Object()));
      System.out.println("Size of direct subclass: " + sizeOf(
            new InstrumentationTest()));
      System.out.println("Size of Calendar: " + sizeOf(Calendar.getInstance()));

   }
}

Save it as InstrumentationTest.java and compile it as shown bellow:

javac -source 1.5 InstrumentationTest.java

To allow our class to be useful, we have to start the VM using this verbose command:

java -ea -javaagent:InstrumentationTest -cp . InstrumentationTest

Someone might be asking how it could be useful. As a friend of mine, Bruno Borges, suggested to me, it could give you a good idea if Prevayler is the right tool for your needs.

Hope you've enjoyed it. More to come!


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Prevayler sux... :)
    Man, you know I still don't believe on how can be good Prevayler... How could I have two systems accessing the same database, if data is "alive" on the memory? :P

    Hibernate continues to be my first choice... :P

    Nothing against you...

    And congratulations for your blog man, it's a very good-hyper-nice work. Hope 1.5 can do much more... (and of course, be fast!). See ya dude!

    Posted by: miojo on February 05, 2004 at 07:25 PM

  • Prevayler sux... :)
    > How could I have two systems accessing
    > the same database, if data is "alive" on the
    > memory? :P

    Well, we are not talking about databases, but about object prevalence to begin with. Any kind of systems integration strategy will do the trick.

    > Hibernate continues to be my first choice... :P

    I also love Hibernate :-D

    > And congratulations for your blog man, it's a very good-hyper-nice work.

    Thanx a lot :-D Maybe I should blog specifically about Prevayler :-P

    Posted by: mister__m on February 05, 2004 at 07:29 PM

  • AMD versus Intel
    Maybe we could make some benchmarks (I have fear when this word appears) running those codes to see where 1.5 can have more performance... :) How's that?

    You know I have an Athlon XP 2800... Let's do it?

    Posted by: miojo on February 05, 2004 at 07:29 PM

  • Not just for AOP
    Its not just AOP that benefits from this facility -- profilers will as well. (We used to have access to this facility through JVMPI, but that required writing C code.)

    Posted by: briangoetz on February 07, 2004 at 08:49 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds