Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i decrease Spring Boot application memory usage in Docker?

I have a question, why very simple Spring Boot application allocate 100 MB RAM ? How can i decrease memory usage?

1) https://start.spring.io/ generate demo progect with "Spring Web Starter" dependency

2) Dockerfile

FROM openjdk:8-jre-alpine
ADD /target/demo.jar demo.jar
EXPOSE 8080
ENTRYPOINT ["sh", "-c", "java $JAVA_OPTS -jar /demo.jar"]

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
    <finalName>demo</finalName>
</build>

Classes

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

package com.example.demo;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/app")
public class AppRestController {
    @GetMapping(value = "/test")
    public ResponseEntity<String> health() {
        return new ResponseEntity<>("test OK!", HttpStatus.OK);
    }
}

1) Memory test 1

docker build -f Dockerfile -t demo .

docker run -p 8080:8080 demo

docker stats

MEM USAGE / LIMIT
101.5MiB / 989.4MiB

2) Memory test 2 with JVM keys

docker stop dc9305c42d09

docker run -p 8080:8080 -e JAVA_OPTS="-Xmx50M -Xms50M" demo

docker stats

MEM USAGE / LIMIT 107.2MiB / 989.4MiB

Memory usage just the same. How can i decrease it?

like image 416
Вячеслав Avatar asked Mar 03 '23 15:03

Вячеслав


1 Answers

You need to pass something like -m 50m to limit memory available for the container along with -Xmx and Xms for JVM.

Following article explains it well.

JVM Memory Allocation in Docker Container

like image 136
Abhinav Agarwal Avatar answered Apr 28 '23 23:04

Abhinav Agarwal