mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-10-06 06:21:16 +07:00
IDEA-161070 Memory leaks of LightVirtualFiles
This commit is contained in:
@@ -61,7 +61,7 @@ public abstract class BaseExternalAnnotationsManager extends ExternalAnnotations
|
||||
|
||||
protected final PsiManager myPsiManager;
|
||||
|
||||
private final ConcurrentMap<VirtualFile, List<PsiFile>> myExternalAnnotations = ContainerUtil.createConcurrentSoftValueMap();
|
||||
private final ConcurrentMap<VirtualFile, List<PsiFile>> myExternalAnnotations = ContainerUtil.createConcurrentWeakKeySoftValueMap();
|
||||
private final Map<AnnotationData, AnnotationData> myAnnotationDataCache = new WeakKeyWeakValueHashMap<AnnotationData, AnnotationData>();
|
||||
private final ConcurrentMap<PsiFile, Pair<MostlySingularMultiMap<String, AnnotationData>, Long>> myAnnotationFileToDataAndModStamp = ContainerUtil.createConcurrentSoftMap();
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
package com.intellij.psi;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationUtil;
|
||||
import com.intellij.ide.highlighter.JavaFileType;
|
||||
import com.intellij.openapi.editor.Document;
|
||||
import com.intellij.openapi.fileEditor.FileDocumentManager;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.openapi.util.Key;
|
||||
import com.intellij.testFramework.LeakHunter;
|
||||
import com.intellij.testFramework.LightProjectDescriptor;
|
||||
import com.intellij.testFramework.LightVirtualFile;
|
||||
import com.intellij.testFramework.PsiTestUtil;
|
||||
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor;
|
||||
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
|
||||
import com.intellij.util.GCUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
*/
|
||||
public class LightVirtualFileLeaksTest extends LightCodeInsightFixtureTestCase {
|
||||
private Key<Boolean> myKey;
|
||||
private static final DefaultLightProjectDescriptor PROJECT_DESCRIPTOR = new DefaultLightProjectDescriptor() {
|
||||
@Override
|
||||
public Sdk getSdk() {
|
||||
return PsiTestUtil.addJdkAnnotations(super.getSdk());
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
myKey = Key.create(getName());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
protected LightProjectDescriptor getProjectDescriptor() {
|
||||
return PROJECT_DESCRIPTOR;
|
||||
}
|
||||
|
||||
public void testDoNotLeakViaDocument() {
|
||||
createFileWithDocument();
|
||||
checkLeak();
|
||||
}
|
||||
|
||||
private void createFileWithDocument() {
|
||||
Document document = FileDocumentManager.getInstance().getDocument(createLightFile());
|
||||
assertNotNull(document);
|
||||
PsiJavaFile psiFile = (PsiJavaFile)PsiDocumentManager.getInstance(getProject()).getPsiFile(document);
|
||||
assertSame(document, PsiDocumentManager.getInstance(getProject()).getDocument(psiFile));
|
||||
}
|
||||
|
||||
private void checkLeak() {
|
||||
GCUtil.tryGcSoftlyReachableObjects();
|
||||
LeakHunter.checkLeak(getProject(), LightVirtualFile.class, vf -> vf.getUserData(myKey) == Boolean.TRUE);
|
||||
}
|
||||
|
||||
public void testDoNotLeakViaExternalAnnotations() {
|
||||
queryExternalAnnotations();
|
||||
checkLeak();
|
||||
}
|
||||
|
||||
private void queryExternalAnnotations() {
|
||||
LightVirtualFile vFile = createLightFile();
|
||||
assertEmpty(AnnotationUtil.getAllAnnotations(((PsiJavaFile)getPsiManager().findFile(vFile)).getClasses()[0], true, null));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private LightVirtualFile createLightFile() {
|
||||
LightVirtualFile vFile = new LightVirtualFile(getName() + ".java", JavaFileType.INSTANCE, "class Foo {}");
|
||||
vFile.putUserData(myKey, Boolean.TRUE);
|
||||
return vFile;
|
||||
}
|
||||
}
|
||||
@@ -220,8 +220,12 @@ public class FileManagerImpl implements FileManager {
|
||||
assert !file.isDirectory();
|
||||
FileViewProvider viewProvider = findCachedViewProvider(file);
|
||||
if (viewProvider != null) return viewProvider;
|
||||
viewProvider = ConcurrencyUtil.cacheOrGet(myVFileToViewProviderMap, file, createFileViewProvider(file, true));
|
||||
return viewProvider;
|
||||
|
||||
viewProvider = createFileViewProvider(file, true);
|
||||
if (file instanceof LightVirtualFile) {
|
||||
return file.putUserDataIfAbsent(myPsiHardRefKey, viewProvider);
|
||||
}
|
||||
return ConcurrencyUtil.cacheOrGet(myVFileToViewProviderMap, file, viewProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+9
-2
@@ -31,7 +31,8 @@ import com.intellij.psi.impl.PsiManagerImpl;
|
||||
import com.intellij.psi.impl.ResolveScopeManager;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.search.SearchScope;
|
||||
import com.intellij.util.containers.ConcurrentFactoryMap;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.util.containers.FactoryMap;
|
||||
import com.intellij.util.indexing.AdditionalIndexableFileSet;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -43,7 +44,13 @@ public class ResolveScopeManagerImpl extends ResolveScopeManager {
|
||||
private final ProjectRootManager myProjectRootManager;
|
||||
private final PsiManager myManager;
|
||||
|
||||
private final Map<VirtualFile, GlobalSearchScope> myDefaultResolveScopesCache = new ConcurrentFactoryMap<VirtualFile, GlobalSearchScope>() {
|
||||
private final Map<VirtualFile, GlobalSearchScope> myDefaultResolveScopesCache = new FactoryMap<VirtualFile, GlobalSearchScope>() {
|
||||
|
||||
@Override
|
||||
protected Map<VirtualFile, GlobalSearchScope> createMap() {
|
||||
return ContainerUtil.createConcurrentWeakKeySoftValueMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected GlobalSearchScope create(@NotNull VirtualFile key) {
|
||||
GlobalSearchScope scope = null;
|
||||
|
||||
Reference in New Issue
Block a user