]> source.dussan.org Git - aspectj.git/commitdiff
JoinPointImpl: Remove thread-locals after usage, where possible
authorKimmingLau <294001791@qq.com>
Wed, 3 Apr 2024 20:20:18 +0000 (04:20 +0800)
committerAlexander Kriegisch <Alexander@Kriegisch.name>
Wed, 10 Apr 2024 09:23:00 +0000 (11:23 +0200)
Avoid potential ThreadLocalMap.Entry accumulation. Entry is a
WeakReference. When JoinPointImpl objects are collected by GC, Entry
instances are still be referenced by ThreadLocalMap, which leads to
memory pressure and potentially more full GCs. So, we proactively remove
ThreadLocal<Integer> arcIndex instances when arcIndex has been
decremented back to -1 per thread. This is not perfect, because not each
thread can be expected to proceed, but it should ameliorate the
situation to some degree.

Fixes #302.

Co-authored-by: Alexander Kriegisch <Alexander@Kriegisch.name>
Signed-off-by: KimmingLau <294001791@qq.com>
Signed-off-by: Alexander Kriegisch <Alexander@Kriegisch.name>
runtime/src/main/java/org/aspectj/runtime/reflect/JoinPointImpl.java

index 8db5e07308fdb48b4030a6cba8a1a54e53e8a48d..01def84518e190f6c9496f8f4fb5c0aa201cc17e 100644 (file)
@@ -154,7 +154,11 @@ class JoinPointImpl implements ProceedingJoinPoint {
                        arcs = new ArrayList<>();
                }
                if (arc == null) {
-                       arcIndex.set(arcIndex.get() - 1);
+                       int newIndex = arcIndex.get() - 1;
+                       if (newIndex > -1)
+                               arcIndex.set(newIndex);
+                       else
+                               arcIndex.remove();
                }
                else {
                        this.arcs.add(arc);