Does the force kill command (kill -9) in linux cleanup the dynamically allocated memory with new operator in C++ application?

Viewed 322

I have a C++ application to be run on Oracle Linux OS.

Consider, I have created few objects with new operator. Though I have used delete operator to deallocate it, but the force kill command would not reach this implementation.

But, if I force kill (kill -9) the process, will the dynamically allocated memory (using new operator) be de-allocated by the operating system? As I am not able to find the straightforward answer to this, I would like to have some information.

Thanks in advance.

2 Answers

But, if I force kill (kill -9) the process, will the dynamically allocated memory (using new operator) be de-allocated by the operating system?

Memory is tied to a process through the virtual memory system and the memory management unit (MMU). Thus yes, all memory (not just the one allocated through new) will be freed.

Exceptions to this are global inter-process communication (IPC) resources like shared memory, cached files, etc.

When a process dies by whatever means, all process resources including memory and file objects are cleaned up by the kernel. When you kill a process it stops running immediately so no cleanup code including destuctors is are run. So yes all memory is deallocated but is happens at a much lower level than heaps and stacks.

Related