KotlinBinaryClassCache: clean-up request caches for all threads
^KT-44550 Fixed
This commit is contained in:
+12
-6
@@ -23,6 +23,7 @@ import com.intellij.openapi.components.ServiceManager
|
|||||||
import com.intellij.openapi.util.Computable
|
import com.intellij.openapi.util.Computable
|
||||||
import com.intellij.openapi.vfs.VirtualFile
|
import com.intellij.openapi.vfs.VirtualFile
|
||||||
import com.intellij.psi.PsiJavaModule
|
import com.intellij.psi.PsiJavaModule
|
||||||
|
import java.util.concurrent.CopyOnWriteArrayList
|
||||||
|
|
||||||
class KotlinBinaryClassCache : Disposable {
|
class KotlinBinaryClassCache : Disposable {
|
||||||
private class RequestCache {
|
private class RequestCache {
|
||||||
@@ -43,17 +44,22 @@ class KotlinBinaryClassCache : Disposable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private val cache = object : ThreadLocal<RequestCache>() {
|
private val cache = object : ThreadLocal<RequestCache>() {
|
||||||
|
private val requestCaches = CopyOnWriteArrayList<RequestCache>()
|
||||||
|
|
||||||
override fun initialValue(): RequestCache {
|
override fun initialValue(): RequestCache {
|
||||||
return RequestCache()
|
return RequestCache().also { requestCaches.add(it) }
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun remove() {
|
||||||
|
for (cache in requestCaches) {
|
||||||
|
cache.result = null
|
||||||
|
cache.virtualFile = null
|
||||||
|
}
|
||||||
|
super.remove()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun dispose() {
|
override fun dispose() {
|
||||||
cache.get().apply {
|
|
||||||
result = null
|
|
||||||
virtualFile = null
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is only relevant for tests. We create a new instance of Application for each test, and so a new instance of this service is
|
// This is only relevant for tests. We create a new instance of Application for each test, and so a new instance of this service is
|
||||||
// also created for each test. However all tests share the same event dispatch thread, which would collect all instances of this
|
// also created for each test. However all tests share the same event dispatch thread, which would collect all instances of this
|
||||||
// thread-local if they're not removed properly. Each instance would transitively retain VFS resulting in OutOfMemoryError
|
// thread-local if they're not removed properly. Each instance would transitively retain VFS resulting in OutOfMemoryError
|
||||||
|
|||||||
Reference in New Issue
Block a user