systemd for java process issue with heap dump

Viewed 981

My application is written in java spring boot, to run the application on linux system i have written a systemd file as below.

[Unit]
Description=Service Module For microservice Service.
After=network.target auditd.service
[Install]
WantedBy=multi-user.target
[Service]
Type=idle
Environment=service_name=/home/ec2-user/test-1.0-SNAPSHOT.jar
Environment=env=dev
ExecStart=/usr/bin/java -jar ${service_name} \
     --spring.profiles.active=${env} \
         --Xmx=1300M \
         -XX:+HeapDumpOnOutOfMemoryError \
         -Xloggc:gc.log \
         -XX:+PrintGCDetails \
         -XX:+PrintGCTimeStamps \
         -XX:+UseGCLogFileRotation \
         -XX:NumberOfGCLogFiles=5 \
         -XX:GCLogFileSize=10M

ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=30000
Restart=always
RestartSec=500
StartLimitBurst=3
StartLimitInterval=120
MemoryLimit=1300M
[Manager]

When this systemd file is started, i can see in process mentioned process is running as expected.

/usr/bin/java -jar /home/ec2-user/test-1.0-SNAPSHOT.jar --spring.profiles.active=dev --Xmx=1300M -XX:+HeapDumpOnOutOfMemoryError -Xloggc:gc.log -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=10M

Issue is when the application fails with "java.lang.OutOfMemoryError: Java heap space" errors in the logs and does not export heap dump and gc logs in the location specified. Has someone used jar with systemd and faced similar issues.

2 Answers

Maybe if you set WorkingDirectory=/home/ec2-user/ to the directory where you want your logs/dumps to appear. Because without that I'm not sure where systemd is trying to create the gc.log you specified.

Or alternativly you could try to specifiy the whole path to the gc.log: -Xloggc:/home/ec2-user/gc.log

Well i have found out that the problem was with the new line and \ characters, If we place all the jvm arguments in the single line it works!!!.

ex: ExecStart=/usr/bin/java -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/location/heapdump.bin -Xloggc:/location/gc.log -jar test.jar --Xms=512M --Xmx=1300M --spring.profiles.active=${env}

Note: Some of the arguments are deprecated in Java 11, so check document arguments to include as per use case. https://docs.oracle.com/en/java/javase/11/tools/java.html#GUID-3B1CE181-CD30-4178-9602-230B800D4FAE

Related