Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heap memory and OS at the exit of a process written in C++

I have a doubt about the role of the operating system in regards to a process lifetime right now. I am using Linux.

Suppose that I have an application that creates a set of objects in the heap using new. During the lifetime of the application I have no need to delete any of those objects except at the exit the application or on an exception before exiting to do the clean-up.

Suppose I don't call delete at the end of the application for all those objects, do usually the OS reclaim/free all the heap allocated to make it available again at the process exits? If the process exit because of an exception or call to return or exit, does this always occurs?

If this is true this means that if I don't call delete there won't be any impact on the OS or on the other applications running on a machine. Right?

I usually use boost shared pointers or use delete but I would like just to clarify this doubt in a OS/Linux context

Kind Regards AFG

like image 240
Abruzzo Forte e Gentile Avatar asked Dec 28 '22 03:12

Abruzzo Forte e Gentile


1 Answers

That is correct. Any leak of memory after the lifetime of a process on a protected mode operating system is as a really nasty bug in the kernel (sometimes processes crash).

Having said that, the simplest way to check for memory leaks is to ensure that the heap has exactly the same number of allocated cells at the end of execution as it has at the beginning of execution. If you do not delete on exit, you have no way of checking this and will never discover legitimate memory leaks.

like image 66
doron Avatar answered Jan 28 '23 06:01

doron