0

I want to get the size of the object I built with java.reflection. Here is the code. I don't understand what I'm doing wrong. When I get the object I'm tried to use instrument interface but it doesn't work. Thanks for helping !

public String analayzeData(Object obj) throws IllegalArgumentException, IllegalAccessException{
        Class c =  obj.getClass();
        System.out.println(sizer.getObjectSize(obj));
        String dataText="*************Start of " + c.getSimpleName() + " Class*************\n";
        Class superclass = c.getSuperclass();
        if(superclass!=null && !superclass.isAssignableFrom(Object.class)){
            Field[] superField = superclass.getDeclaredFields();
            for(Field field : superField){
                field.setAccessible(true);
                Object fieldVal = field.get(obj);
                dataText+=field.getName() + " = " + fieldVal + "\n";
            }
        }
    //  Instrumentation objectSizes =null;

        Field[] fieldType= c.getDeclaredFields();
        for(Field field : fieldType){
            field.setAccessible(true);
            dataText+=field.getName() + ":";
            Object fieldVal = field.get(obj);
            if(fieldVal instanceof ArrayList){
                ArrayList<?> arrayListField = (ArrayList<?>)fieldVal;
                dataText+="Size of " + field.getName() +" ArrayList = " + arrayListField.size() + "\n";
                for (int i=0; i < arrayListField.size();i++){
                    dataText+=analayzeData(arrayListField.get(i));
                }
                dataText+="****End of " + field.getName()  + "List****\n";
            }
            else
                dataText+= " = " + fieldVal+"\n";
        }
        dataText+="*************End of " + c.getSimpleName() + " Class*************\n";
        return dataText ;
    }

}
class sizer{
    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

    public static long getObjectSize(Object o) {
        return instrumentation.getObjectSize(o);
    }

}
Ofir N
  • 89
  • 1
  • 2
  • 8

1 Answers1

0

Not sure if I understood your question correctly, but I guess you are wondering why the size of the object you print is (almost) always the same. A few examples using your analyzeData method:

analayzeData("123456789123456789123456789"); // will print a size of 16
analayzeData("123456789"); // will also print a size of 16
analayzeData(new BigInteger("1")) // will print a size of 32!
analayzeData(Arrays.asList(1, 2, 3, 4, 5)); // will print a size of 16

The reason for these counter-intuitive results is that Instrumentation.getObjectSize(Object) returns the (estimated) size of the object itself and not the size of the object plus all it's members/fields.

So for an ArrayList with 1000 elements the result will still be 16, because that is the size of the ArrayList - the size of it's members will have to be calculated separately.

Edit

removed dead links

nyname00
  • 2,496
  • 2
  • 22
  • 25
  • The problem is with the instrumentation that not return nothing – Ofir N Apr 20 '16 at 13:18
  • @OfirN how are you invoking your tests? Using a setup similar to the one posted [here](http://blog.javabenchmark.org/2013/05/java-instrumentation-tutorial.html) works for me – nyname00 Apr 20 '16 at 13:43
  • @nyname00 link is not available "with maven" and the other link you have given on commnet is also not available – rajeev pani.. Sep 11 '19 at 16:58
  • @rajeevpani.. the linked question has this topic covered, I think. Check out the links there – nyname00 Sep 12 '19 at 09:27