Monitor memory consumption (Java Thread Dump)

Here we are, happily working on our project when, while trying to run the jstack command on a PID=1 process, we are confronted with the following error:   «Unable to get pid of LinuxThreads manager thread».

This happens because the process PID=1 is a reserved process of the operating system. So how can we fix it?

To execute the process, we are going to make a number of changes in the project to first start a «sh» process that later starts the Java process, leaving it all as follows:

Steps to take

1.- Create file

It must be at the same src height, Docker, JenkinsFile, with the Spring Boot boot line and profile:

java,securityOff -jar /app.jar 

2.- Add the file to the container

We will do this after the packaging process (mvn).

We will edit the pom.xml file to add a plugin, in the build stage, that copies the .sh files to the target folder, so that this file can later be included in the Docker image, along with the .jar of the application.


3.- Modify DockerFile file

We will add the following line after adding the Spring Boot application; that is to say: after this line, ADD *-exec.jar app.jar


We will grant execution permissions and owner permissions to the created file:

RUN chown onesait:onesait && \
chmod +x

We will comment out the «ENTRYPOINT» line and add the following line:

CMD ["/bin/sh","-c", "/"] 

A complete DockerFile example would look like this:

ADD *-exec.jar app.jar

# logs folder
RUN mkdir -p /var/logs/smartindustry/scheduler/transport-etl && \
	mkdir ./target
# create onesait user/group
RUN addgroup -S onesait -g 433 && adduser -u 431 -S -g onesait -h /usr/local -s /sbin/nologin onesait 

RUN chown -R onesait:onesait /usr/local && \
    chown -R onesait:onesait /var/logs/smartindustry/scheduler/transport-etl && \
    chown -R onesait:onesait ./target && \
    chown onesait:onesait app.jar && \
    chmod -R 777 ./target && \
    chmod -R 777 /var/logs && \
    chmod -R 777 /usr/local && \
    chown onesait:onesait && \
    chmod +x

VOLUME ["/tmp", "/var/logs/smartindustry/scheduler/transport-etl"]
USER onesait


#ENTRYPOINT ["java","",",securityOff","-jar","/app.jar"]
#ENTRYPOINT ["/bin/sh", "-c", "set -e && java,securityOff -jar /app.jar"]
CMD ["/bin/sh","-c", "/"]
#CMD ["/"]

After performing these steps, the PID will be changed:

And that’s it. Now it’s possible to execute the command on the desired PID:

$ jstack -l 10
2020-06-28 12:36:56
Full thread dump OpenJDK 64-Bit Server VM (25.212-b04 mixed mode):
"Attach Listener" #40 daemon prio=9 os_prio=0 tid=0x00005576e5349000 nid=0x4c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   Locked ownable synchronizers:
        - None
"DestroyJavaVM" #39 prio=5 os_prio=0 tid=0x00005576e81ae000 nid=0xc waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   Locked ownable synchronizers:
        - None

Header image: Reka Illyes at Unsplash

✍🏻 Author(s)

Leave a Reply

Your email address will not be published. Required fields are marked *