# How to Collect Memory Dumps for .NET Applications on Kubernetes

Some time ago, we published an [article](https://blog.raulnq.com/diagnostic-net-apps-in-kubernetes-with-dotnet-monitor) about `dotnet-monitor` and how to collect diagnostic data from applications running in a Kubernetes cluster. But what if you didn't follow that guideline and now need to collect diagnostic data, like a memory dump, to troubleshoot an issue in our application? Luckily, tools like [`dotnet-dump`](https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dump) and some manual steps can help us achieve that.

> The `dotnet-dump` global tool is a way to collect and analyze dumps on Windows, Linux, and macOS without any native debugger involved.

First, download the [tool](https://aka.ms/dotnet-dump/linux-x64) to your local machine. Then, copy the downloaded file to the pod using the following command (assuming we are in the folder containing the file):

```powershell
kubectl cp ./dotnet-dump "MY_POD_NAME:/tmp/dotnet-dump" --namespace MY_NAMESPASE
```

Access the pod by running the command:

```powershell
kubectl exec -it "pod/MY_POD_NAME" --namespace MY_NAMESPASE --sh
```

Once inside the pod, navigate to the `tmp` folder:

```powershell
cd /tmp
```

Change the file permissions with this command:

```powershell
chmod 777 ./dotnet-dump
```

Proceed to collect the memory dump by running this command:

```powershell
./dotnet-dump collect -p 1
```

Wait until the memory dump is complete, then exit the pod using the command `exit`. The collected memory dump will be saved in the format `core_yyyyMMdd_hhmmss`. To copy the file from the pod to your local machine, use the following command:

```powershell
kubectl cp "MY_POD_NAME:/tmp/core_20250109_110027" ./core_20250109_110027.DMP --namespace MY_NAMESPASE
```

That's it! We are now ready to analyze the memory dump on our local machine. Thank you, and happy coding!
