#BAEL-4654 (#10450)

* #BAEL-4654

* Fixing PR comments.
This commit is contained in:
Philippe Soares 2021-02-22 14:37:46 -05:00 committed by GitHub
parent bd4ff652f8
commit f6470356d2
4 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,4 @@
FROM openjdk:8u92-jdk-alpine
COPY /src /src/
RUN mkdir /app && ls /src && javac /src/main/java/com/baeldung/docker/heapsizing/PrintXmxXms.java -d /app
CMD java -version && java $JAVA_OPTS -cp /app com.baeldung.docker.heapsizing.PrintXmxXms

View File

@ -0,0 +1,61 @@
<?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 https://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.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.baeldung.docker</groupId>
<artifactId>heap-sizing</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>heap-sizing</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>heapsizing-demo</name>
</image>
</configuration>
</plugin>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>2.7.1</version>
<configuration>
<to>
<image>heapsizing-demo-jib</image>
</to>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,19 @@
package com.baeldung.docker.heapsizing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.logging.Logger;
import static com.baeldung.docker.heapsizing.PrintXmxXms.logMemory;
@SpringBootApplication
public class HeapSizingApplication {
private static final Logger logger = Logger.getLogger(HeapSizingApplication.class.getName());
public static void main(String[] args) {
SpringApplication.run(HeapSizingApplication.class, args);
logMemory(logger);
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.docker.heapsizing;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryPoolMXBean;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PrintXmxXms {
private static final Logger logger = Logger.getLogger(PrintXmxXms.class.getName());
public static void main(String[] args) {
logMemory(logger);
}
/**
* We're reusing this method in HeapSizingApplication, therefore this method was extracted
* to avoid repetition.
*/
static void logMemory(Logger logger) {
float mb = 1024f * 1024f;
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
// xmx controls the maximum size of the memory allocation pool,
// which includes the heap, the garbage collector's survivor space, and other pools.
float xmx = memoryBean.getHeapMemoryUsage().getMax() / mb;
float xms = memoryBean.getHeapMemoryUsage().getInit() / mb;
logger.log(Level.INFO, "Initial Memory (xms) : {0}mb", xms);
logger.log(Level.INFO, "Max Memory (xmx) : {0}mb", xmx);
for (MemoryPoolMXBean mp : ManagementFactory.getMemoryPoolMXBeans()) {
logger.log(Level.INFO, "Pool: {0} (type {1}) = {2}", new Object[]{ mp.getName(), mp.getType(), mp.getUsage().getMax() / mb });
}
}
}