4 Replies Latest reply: Oct 23, 2010 6:26 AM by 796440 RSS

    Object Size in java

    806345
      hi
      i want a static method which can find the size of any object passed to it . the size must be accurate in kb or bytes. the input to the method can be any object like arrays, vectors, hashmaps or just normal objects
        • 1. Re: Object Size in java
          796440
          803342 wrote:
          hi
          i want a static method which can find the size of any object passed to it .
          Not possible.

          1. Object size is implementation-dependent, so it can't be calculated.

          2. Object size is not exposed.

          3. Things like the constant pool and multiple references to the same object can throw the count off.

          You can create a lot of objects, and call the various memory methods in the System class (or maybe it's Runtime), to get a rough idea. Or you could use a proflier.

          If you want to try to calculate the sizes programatically, you can use these as lower bounds. A given implementation might make things bigger, to align on word boundaries, and the constant pool might make things smaller.

          object: 12 bytes.
          reference: 4 bytes.
          byte: 1 byte (although I think the spec may say that it's stored in an int, which would make it 4 bytes)
          char: 2 bytes
          short: 2 bytes
          int: 4 bytes
          long: 8 bytes
          float: 4 bytes
          double: 8 bytes
          boolean: 1 byte (although I think the spec may say that it's stored in an int, which would make it 4 bytes)
          • 2. Re: Object Size in java
            jschellSomeoneStoleMyAlias
            jverd wrote:
            byte: 1 byte (although I think the spec may say that it's stored in an int, which would make it 4 bytes)
            boolean: 1 byte (although I think the spec may say that it's stored in an int, which would make it 4 bytes)
            In arrays they will be bytes. I wouldn't be surprised if members were 4 bytes though.
            Although I wonder then what they would be in a 64 bit vm.
            • 3. Re: Object Size in java
              tschodt
              803342 wrote:
              i want a static method which can find the size of any object passed to it.
              the size must be accurate in kb or bytes.
              the input to the method can be any object like arrays, vectors, hashmaps or just normal objects
              With the proviso that the object passed in must implement Serializable
              static int sizeof(Serializable obj) {
                 try {
                    java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
                    new java.io.ObjectOutputStream(baos).writeObject(obj);
                    return baos.size();
                 } catch(IOException ignore) {}
                 return 0;
              }
              Arrays and Collections are Serializable
              but that method will not work
              if they store non-Serializable objects.

              I have no idea if the return value is "accurate".
              I have no idea what "accurate" means in this context
              as Java does not define what is understood by the "size" of an object.
              • 4. Re: Object Size in java
                796440
                tschodt wrote:
                With the proviso that the object passed in must implement Serializable
                ...
                I have no idea if the return value is "accurate".
                Probably not, as there's no reason to assume that the serialized form of an object is simply a direct copy of its bytes in memory. In fact, it can't be, else it wouldn't be portable across JVM implementations.