0 Replies Latest reply: Jun 29, 2013 12:09 AM by Zachary1234 RSS

    Obtaining the size of any object in memory easily.

    Zachary1234

      -I have tried to use the instrumentation api in java 1.6.0, but found that I sinply can't

      get it to work, certainly outside of a jar file, since I don't understand how to set up

      the System.setProperty("property","value") calls correctly to avoid using any keyboard input arguments.

       

      At any rate,  I have discovered the DataFlavor class, which I leverage as follows:

       

      /*

               Byte item = new Byte((byte)1);

               DataFlavor dataFlavor = new DataFlavor(item.getClass(),"java.lang.Byte");

               ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();

               ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteOutputStream);

               dataFlavor.writeExternal(objectOutputStream);

               objectOutputStream.flush();  

               objectOutputStream.close();

               int total  = byteOutputStream.toByteArray().length;

               out.println("number of characters: " + item.length());

               out.println("Number of bytes: " + total);

      */

       

               -How exactly does this put an object into the ByteArrayOutputStream?

                Can I get the bytes and count them for a good representation of an object's size in memory?

                However, is this doing a shallow or a deep object write?

                Does it include anything contained inside annotations or not?


      -In terms of 1024 bytes per kilobyte, and 8 bits per byte,

      how many bit (full) may be stored in a java byte typed variable, as a check?

      Will it always use a 9th bit to store + or - anyway (always a 9th for +) ?