From 0528eca3ca3e24b61b8831063716fa9cb83d93cb Mon Sep 17 00:00:00 2001 From: zhouhuidong Date: Thu, 17 Mar 2022 19:15:24 +0800 Subject: [PATCH] [HUDI-3652] use threadlocal to decrease ObjectSizeCalculator memory use [HUDI-3652] Make ObjectSizeCalculator threadlocal to reduce memory footprint --- .../org/apache/hudi/common/util/ObjectSizeCalculator.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/hudi-common/src/main/java/org/apache/hudi/common/util/ObjectSizeCalculator.java b/hudi-common/src/main/java/org/apache/hudi/common/util/ObjectSizeCalculator.java index 7e625e8eb478f..171b745795e14 100644 --- a/hudi-common/src/main/java/org/apache/hudi/common/util/ObjectSizeCalculator.java +++ b/hudi-common/src/main/java/org/apache/hudi/common/util/ObjectSizeCalculator.java @@ -54,6 +54,8 @@ * @author Attila Szegedi */ public class ObjectSizeCalculator { + private static final ThreadLocal objectSizeCalculator = ThreadLocal.withInitial(() -> new ObjectSizeCalculator(CurrentLayout.SPEC)); + private static class CurrentLayout { private static final MemoryLayoutSpecification SPEC = getEffectiveMemoryLayoutSpecification(); @@ -71,7 +73,7 @@ private static class CurrentLayout { * @throws UnsupportedOperationException if the current vm memory layout cannot be detected. */ public static long getObjectSize(Object obj) throws UnsupportedOperationException { - return obj == null ? 0 : new ObjectSizeCalculator(CurrentLayout.SPEC).calculateObjectSize(obj); + return obj == null ? 0 : objectSizeCalculator.get().calculateObjectSize(obj); } // Fixed object header size for arrays.