public static int sizeOfAnything(Object object) 
        throws CannotCalculateSizeException {
        // If the object is Cacheable, ask it its size.
        if (object == null) {
            return 0;
        }
        if (object instanceof Cacheable) {
            return ((Cacheable)object).getCachedSize();
        }
        // Check for other common types of objects put into cache.
        else if (object instanceof String) {
            return sizeOfString((String)object);
        }
        else if (object instanceof Long) {
            return sizeOfLong();
        }
        else if (object instanceof Integer) {
            return sizeOfObject() + sizeOfInt();
        }
        else if (object instanceof Double) {
            return sizeOfObject() + sizeOfDouble();
        }
        else if (object instanceof Boolean) {
            return sizeOfObject() + sizeOfBoolean();
        }
        else if (object instanceof Map) {
            return sizeOfMap((Map)object);
        }
        else if (object instanceof long[]) {
            long[] array = (long[])object;
            return sizeOfObject() + array.length * sizeOfLong();
        }
        else if (object instanceof Collection) {
            return sizeOfCollection((Collection)object);
        }
        else if (object instanceof byte[]) {
            byte [] array = (byte[])object;
            return sizeOfObject() + array.length;
        }
        // Default behavior -- serialize the object to determine its size.
        else {
            int size = 1;
            try {
                // Default to serializing the object out to determine size.
                CacheSizes.NullOutputStream out = new NullOutputStream();
                ObjectOutputStream outObj = new ObjectOutputStream(out);
                outObj.writeObject(object);
                size = out.size();
            }
            catch (IOException ioe) {
                throw new CannotCalculateSizeException(object);
            }
            return size;
        }
    }
Returns the size of an object in bytes. Determining size by serialization
is only used as a last resort.

@param object the object to calculate the size of
@return the size of an object in bytes.
@throws CannotCalculateSizeException if the size cannot be calculated
xmppserver/src/main/java/org/jivesoftware/util/cache/CacheSizes.java