oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
19.97k stars 1.6k forks source link

[GR-54108] Polyglot memory leak on simple python execution #8927

Open allanfrankwork opened 1 month ago

allanfrankwork commented 1 month ago

Describe GraalVM and your environment :

Have you verified this issue still happens when using the latest snapshot? Yes, but did have to run with -Dpolyglotimpl.DisableVersionChecks=true

Describe the issue When running a simple python Hello world case (from https://www.graalvm.org/latest/reference-manual/python/) in a loop, the CACHE in com.oracle.graal.python.builtins.objects.function.BuiltinMethodDescriptor keeps growing.

I would expect this leads to a OOM eventually although have not reproduced this with the simple case. I have seen a production application experiencing OOM with 80% of memory consumed by this CACHE variable.

Code snippet or code repository that reproduces the issue

public class Main {
    public static void main(String[] args) {
        while(true) {
            try (var context = Context.create()) {
                System.out.println(context.eval("python", "'Hello Python!'").asString());
            }
        }
    }
}
pom.xml with:
    <dependencies>
        <dependency>
            <groupId>org.graalvm.polyglot</groupId>
            <artifactId>polyglot</artifactId>
            <version>24.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.graalvm.polyglot</groupId>
            <artifactId>python</artifactId>
            <version>24.0.1</version>
            <type>pom</type>
        </dependency>
    </dependencies>

Steps to reproduce the issue Please include both build steps as well as run steps

  1. Put code into Main.java
  2. Put dependencies into pom.xml
  3. Run code. Run in debug and monitor com.oracle.graal.python.builtins.objects.function.BuiltinMethodDescriptor.CACHE. This keeps growing with 16 entries per python execution.

Expected behavior After Context.close(), would expect Garbage collector to do its work. Or at least that the cache should not keep growing when executing the same code again.

Additional context com.oracle.graal.python.builtins.objects.function.BuiltinMethodDescriptor.CACHE seems to contain builtin functions that look fine, but dump contents I see these entries keep growing:

UnaryBuiltinDescriptor{tuple.repr}

Can it be that some proxy class object is unique per execution leading to new entries getting put into the CACHE?

I have tried several different versions of contructing Engine, Source, Context, but they all experience ever-growing CACHE.

Thanks!

fernando-valdez commented 1 month ago

Thanks for reporting this issue. I created an internal ticket to track this issue: GR-54108