If all memory allocated in a daemon will be freed after exit()?

A simple question that if I do malloc but without free before exit(), the system should free all memory of the daemon automatically, is it correct?
Answer
When a process (including a daemon) exits, the OS reclaims all its allocated memory, including
- Heap memory allocated with malloc
- Stack memory
- File descriptors
- Memory mappings
- Other resources (with exceptions like shared memory)
Answer
Yes, the OS will automatically reclaim all memory when the daemon process exits.
However,
This applies only on process termination.If your daemon is long-running and keeps allocating memory without freeing it, this can lead to memory leaks and exhaustion over time.
If your process is short-lived (utility that runs and exits), skipping free() is generally acceptable.
Tools like Valgrind will report such allocations as "still reachable" or "not freed", even though the OS reclaims them, because they were never explicitly freed.
For some resources (temporary files, shared memory, or mutexes), relying on process exit alone may not be sufficient.
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions