🩸
Memory Management

Memory Leaks & Debugging

Jab Memory Wapas Nahi Aati
💡 Memory leak ek naal (tap) hai jo band karna bhool gaye — thodi thodi paani (memory) bahta rehta hai, aur kaafi der baad poora tank (RAM) khaali ho jaata hai.

Memory leak tab hota hai jab malloc()/calloc() se li gayi memory ko free() nahi kiya jaata, aur us memory ka pointer bhi kho jaata hai (reference khatam ho jaata hai) — ab na to use kar sakte ho, na free kar sakte ho. Long-running programs (jaise servers) mein ye dheere-dheere saari RAM khatam kar sakta hai.

Common causes: function ke andar malloc() karke pointer return na karna, loop mein baar-baar malloc() karna bina purana free() kiye, ya error path mein early return karna bina cleanup kiye.

Debugging tools jaise Valgrind (Linux/Mac) memory leaks ko automatically detect karte hain — wo batate hain exactly kaunsi line par memory allocate hui thi jo kabhi free nahi hui. Production C code mein Valgrind se test karna standard practice hai.

void leaky() {
  int *p = malloc(100 * sizeof(int));
  // ... kaam karo ...
  // free(p);  // BHOOL GAYE — memory leak!
}  // p yahi gayab ho gaya, memory kabhi free nahi hogi

// Valgrind se check karo:
// $ gcc -g program.c -o program
// $ valgrind --leak-check=full ./program
🩸
Memory leak ek naal (tap) hai jo band karna bhool gaye — thodi thodi paani (memory) bahta rehta hai, aur kaafi der baad poora tank (RAM) khaali ho jaata hai.
1 / 2
⚡ Quick Recap
  • Leak = allocated memory jiska pointer reference kho gaya
  • Common cause: error paths mein cleanup bhoolna
  • Valgrind jaise tools se leaks detect karo
Is page mein (2 subtopics)

Bahut saare real-world memory leaks tab hote hain jab function beech mein hi "early return" karta hai (error handling ke liye) bina pehle allocate ki gayi memory free kiye. Har error path ko cleanup ka khayal rakhna padta hai.

int process(int n) {
  int *data = malloc(n * sizeof(int));
  if (data == NULL) return -1;

  if (n < 0) {
    return -1;  // BUG: data ko free() kiye bina return kar diya — LEAK
  }

  // ... kaam karo ...
  free(data);
  return 0;
}

// FIX: har return se pehle free() ya "goto cleanup" pattern use karo
⚠️Common Mistake: Bade functions mein multiple return paths hone par memory leaks aasani se ho jaate hain — C mein isse handle karne ka classic pattern "goto cleanup;" label hai (jo aksar beginners ko ajeeb lagta hai lekin real-world C codebases mein common hai).

Valgrind ke "--leak-check=full" output mein har leak ke liye exact line number milta hai jaha memory allocate hui thi. "definitely lost" ka matlab hai confirm leak hai; "still reachable" ka matlab hai memory freed nahi hui lekin pointer abhi bhi accessible hai (program end tak).

// $ valgrind --leak-check=full ./program
//
// ==1234== HEAP SUMMARY:
// ==1234==     in use at exit: 400 bytes in 1 blocks
// ==1234==   total heap usage: 2 allocs, 1 frees, 1,024 bytes allocated
// ==1234==
// ==1234== 400 bytes in 1 blocks are definitely lost in loss record 1 of 1
// ==1234==    at 0x...: malloc
// ==1234==    by 0x...: leaky (program.c:12)
💡Tip: "total heap usage: 2 allocs, 1 frees" line sabse pehle dekho — agar allocs aur frees ka count match nahi karta, kahi na kahi leak hai, chahe "definitely lost" section chhota hi kyun na lage.