public static ObjectSize deepSizeOf ( Object obj , IObjectSizeFilter filter , int maxObjects ) { Map < Object , Object > visited = new IdentityHashMap < Object , Object > ( ) ; Stack < ObjectEntry > stack = new Stack < ObjectEntry > ( ) ; InvocationCounter sizeHistogram = new InvocationCounter ( false ) ; long result = internalSizeOf ( new ObjectEntry ( obj , "" , "" ) , stack , visited , filter , "" ) ; sizeHistogram . recordInvocation ( obj . getClass ( ) . getName ( ) , ( int ) result ) ; int n = 1 ; while ( ! stack . isEmpty ( ) ) { ObjectEntry entry = stack . pop ( ) ; long size = internalSizeOf ( entry , stack , visited , filter , entry . indent ) ; result += size ; n ++ ; sizeHistogram . recordInvocation ( entry . object . getClass ( ) . getName ( ) , ( int ) size ) ; if ( n >= maxObjects ) { return new ObjectSize ( result , false ) ; } } visited . clear ( ) ; if ( VERBOSE ) { System . out . println ( ) ; System . out . println ( "-------------------------------------------------" ) ; sizeHistogram . print ( ) ; } return new ObjectSize ( result , true ) ; }
Calculates full size of object iterating over its hierarchy graph .