@@ -290,18 +290,36 @@ public static long alignObjectSize(long size) {
290
290
* should be GCed.</p>
291
291
*/
292
292
public static long sizeOf (Object obj ) {
293
- return measureObjectSize (obj );
293
+ ArrayList <Object > stack = new ArrayList <Object >();
294
+ stack .add (obj );
295
+ return measureSizeOf (stack );
294
296
}
295
297
296
298
/**
297
- * Same as {@link #sizeOf(Object)} but sums up all the arguments.
299
+ * Same as {@link #sizeOf(Object)} but sums up all the arguments. Not an
300
+ * overload to prevent accidental parameter conflicts with {@link #sizeOf(Object)}.
298
301
*/
299
- public static long sizeOf (Object ... obj ) {
300
- long sum = 0 ;
301
- for (Object o : obj ) {
302
- sum += measureObjectSize (o );
302
+ public static long sizeOfAll (Object ... objects ) {
303
+ return sizeOfAll (Arrays .asList (objects ));
304
+ }
305
+
306
+ /**
307
+ * Same as {@link #sizeOf(Object)} but sums up all the arguments. Not an
308
+ * overload to prevent accidental parameter conflicts with {@link #sizeOf(Object)}.
309
+ */
310
+ public static long sizeOfAll (Iterable <Object > objects ) {
311
+ final ArrayList <Object > stack ;
312
+ if (objects instanceof Collection <?>) {
313
+ stack = new ArrayList <Object >(((Collection <?>) objects ).size ());
314
+ } else {
315
+ stack = new ArrayList <Object >();
303
316
}
304
- return sum ;
317
+
318
+ for (Object o : objects ) {
319
+ stack .add (o );
320
+ }
321
+
322
+ return measureSizeOf (stack );
305
323
}
306
324
307
325
/**
@@ -321,6 +339,28 @@ public static long shallowSizeOf(Object obj) {
321
339
}
322
340
}
323
341
342
+ /**
343
+ * Same as {@link #shallowSizeOf(Object)} but sums up all the arguments. Not an
344
+ * overload to prevent accidental parameter conflicts with {@link #shallowSizeOf(Object)}.
345
+ */
346
+ public static long shallowSizeOfAll (Object ... objects ) {
347
+ return shallowSizeOfAll (Arrays .asList (objects ));
348
+ }
349
+
350
+ /**
351
+ * Same as {@link #shallowSizeOf(Object)} but sums up all the arguments. Duplicate
352
+ * objects are not resolved and will be counted twice.
353
+ * Not an overload to prevent accidental parameter conflicts with
354
+ * {@link #shallowSizeOf(Object)}.
355
+ */
356
+ public static long shallowSizeOfAll (Iterable <Object > objects ) {
357
+ long sum = 0 ;
358
+ for (Object o : objects ) {
359
+ sum += shallowSizeOf (o );
360
+ }
361
+ return sum ;
362
+ }
363
+
324
364
/**
325
365
* Returns the shallow instance size in bytes an instance of the given class would occupy.
326
366
* This works with all conventional classes and primitive types, but not with arrays
@@ -378,18 +418,17 @@ private static long shallowSizeOfArray(Object array) {
378
418
return alignObjectSize (size );
379
419
}
380
420
381
- /*
421
+ /**
382
422
* Non-recursive version of object descend. This consumes more memory than recursive in-depth
383
423
* traversal but prevents stack overflows on long chains of objects
384
424
* or complex graphs (a max. recursion depth on my machine was ~5000 objects linked in a chain
385
- * so not too much).
425
+ * so not too much).
426
+ *
427
+ * @param stack Root objects.
386
428
*/
387
- private static long measureObjectSize ( Object root ) {
429
+ private static long measureSizeOf ( ArrayList < Object > stack ) {
388
430
final IdentityHashSet <Object > seen = new IdentityHashSet <Object >();
389
431
final IdentityHashMap <Class <?>, ClassCache > classCache = new IdentityHashMap <Class <?>, ClassCache >();
390
- final ArrayList <Object > stack = new ArrayList <Object >();
391
-
392
- stack .add (root );
393
432
394
433
long totalSize = 0 ;
395
434
while (!stack .isEmpty ()) {
0 commit comments