diff --git a/platform/util/src/com/intellij/openapi/util/LowMemoryWatcher.java b/platform/util/src/com/intellij/openapi/util/LowMemoryWatcher.java index df142a91f5f5..99367bb4f374 100644 --- a/platform/util/src/com/intellij/openapi/util/LowMemoryWatcher.java +++ b/platform/util/src/com/intellij/openapi/util/LowMemoryWatcher.java @@ -1,18 +1,4 @@ -/* - * Copyright 2000-2016 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ +// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.openapi.util; import com.intellij.openapi.Disposable; @@ -21,6 +7,8 @@ import com.intellij.util.containers.WeakList; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import java.util.concurrent.atomic.AtomicBoolean; + /** * @author Eugene Zhuravlev */ @@ -35,8 +23,10 @@ public class LowMemoryWatcher { private static final WeakList ourListeners = new WeakList<>(); private final Runnable myRunnable; private final LowMemoryWatcherType myType; + public static final AtomicBoolean ourNotificationsSuppressed = new AtomicBoolean(); public static void onLowMemorySignalReceived(boolean afterGc) { + if (ourNotificationsSuppressed.get()) return; LOG.info("Low memory signal received: afterGc=" + afterGc); for (LowMemoryWatcher watcher : ourListeners.toStrongList()) { try { diff --git a/platform/util/src/com/intellij/util/ref/GCWatcher.java b/platform/util/src/com/intellij/util/ref/GCWatcher.java index dd9f5d2788ed..eddad83a0429 100644 --- a/platform/util/src/com/intellij/util/ref/GCWatcher.java +++ b/platform/util/src/com/intellij/util/ref/GCWatcher.java @@ -1,6 +1,7 @@ // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package com.intellij.util.ref; +import com.intellij.openapi.util.LowMemoryWatcher; import com.intellij.openapi.util.Ref; import com.intellij.reference.SoftReference; import com.intellij.util.MemoryDumpHelper; @@ -68,7 +69,15 @@ public class GCWatcher { } public boolean tryCollect() { - return GCUtil.allocateTonsOfMemory(new StringBuilder(), this::isEverythingCollected); + LowMemoryWatcher.ourNotificationsSuppressed.set(true); + try { + long startTime = System.currentTimeMillis(); + GCUtil.allocateTonsOfMemory(new StringBuilder(), () -> isEverythingCollected() && System.currentTimeMillis() - startTime < 5000); + return isEverythingCollected(); + } + finally { + LowMemoryWatcher.ourNotificationsSuppressed.set(false); + } } /**