Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in compiler due to duplicated entries in typeCache map #31132

Closed
azinneera opened this issue Jun 11, 2021 · 1 comment · Fixed by #31495
Closed

Memory leak in compiler due to duplicated entries in typeCache map #31132

azinneera opened this issue Jun 11, 2021 · 1 comment · Fixed by #31495
Assignees
Labels
Area/SemanticAPI Semantic API Related Issues #Compiler Priority/Blocker Team/CompilerFETools Semantic API, Formatter, Shell Type/Bug

Comments

@azinneera
Copy link
Contributor

azinneera commented Jun 11, 2021

Description:

$subject was found when we analyzed the heap usage of the jballerina-tools distribution built from 2.0.0-alpha5-dev branch (commit: 8241385) for the following scenario:

Adding and deleting newlines to a source file in googleapis.calendar module [1]. Some of the dependencies were not there in the local cache so the package showed diagnostics for the unresolved modules (since the LS is run offline).

When we kept adding/deleting newlines we found out that the retained bytes increased significantly for org.wso2.ballerinalang.compiler.util.Name [2]. When analyzing further with @pubudu91 we figured out that the memory leak is due to duplicated entries in the typeCache map resulting in unnecessarily held BSymbol objects.

Steps to reproduce:

Duplication of entries can be reproduced with ballerina-lang as follows:

  1. Write the below test case in io.ballerina.projects.test.TestBuildProject [3]:
@Test
    public void testMemoryLeak() {
        BuildProject project = BuildProject.load(Paths.get("<path-to-module-ballerinax-googleapis.calendar>/calendar"));
        project.currentPackage().getCompilation();
        for (int i = 0; i < 10; i++) {
            DocumentId documentId = project.currentPackage().getDefaultModule().documentIds().stream().findFirst().get();
            String sourceCode = project.currentPackage().getDefaultModule().document(documentId).syntaxTree().toSourceCode();
            project.currentPackage().getDefaultModule().document(documentId).modify().withContent(sourceCode).apply();
            project.currentPackage().getCompilation();
        }
    }
  1. Debug and evalute TypesFactory.getInstance(project.projectEnvironmentContext().getService(CompilerContext.class)).typeCache for each iteration and you can observe it growing

Affected Versions:
At least Alpha 5.4

OS, DB, other environment details and versions:
Tested on Ubuntu 20.04

[1] https://github.com/ballerina-platform/module-ballerinax-googleapis.calendar
[2] https://github.com/ballerina-platform/ballerina-lang/blob/master/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/util/Names.java
[3] https://github.com/ballerina-platform/ballerina-lang/blob/master/project-api/project-api-test/src/test/java/io/ballerina/projects/test/TestBuildProject.java

@anupama-pathirage anupama-pathirage added the Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. label Jun 12, 2021
@pubudu91 pubudu91 added the Area/SemanticAPI Semantic API Related Issues #Compiler label Jun 14, 2021
@pcnfernando pcnfernando added Priority/High Team/CompilerFETools Semantic API, Formatter, Shell and removed Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. labels Jun 16, 2021
@pubudu91 pubudu91 added this to the Ballerina Swan Lake - Beta3 milestone Jun 21, 2021
@pubudu91
Copy link
Contributor

When working on fixing this issue by implementing equals() and hashCode() for BType and BSymbol and their sub classes, faced an issue with cyclic types. Basically attempting to implement hashCode() for them resulted in stack over flow errors. Even if we got around the stack over flow issue, there's the concern about correctness of using mutable fields of a class in the hash code computation. Therefore, we decided not to go down this path. Instead, we'll remove the type cache for now and see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/SemanticAPI Semantic API Related Issues #Compiler Priority/Blocker Team/CompilerFETools Semantic API, Formatter, Shell Type/Bug
Projects
None yet
4 participants