What is Memory Leak, Valgrind and PID?

Neeraj Nawale
6 min readFeb 3, 2022


This article contains information about how and why memory leak occurs, how to perform profiling using Valgrind and how to look into a process using Process ID (PID).

What is Process ID (PID)?

To understand process ID, let’s have a look upon some terminologies required to capture this concept in better and easy way.
Terminologies are ::

  1. Program → A program is set of instructions or code created by developer to perform necessary/desire actions.
  2. Process → A program becomes process when that program is loaded or given some space on RAM i.e. runtime environment or whenever developer executes the program.
  3. Kernel → It is a component of Operating System and whenever developer runs a code, that code is provided to kernel and then, kernel decides what to do. Hence, kernel behind the scene, converts the program or code and loads it on top of RAM.

Whenever a system boots up, lot of programs gets executed by kernel. Therefore, to manage all these processes, kernel internally assigns a Process ID to each process. So, after these understandings, now let’s understand the core meaning of Process ID.

Process ID is a unique number assigned by kernel to each process but, internally it is just a folder or directory which contains all information/data of a process. In other words, each process has a unique folder/directory assigned to it. Developer can actually look inside this folder.
In Linux, to see or look into these folders, entire RAM is mounted on “/proc” folder and this folder doesn’t exist in hard disk, it is created when OS is booted.

Let’s see the “/proc” folder. I will be using Redhat Linux,
No. of processes in running state i.e. on RAM ::

“/proc” folder ::

Let’s understand PID with an example,
Execution of “sleep” command and get inside its PID i.e. folder.
Steps ::

1. Execute or run sleep command and put it in background.

2. Locate PID for sleep command.

3. Go inside “/proc” folder, look for PID=3026 and you will be inside “sleep” command or process.

Note ::
i} “/proc” folder doesn’t exist on hard disk, it is created after system gets booted.
ii} Process ID is a directory in form of number.
iii} Each process has a unique ID

What is Memory Leak?

Now that you are familiar with PID, it would be more easier to understand memory leak.
You know that, whenever a developer creates and executes a code, it is loaded onto RAM, becomes a process and PID is assigned to it. Generally, PID is divided into lots of parts but, below three parts play an essential role ::
1} Code Section
2} Stack Memory
3} Heap Memory

i} Code Section → Contains machine code or binary code and it is necessary because, this code will be executed by CPU.

ii} Stack Memory → It holds all static variables or static functions e.g. x=5, it will be stored in stack memory, to get no. of bytes assigned for a variable. In other words, fixed size memory allocation is stored in stack memory.

iii} Heap Memory → It is used for dynamic memory allocation i.e. runtime. In other words, no. of bytes allocated at runtime by developer is stored in heap memory. Heap memory plays a vital role because now-a-days almost every application keep on asking information at runtime and it all depend on developer.

When does Memory leak occur?

Developer asks to kernel for some size/memory from heap memory and kernel allocates memory as per need but, when program gets completed or PID is removed from RAM, the memory allocated by heap memory still exists. Interesting thing to notice is, process stops, dynamically allocated memory still exists and it totally depend on memory management program that holds all allocations of heap. In short, that memory is still reserved for a program which is stopped and it doesn’t make any sense.

Hence, in future, if another program comes up, that allocated memory will never be used for this new program. Another scenario can be, program with dynamic memory allocation (i.e. heap memory) is kept on executing multiple times, slowly entire RAM will get allocated without any reason and after 100% RAM usage, system goes down. This concept is called memory leakage or memory leak.
As a developer, its your responsibility to free the memory after performing required operations to avoid memory leak. Given below is an example in C language ::

Dynamic Memory allocation using “malloc” and free/remove that memory using “free”

What is Valgrind?

There are more different kinds of example for memory leakage and sometime developer doesn’t check if memory leakage occurred or not.
So, as an admin, you should know a tool, who will help you to check whether a program have memory leakage or not, without looking at the code. Lots of tools are available in market to check and analyze memory leak, one of the tool is “Valgrind”.

Valgrind is powerful software/tool for heap profiler and it comes with lots of option like, “leak-check”, etc. also, it have sub-tools like “memcheck”, “massif”, etc. for profiling.
Given below is an example for understanding how Valgrind is used ::

  • Allocate 10 bytes on heap memory using “malloc”
  • Execute Valgrind with “memcheck” tool
  • Free the allocated memory
  • Massif is another tool for profiling and also checks stack size


Now you know the exact meaning of PID, memory leakage and how to resolve it, also how Valgrind plays a vital role in profiling.

Keep Learning..
Thank You…