MEMORY LEAKAGE
Memory leak occurs when we create a memory in heap and forget to delete it .Memory leaks are particularly serious issues for programs like deamons and servers which by definition never terminate.
Memory leak should always be checked and the memory is always a constraint on a device. If there are many memory leakage in the device memory , the device will run out of memory faster, though there is unused memory there.
How to avoid memory leakage: To avoid memory leaks,memory allocated on heap should always be freed when no longer needed.
Methods to check memory leakage:
1. GDB: Typical usage of GDB runs as follows: After starting GDB, we set breakpoints,which are place where the execution of code wish to pause.Each time GDB encounters a breakpoint, it suspends its execution of the program at that point, giving us a chance to check the values of various variables. INVOKING GDB: Before starting make sure you complied the programs with -g option, without -g GDB would be useless since no information of the variable and function names , line numbers and so on. Syntax: gcc -g program.c TO start GDB: syntax: gdb a.out (in place of a.out any executable file can be there). ` RUN GDB: syntax: r or run
BREAKPOINT SET: b main (or function name) / b 10 (10 here is line number) DISPLAY AND PRINT COMMAND: disp variable will display the value of variable at every time the program pause. Print : p variable will print the current value of the variable NEXT (n ) or STEP (s) command: s will pause the program execution at the start of the function syntax: s n will move to the next line of execution. Syntax: n QUIT COMMAND: q will quit the program in execution. Syntax: q 2. Valgrind: Valgrind is primarily a memory checker tool(Memcheck). Cachegrind :CPU cache profiler. Callgrind:provides more callgraph info DRD:pthread bug detector. HELGRIND:Data race detector for multithreaded applications. Massif:Heap profiler MEMCHECK:memory bugs detector, includes out-of- bounds(OOB),accesses(READ/WRITE UNDER/OVERFLOW),unitialized data accesses,UAF,UAR,memory leakage, double free and overlapping memory region bugs. This is the default tool 3. ASAN: Purpose: assign ASAN with complier GCC. Usage: -fsanitize=address (address flag must be used). 4:KASAN(KERNEL SPACE SANITIZE): Purpose: debugging kernel space. Usage: -fsanitize=kernel_address 5:MSAN(MEMORY SANITIZIER): Purpose: UMR detector Usage: -fsanitize=memoryFPIE
6:TSAN(THREAD SANITIZIER) Purpose: data race detector Usage: -fsanitize=thread 7:LSAN(LEAK SANITIZER): Purpose: Memory Leakage detector Usage: fsanitize= leak 8:UBSAN(UNDEFINED BEHAVIOR SANITIZER): Purpose:Undefined behavior sanitize Usage: -fsanitize=unidentified
















