|
26 | 26 |
|
27 | 27 | import java.io.File; |
28 | 28 | import java.io.PrintWriter; |
| 29 | +import java.lang.invoke.MethodHandles; |
| 30 | +import java.lang.invoke.VarHandle; |
29 | 31 | import java.lang.management.GarbageCollectorMXBean; |
30 | 32 | import java.lang.management.ManagementFactory; |
31 | 33 | import java.lang.management.OperatingSystemMXBean; |
@@ -441,14 +443,28 @@ private static Map<String, Long> calculateCodeBreakdown(Collection<CompileTask> |
441 | 443 | return classNameToCodeSize; |
442 | 444 | } |
443 | 445 |
|
| 446 | + private static final VarHandle STRING_VALUE; |
| 447 | + static { |
| 448 | + try { |
| 449 | + MethodHandles.Lookup privateLookup = MethodHandles.privateLookupIn(String.class, MethodHandles.lookup()); |
| 450 | + STRING_VALUE = privateLookup.unreflectVarHandle(String.class.getDeclaredField("value")); |
| 451 | + } catch (ReflectiveOperationException e) { |
| 452 | + throw new ExceptionInInitializerError(e); |
| 453 | + } |
| 454 | + } |
| 455 | + |
| 456 | + private static int getInternalByteArrayLength(String string) { |
| 457 | + return ((byte[]) STRING_VALUE.get(string)).length; |
| 458 | + } |
| 459 | + |
444 | 460 | private Map<String, Long> calculateHeapBreakdown(Collection<ObjectInfo> heapObjects) { |
445 | 461 | Map<String, Long> classNameToSize = new HashMap<>(); |
446 | 462 | long stringByteLength = 0; |
447 | 463 | for (ObjectInfo o : heapObjects) { |
448 | 464 | classNameToSize.merge(o.getClazz().toJavaName(true), o.getSize(), Long::sum); |
449 | 465 | Object javaObject = o.getObject(); |
450 | 466 | if (javaObject instanceof String) { |
451 | | - stringByteLength += StringAccess.getInternalByteArrayLength((String) javaObject); |
| 467 | + stringByteLength += getInternalByteArrayLength((String) javaObject); |
452 | 468 | } |
453 | 469 | } |
454 | 470 |
|
|
0 commit comments