0

I am using java 17 and Spring Boot for GraphQL and experiencing a Java Heap Space OutOfMemoryError when handling large response payloads.

As you know spring provides an annotation-based programming model for GraphQL and my handlers look like this:

@QueryMapping
public Mono<Book> bookById(@Argument Long id) {
    // ...
}

The instances of my handler's return type (Book) are not being cleared from the heap even after a GC run.

The pom file looks like this:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.2</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-r2dbc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

    <dependency>
        <groupId>io.r2dbc</groupId>
        <artifactId>r2dbc-mssql</artifactId>
        <version>${io.r2dbc.mssql.version}</version>
    </dependency>

    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.mapstruct</groupId>
        <artifactId>mapstruct</artifactId>
        <version>${org.mapstruct.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-graphql</artifactId>
    </dependency>

    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>graphql-java-extended-scalars</artifactId>
        <version>${graphql.extended.scalars.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-text</artifactId>
        <version>${org.apache.commons.text.version}</version>
    </dependency>

    <dependency>
        <groupId>com.graphql-java</groupId>
        <artifactId>java-dataloader</artifactId>
        <version>${com.graphql.java.dataloader.version}</version>
    </dependency>

    <dependency>
        <groupId>com.googlecode.owasp-java-html-sanitizer</groupId>
        <artifactId>owasp-java-html-sanitizer</artifactId>
        <version>${com.googlecode.html.sanitizer.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>${commons.io.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.12</artifactId>
        <version>${org.apache.spark.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.12</artifactId>
        <version>${org.apache.spark.version}</version>
        <scope>provided</scope>
    </dependency>

</dependencies>

What could be the reason for this issue and how can I resolve it?

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 1
    Have a look at the heap dump and see what still has a reference to the Book instances. – tgdavies Feb 12 '23 at 05:22
  • 1
    Nobody can find the cause of the (probable) memory leak in your code based on the information you have provided. So I have found you a duplink to help you to find the leak for yourself. – Stephen C Feb 12 '23 at 09:47

0 Answers0