<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amit Ranjan</title>
    <description>The latest articles on DEV Community by Amit Ranjan (@amit_ranjan_4b5cdcf88dc9b).</description>
    <link>https://dev.to/amit_ranjan_4b5cdcf88dc9b</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3443031%2F285d5ab2-7065-438d-8952-8fc9b38279de.jpg</url>
      <title>DEV Community: Amit Ranjan</title>
      <link>https://dev.to/amit_ranjan_4b5cdcf88dc9b</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amit_ranjan_4b5cdcf88dc9b"/>
    <language>en</language>
    <item>
      <title>Analyzing JVM Memory Usage Inside a Docker Container</title>
      <dc:creator>Amit Ranjan</dc:creator>
      <pubDate>Mon, 18 Aug 2025 16:26:16 +0000</pubDate>
      <link>https://dev.to/amit_ranjan_4b5cdcf88dc9b/analyzing-jvm-memory-usage-inside-a-docker-container-1jna</link>
      <guid>https://dev.to/amit_ranjan_4b5cdcf88dc9b/analyzing-jvm-memory-usage-inside-a-docker-container-1jna</guid>
      <description>&lt;p&gt;This article describes the steps required to analyze memory usage of a Java application running inside a Docker container. Although some AWS-specific references are included, the procedure is cloud-agnostic and applicable to any environment where Docker containers are deployed.&lt;/p&gt;

&lt;p&gt;JVM Memory Categories&lt;/p&gt;

&lt;p&gt;The Java Virtual Machine (JVM) allocates memory across several distinct areas. The following categories are commonly analyzed when troubleshooting or profiling memory usage:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Heap Memory – Used for object allocation and garbage collection.&lt;/li&gt;
&lt;li&gt;Metaspace (non-heap) – Stores class metadata.&lt;/li&gt;
&lt;li&gt;Code Cache – Holds compiled bytecode generated by the Just-In-Time (JIT) compiler.&lt;/li&gt;
&lt;li&gt;Thread Stacks – Memory allocated per thread for execution stacks.&lt;/li&gt;
&lt;li&gt;Off-heap Allocations – Memory allocated directly outside the heap.&lt;/li&gt;
&lt;li&gt;Native Memory (JVM &amp;amp; Libraries) – Memory consumed by the JVM itself and linked native libraries.&lt;/li&gt;
&lt;li&gt;JVM Overhead &amp;amp; Internal Allocations – Additional memory reserved for internal operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Connect to the Host&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since the application is running in a Docker container, access to the host system is required. For example, on AWS EC2:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ssh @&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Check Container Memory Usage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once logged into the host, run the following command to monitor memory consumption at the container level:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;docker stats&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This command outputs memory usage, CPU utilization, and other runtime statistics. The container ID of the application will also be displayed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Access the Container&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To interact directly with the application inside the container, open an interactive shell:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;docker exec -it  /bin/bash&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This session enables execution of commands as if they were run directly within the container environment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Identify JVM Process&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inside the container, use the jcmd utility to list Java processes and their corresponding process IDs (PIDs):&lt;/p&gt;

&lt;p&gt;&lt;em&gt;jcmd&lt;/em&gt;&lt;br&gt;
Typically, the application will be associated with a single PID.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Analyze JVM Memory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.1 Heap Memory Information&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;jcmd  GC.heap_info&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This command provides details regarding:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Maximum heap size
2. Committed memory
3. Currently used memory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;5.2 Native Memory Summary&lt;/strong&gt;&lt;br&gt;
&lt;em&gt;jcmd  VM.native_memory summary&lt;/em&gt;&lt;br&gt;
This command generates a detailed breakdown of native memory usage, which is particularly valuable for analyzing allocations outside the heap.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 6: Enable Native Memory Tracking (if required)&lt;/strong&gt;&lt;br&gt;
If native memory tracking is not enabled, the above command may return an error. To enable it, configure the JVM with the following option:&lt;br&gt;
&lt;em&gt;{"name": "JAVA_TOOL_OPTIONS", "value": "-XX:NativeMemoryTracking=summary"}&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This setting enables summary-level tracking of native memory allocations.&lt;/p&gt;

&lt;p&gt;By following these steps, developers can accurately profile heap, non-heap, and native memory usage of Java applications running inside Docker containers, thereby gaining insights into performance and potential memory-related issues.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
