Skip to content

Commit d0c0ca2

Browse files
author
Philippe Soares
authored
#BAEL-4654 (eugenp#10450)
* #BAEL-4654 * Fixing PR comments.
1 parent e347393 commit d0c0ca2

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

docker/heap-sizing/Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM openjdk:8u92-jdk-alpine
2+
COPY /src /src/
3+
RUN mkdir /app && ls /src && javac /src/main/java/com/baeldung/docker/heapsizing/PrintXmxXms.java -d /app
4+
CMD java -version && java $JAVA_OPTS -cp /app com.baeldung.docker.heapsizing.PrintXmxXms

docker/heap-sizing/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.4.2</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.baeldung.docker</groupId>
12+
<artifactId>heap-sizing</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>heap-sizing</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>11</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-webflux</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-test</artifactId>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>io.projectreactor</groupId>
31+
<artifactId>reactor-test</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
<configuration>
42+
<image>
43+
<name>heapsizing-demo</name>
44+
</image>
45+
</configuration>
46+
</plugin>
47+
<plugin>
48+
<groupId>com.google.cloud.tools</groupId>
49+
<artifactId>jib-maven-plugin</artifactId>
50+
<version>2.7.1</version>
51+
52+
<configuration>
53+
<to>
54+
<image>heapsizing-demo-jib</image>
55+
</to>
56+
</configuration>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
61+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.docker.heapsizing;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
import java.util.logging.Logger;
7+
8+
import static com.baeldung.docker.heapsizing.PrintXmxXms.logMemory;
9+
10+
@SpringBootApplication
11+
public class HeapSizingApplication {
12+
private static final Logger logger = Logger.getLogger(HeapSizingApplication.class.getName());
13+
14+
public static void main(String[] args) {
15+
SpringApplication.run(HeapSizingApplication.class, args);
16+
logMemory(logger);
17+
}
18+
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.docker.heapsizing;
2+
3+
import java.lang.management.ManagementFactory;
4+
import java.lang.management.MemoryMXBean;
5+
import java.lang.management.MemoryPoolMXBean;
6+
import java.util.logging.Level;
7+
import java.util.logging.Logger;
8+
9+
public class PrintXmxXms {
10+
11+
private static final Logger logger = Logger.getLogger(PrintXmxXms.class.getName());
12+
13+
public static void main(String[] args) {
14+
logMemory(logger);
15+
}
16+
17+
/**
18+
* We're reusing this method in HeapSizingApplication, therefore this method was extracted
19+
* to avoid repetition.
20+
*/
21+
static void logMemory(Logger logger) {
22+
float mb = 1024f * 1024f;
23+
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
24+
25+
// xmx controls the maximum size of the memory allocation pool,
26+
// which includes the heap, the garbage collector's survivor space, and other pools.
27+
float xmx = memoryBean.getHeapMemoryUsage().getMax() / mb;
28+
float xms = memoryBean.getHeapMemoryUsage().getInit() / mb;
29+
logger.log(Level.INFO, "Initial Memory (xms) : {0}mb", xms);
30+
logger.log(Level.INFO, "Max Memory (xmx) : {0}mb", xmx);
31+
32+
for (MemoryPoolMXBean mp : ManagementFactory.getMemoryPoolMXBeans()) {
33+
logger.log(Level.INFO, "Pool: {0} (type {1}) = {2}", new Object[]{ mp.getName(), mp.getType(), mp.getUsage().getMax() / mb });
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)