|
36 | 36 | import java.util.Collections; |
37 | 37 | import java.util.List; |
38 | 38 |
|
| 39 | +import com.google.common.base.Supplier; |
| 40 | +import com.google.common.base.Suppliers; |
39 | 41 | import com.google.common.collect.Iterables; |
40 | 42 | import com.sun.tools.hat.internal.parser.ReadBuffer; |
41 | 43 |
|
@@ -76,8 +78,7 @@ public class JavaClass extends JavaHeapObject { |
76 | 78 | // Total number of fields including inherited ones |
77 | 79 | private int totalNumFields; |
78 | 80 | // Cache of total instance size |
79 | | - private volatile long totalInstanceSize = -1L; |
80 | | - |
| 81 | + private final Supplier<Long> totalInstanceSizeSupplier = Suppliers.memoize(this::getTotalInstanceSizeSupplierImpl); |
81 | 82 |
|
82 | 83 | public JavaClass(long id, String name, long superclassId, long loaderId, |
83 | 84 | long signersId, long protDomainId, |
@@ -388,14 +389,10 @@ public int getInstanceSize() { |
388 | 389 | * arrays. |
389 | 390 | */ |
390 | 391 | public long getTotalInstanceSize() { |
391 | | - if (totalInstanceSize >= 0) { |
392 | | - return totalInstanceSize; |
393 | | - } else { |
394 | | - return cacheTotalInstanceSize(); |
395 | | - } |
| 392 | + return totalInstanceSizeSupplier.get(); |
396 | 393 | } |
397 | 394 |
|
398 | | - public synchronized long cacheTotalInstanceSize() { |
| 395 | + private long getTotalInstanceSizeSupplierImpl() { |
399 | 396 | int count = instances.size(); |
400 | 397 | if (count == 0 || !isArray()) { |
401 | 398 | totalInstanceSize = (long) count * instanceSize; |
|
0 commit comments