Memory Allocation
How can you notice a memory leak in real-life?
Taught to me by CY Chen, when you’ll notice a gradual increase in memory utilization.
Memory Leaks
You can still have memory leak in a program that has automatic memory management. Imagine a program that never terminates, always keeps requesting for more memory. Learned from Hemal Shah.
In C++
Use the new
keyword.
Do NOT Forget
Do NOT forget to free the memory at the end of the program by using the
delete
keyword, else you will have memory leak.
new
vs.malloc
?In C++, you can still use
malloc
. The difference is thatnew
allocates memory AND initializes.
Null pointers are represented by nullptr
.
NEW Learning Jul-13-2023: Consider the following
- I assumed that calling the
delete
keyword automatically sets it to anullptr
. - Rather, the pointer becomes a dangling pointer, meaning it still holds the memory address of the deallocated memory, but the memory itself is no longer valid for your program to use.
= delete
keyword
=
delete
keywordThe =
delete
keyword is used to disable a certain function. Often used to disallow copying. Source
Disallow coying:
In C
We use malloc
and free
from stdlib.h
library to allocate and deallocate memory from the Heap.
Failure to free the memory that you have allocated is called a memory leak.
More Functions (allocators)
From Hemal Shah
And we had a discussion about malloc, how stack and heap actually work. When a process starts, it gets stack allocated things.
We also talked about memory management
- Modern OS will keep track of when memory is requested by a process, so that when the process dies, it will clean up that memory.
- Older phones didn’t have that back then
And then I said: “Then where is the need to check for memory leak?” And he clarified how memory leaks is a whole different thing than automatic garbage collection.
- Because you can have a program that doesn’t crash and continuously have memory leak. It will keep on leaking and leaking, requesting more and more memory, until the program sufffocates
- So it’s important to still detect memory leaks, even though we have automatic garbage collection, because the program might not crash, and it will just continually leak memory.
- Sidenote on
valgrind
: What valgrind does is that it creates a fake environment. Runs the program super slowly, and everything the program requests for memory, valgrind will remember it
How does malloc
and free
work under the hood?
They are not constant time operations. Kajanan told me that they are actually really complicated.
https://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work
From ChatGPT:
- malloc: Allocates a specified amount of memory by checking a list of free blocks. If a suitable block is found, it’s removed from the list and returned. If not, more memory is requested from the OS. It maintains metadata for memory management.
- free: Deallocates memory allocated by
malloc
by adding the block back to the free list. It may merge adjacent free blocks (coalescing) to reduce Fragmentation. The function does not always return memory to the OS immediately.