From 3a79d7fb4892aee450fbb1f191fb13d12e4416f7 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 19 Mar 2019 09:51:29 +0100 Subject: [PATCH] move reachabilityFence to ObjectUtils --- .../fixtures/impl/CodeInsightTestFixtureImpl.java | 3 +-- platform/util/src/com/intellij/util/ObjectUtils.java | 7 +++++++ platform/util/src/com/intellij/util/ref/GCUtil.java | 9 +++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java index 445da646d0eb..827c47e32c50 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -1460,8 +1460,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig Document document = getDocument(getFile()); data.checkLineMarkers(DaemonCodeAnalyzerImpl.getLineMarkers(document, getProject()), document.getText()); } - //noinspection ResultOfMethodCallIgnored - hardRefToFileElement.hashCode(); // use it so gc won't collect it + ObjectUtils.reachabilityFence(hardRefToFileElement); return elapsed; } diff --git a/platform/util/src/com/intellij/util/ObjectUtils.java b/platform/util/src/com/intellij/util/ObjectUtils.java index f4f0a1f8a7d4..f1da9eef87bf 100644 --- a/platform/util/src/com/intellij/util/ObjectUtils.java +++ b/platform/util/src/com/intellij/util/ObjectUtils.java @@ -37,6 +37,13 @@ public class ObjectUtils { public static Object sentinel(@NotNull String name) { return new Sentinel(name); } + + /** + * They promise in http://mail.openjdk.java.net/pipermail/core-libs-dev/2018-February/051312.html that + * the object reference won't be removed by JIT and GC-ed until this call. + */ + public static void reachabilityFence(@SuppressWarnings("unused") Object o) {} + private static class Sentinel { private final String myName; diff --git a/platform/util/src/com/intellij/util/ref/GCUtil.java b/platform/util/src/com/intellij/util/ref/GCUtil.java index 8fabfebc3c02..3b2a4655fbb6 100644 --- a/platform/util/src/com/intellij/util/ref/GCUtil.java +++ b/platform/util/src/com/intellij/util/ref/GCUtil.java @@ -16,6 +16,7 @@ package com.intellij.util.ref; import com.intellij.diagnostic.ThreadDumper; +import com.intellij.util.ObjectUtils; import org.jetbrains.annotations.TestOnly; import java.beans.Introspector; @@ -40,7 +41,7 @@ public class GCUtil { //long started = System.nanoTime(); ReferenceQueue q = new ReferenceQueue<>(); SoftReference ref = new SoftReference<>(new Object(), q); - reachabilityFence(ref.get()); + ObjectUtils.reachabilityFence(ref.get()); System.gc(); @@ -52,7 +53,7 @@ public class GCUtil { // using ref is important as to loop to finish with several iterations: long runs of the method (~80 run of PsiModificationTrackerTest) // discovered 'ref' being collected and loop iterated 100 times taking a lot of time - reachabilityFence(ref.get()); + ObjectUtils.reachabilityFence(ref.get()); //System.out.println("Done gc'ing refs:" + ((System.nanoTime() - started) / 1000000)); } @@ -102,10 +103,6 @@ public class GCUtil { return until.getAsBoolean(); } - // They promise in http://mail.openjdk.java.net/pipermail/core-libs-dev/2018-February/051312.html that - // the object reference won't be removed by JIT and GC-ed until this call - private static void reachabilityFence(@SuppressWarnings("unused") Object o) {} - /** * Using java beans (e.g. Groovy does it) results in all referenced class infos being cached in ThreadGroupContext. A valid fix * would be to hold BeanInfo objects on soft references, but that should be done in JDK. So let's clear this cache manually for now,