devxlogo

Memory Leaks and Their Prevention

Memory Leaks and Their Prevention

A memory leak occurs when an application fails for some reason to release dynamic memory that was allocated using new or malloc().The application won’t reclaim the allocated memory because both C and C++ assume that if you don’t explicitly free dynamically-allocated memory, you want it to remain available throughout the lifetime of the application. Some operating systems reclaim the leaked memory but they do that only after the application has terminated. On other operating systems (DOS, for example) leaked memory is lost until the system is rebooted. In either case, recurrent memory leaks cause an application to bog down or crash altogether. The easiest way to avoid memory leaks is by not using dynamic allocations in the first place. If possible, the application should use automatic and static storage instead of dynamic memory. When dynamic memory allocation is unavoidable, remember to add a matching delete call for every object allocated by new.

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist