<?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>Forem: Damon Morris</title>
    <description>The latest articles on Forem by Damon Morris (@damonjames).</description>
    <link>https://forem.com/damonjames</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%2F789009%2Fe3dd2d57-78c9-43a4-93c6-1a84f48113a0.png</url>
      <title>Forem: Damon Morris</title>
      <link>https://forem.com/damonjames</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/damonjames"/>
    <language>en</language>
    <item>
      <title>Diagnostics on Kubernetes: Obtaining a Memory Dump</title>
      <dc:creator>Damon Morris</dc:creator>
      <pubDate>Mon, 10 Jan 2022 19:14:38 +0000</pubDate>
      <link>https://forem.com/damonjames/diagnostics-on-kubernetes-obtaining-a-memory-dump-kgb</link>
      <guid>https://forem.com/damonjames/diagnostics-on-kubernetes-obtaining-a-memory-dump-kgb</guid>
      <description>&lt;p&gt;If like me you found that there is a slight memory leak in your application running on Kubernetes and you are searching non-stop on how to collect a memory dump, it can be hard to find exactly how to do this on a .NET application running on Linux-based containers.&lt;/p&gt;

&lt;p&gt;There are a handful of articles with useful instructions and tips, however none take into account whether your Docker images are &lt;a href="https://techcommunity.microsoft.com/t5/azure-developer-community-blog/hardening-an-asp-net-container-running-on-kubernetes/ba-p/2542224" rel="noopener noreferrer"&gt;hardened&lt;/a&gt;. So below, I've broken down the process into four steps which explain just how exactly to do this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Installing the &lt;strong&gt;dotnet-gcdump&lt;/strong&gt; tool&lt;/li&gt;
&lt;li&gt;Running the tool on your application&lt;/li&gt;
&lt;li&gt;Collecting the results&lt;/li&gt;
&lt;li&gt;Analysing the results&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;p&gt;Just some important things to note before we get into the good stuff:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node OS: Linux&lt;/li&gt;
&lt;li&gt;Local machine OS: Windows&lt;/li&gt;
&lt;li&gt;Alpine Linux-based containers&lt;/li&gt;
&lt;li&gt;.NET 6 SDK &amp;amp; Runtime&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The steps below are tailored for this particular setup. They may differ in some way depending on what you yourself are running; for example, the &lt;strong&gt;dotnet-gcdump&lt;/strong&gt; tool is only available in .NET Core 3.1 and above, so be sure to find the details of your setup before attempting this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing the dotnet-gcdump tool
&lt;/h2&gt;

&lt;p&gt;Like I mentioned earlier, if your containers are &lt;em&gt;hardened&lt;/em&gt; (i.e. your containers are not running as root) you will not be able to directly install this tool onto your running container.&lt;/p&gt;

&lt;p&gt;What you need to do instead is add the instructions to your Dockerfile to install the tool onto your container for it then to be available when your container is running:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine3.14 as build
...
RUN dotnet tool install --tool-path /tools dotnet-gcdump
...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above example; I am referencing the .NET 6 Alpine SDK base image, from which I am running the &lt;code&gt;dotnet tool install&lt;/code&gt; command and installing the dotnet-gcdump tool to the &lt;code&gt;/tools&lt;/code&gt; folder in my container.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the tool
&lt;/h2&gt;

&lt;p&gt;First things first is to check that your tool has successfully installed onto your container. To do this, boot up an interactive terminal session on one of your pods by running the &lt;code&gt;kubectl exec&lt;/code&gt; command with the necessary arguments. From here, change to the &lt;code&gt;tools&lt;/code&gt; directory and run the &lt;code&gt;ls&lt;/code&gt; command to list the files within this folder, and you should see the dotnet-gcdump tool within.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw2i3eiwyof96znwulamo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw2i3eiwyof96znwulamo.png" alt="Booting up a terminal session on a pod"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While you are in this folder, you will want to run the &lt;code&gt;dotnet-gcdump ps&lt;/code&gt; command to find the process ID of your running application (if you don't already know what it is). Make a note of this as it will be needed for the next step.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8sypjh0154ra42d0mgk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk8sypjh0154ra42d0mgk.png" alt="Running dotnet-gcdump ps command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now we have everything we need to run the &lt;code&gt;dotnet-gcdump collect&lt;/code&gt; command. You may find that your application does not have write permissions to the &lt;code&gt;/tools&lt;/code&gt; folder if you try to run this command without declaring an output file location. If this is the case, declare the output file location to the temporary directory (&lt;code&gt;/tmp&lt;/code&gt; in linux) which should allow your application to write to.&lt;/p&gt;

&lt;p&gt;You will also need to give your file a name. In the below example, I have called the file &lt;code&gt;memory-dump-test&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vs9m30w301layjzpf9w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vs9m30w301layjzpf9w.png" alt="Running dotnet-gcdump collect command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The process should take no longer than a few seconds, and when succeeded, you should see the message &lt;code&gt;Finished writing XXXX bytes.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To confirm, change directories to your &lt;code&gt;/tmp&lt;/code&gt; file and run the &lt;code&gt;ls&lt;/code&gt; command again to find that your &lt;code&gt;.gcdump&lt;/code&gt; file has successfully been generated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb01dnxrbs6soc5vldxxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fb01dnxrbs6soc5vldxxy.png" alt="Generated gcdump file"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Collecting the results
&lt;/h2&gt;

&lt;p&gt;Now you have generated your memory dump, the next step is porting this file over from your pod to your local machine so you can read the results. In my example below, I used the &lt;code&gt;kubectl cp&lt;/code&gt; command. Before executing this command, be sure that you have exited from the terminal session in the previous section!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bv75a4km7et2yz08ew4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4bv75a4km7et2yz08ew4.png" alt="Running the kubectl cp command"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This command can be fiddly due to needing to match the file path syntax with that of the corresponding operating system (Linux uses '/' while Windows uses '\') as you can see in the screenshot.&lt;/p&gt;

&lt;p&gt;There is no success message when this command has completed, but you may or may not see a &lt;code&gt;tar&lt;/code&gt; message referring to leading '/' from member names, however if you check the output file you declared, you should see that your &lt;code&gt;gcdump&lt;/code&gt; file has successfully ported over.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysing the results
&lt;/h2&gt;

&lt;p&gt;The resulting &lt;code&gt;gcdump&lt;/code&gt; file can be opened in Visual Studio.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nofnaoagcpteun1tst8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6nofnaoagcpteun1tst8.png" alt="gcdump file in Visual Studio"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This file gives you the count of a specific object, the size in bytes, and inclusive size (which is the size of all objects that are referenced by the type). Visual Studio also gives you the option to select two memory dumps and compare the difference between them.&lt;/p&gt;

&lt;p&gt;I won't go into too much detail about these files, as Microsoft themselves have many great, bitesize videos and documents on how to interpret these results, such as &lt;a href="https://www.youtube.com/watch?v=SHGeE_PFA4s&amp;amp;ab_channel=dotNET" rel="noopener noreferrer"&gt;this&lt;/a&gt; one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;And that's all there is too it! The solution is fairly trivial in retrospect, however trying to piece together the separate steps can be a bit of a nightmare especially when security measures are in place. Hopefully you can get to the bottom of your memory issues in peace!&lt;/p&gt;

</description>
      <category>kubernetes</category>
      <category>linux</category>
      <category>dotnet</category>
    </item>
  </channel>
</rss>
