Skip to content

Commit 1b0bcb7

Browse files
committed
SONARKT-628 Fix memory leak: RegexCache should be per file and not per project
Otherwise it retains ASTs of files with regular expressions till the end of project analysis.
1 parent 9152cb7 commit 1b0bcb7

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/frontend/KotlinTree.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ import org.sonarsource.kotlin.api.reporting.KotlinTextRanges.textPointerAtOffset
2929
class KotlinTree(
3030
val psiFile: KtFile,
3131
val document: Document,
32-
val regexCache: RegexCache,
33-
)
32+
) {
33+
val regexCache = RegexCache()
34+
}
3435

3536
data class KotlinSyntaxStructure(val ktFile: KtFile, val document: Document, val inputFile: InputFile) {
3637
companion object {

sonar-kotlin-api/src/main/java/org/sonarsource/kotlin/api/sensors/AbstractKotlinSensorExecuteContext.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,13 @@ abstract class AbstractKotlinSensorExecuteContext(
100100

101101
fun analyzeFiles(): Boolean {
102102
try {
103-
val regexCache = RegexCache()
104103
progressReport.start(filenames)
105104
kotlinFiles.filter {
106105
!EMPTY_FILE_CONTENT_PATTERN.matches(it.inputFile.contents())
107106
}.forEach { (ktFile, doc, inputFile) ->
108107
if (sensorContext.isCancelled) return false
109108
val inputFileContext = InputFileContextImpl(sensorContext, inputFile, isInAndroidContext)
110-
val tree = KotlinTree(ktFile, doc, regexCache)
109+
val tree = KotlinTree(ktFile, doc)
111110

112111
measureDuration(inputFile.filename()) {
113112
analyzeFile(inputFileContext, tree)

sonar-kotlin-test-api/src/main/java/org/sonarsource/kotlin/testapi/TestUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ fun kotlinTreeOf(content: String, environment: Environment, inputFile: InputFile
3939
listOf(virtualFile),
4040
)
4141
val (ktFile, document) = KotlinSyntaxStructure.of(content, environment, inputFile)
42-
return KotlinTree(ktFile, document, RegexCache())
42+
return KotlinTree(ktFile, document)
4343
}

0 commit comments

Comments
 (0)