Memory leaks!! The Silent Killer of System Performance

How the Memory Leak can impact the software performance and system performance, And it can be a shows stopper issue for your whole system.

Narendra Harny
Make Android

--

int main() { /* Let's create objects....!! */ while(true) new Object }

When I picked this topic to write on, I was more inclined towards writing about Memory Leaks” in the context of the Android Operating System as I work on the same. then, I decided to write only on Memory Leaks without referencing any Operating System or a Programming Language because it is a common issue for almost every software system and programming language in general.

I thought to write about some information which is required to understand the Memory Leak as an issue and how it can impact the system performance and the evolution of programming languages concerning the optimization from a system performance perspective.

This article does not contain a lot of code examples for memory leaks, but I tried to provide the information on the topic or related to it as much as possible.

So let’s understand what a memory leak is first!!

Technically when a program runs, the memory allocation happens for the objects that have been initialized into it and “due to some incorrectly managed memory allocation and deallocation” the objects will be present in memory but can not be accessed or used. That is how the allocated memory becomes unused and “This situation in the memory of allocated but inaccessible memory blocks is called a Memory Leak”.

Oops! Looks bad right?

I was looking for a practical example of a Memory Leak in a real-time scenario and after some time, I went on browsing I did not like anything to add here, So I created an example of a bad system that is practically possible.

The “Hotel Cleaning Service”!!

There are 64 rooms in the Hotel with an efficient working software model of check-in & check-out system. There is a cleaning service team and they clean all 64 rooms in a sequence from 1 to 64 daily.

The room cleaning service team follow a rule that is leaner from 1 to 64 rooms which is a fix for every day. One room takes 15 minutes to clean and cleaning happens from 6 am to 10 pm.

When the checkout happens cleaning service does not go and clean the room randomly or demand basis. Suppose room number 56 guest checked out at 11 am. The leaner or sequential algorithm of the cleaning service allows them to clean room number 56 around 8 pm.

So we can see the potential problem here that after 11 am the room was available and could be given to any new guest who visited but it was not clean so not available.

Same in the Memory Leak the the allocated memory to the object is unused but it can not be accessed. The solution is to clean the memory so that is Hotel room as soon as it is unused.

With the above example, I wanted to convey that!!

It is very important to understand that the above problem will not be problematic for the hotel with 100000 rooms or it will be detected very late or never because some rooms will be mostly available so it is rare to find the fault in the wrong choice of cleaning service algorithm.

Even In terms of operating system hardware and resources, the memory leak will show the impact late or on a circumstantial basis. When the system is equipped with highly capable resources (processors and memory) in size then memory leak can be hidden behind the power cost but concerning the low-end embedded devices with smaller size resources.

It is a system Show-stopper!! Let’s see how.

There is a term in Software Engineering called “Software Ageing’ and Memory Leak is one of the responsible causes of Software Ageing.

The term Software Ageing was identified by Sir David Lorge Parnas they said that **Programs, like people, get old. We can’t prevent ageing, but we can understand its causes, take steps to limit its effects, temporarily reverse some of the damage it has caused, and prepare for the day when the software is no longer viable.** Ref link.

Why the title Memory Leak is a “Silent Killer” for Software Systems Performance?

When a program falls due to an Exception, Error or Crash that used to be quite evident on UI, It usually becomes a priority fix issue in software development. Concerning the user interaction perspective the crash with a null pointer exception is bigger than a memory leak for the users because crashing is the maximum sound an issue can make on UI.

But the issue which does not sound too much like a “Memory Leak” will be detected or noticed when the overall system performance is impacted that’s why I said a Memory Leak is a silent killer for system performance.

A memory leak is not a too serious issue in modern operating systems and it will not be detected so early. These operating system only runs programs for a short period and all the memory will be released once the program is terminated then memory leak is not a serious issue for those systems.

Where memory leak is serious!!

  1. When a program runs long time or always.
  2. Embedded system or portable device where fewer memory resources are available.
  3. Computers Game and Animations based software.
  4. When memory management components like the memory manager itself have the issue.
  5. When the issue is present with the system-level background process, daemons and service.
  6. When the system is not equipped with an automatic memory management algorithm to clear the unused memory from time to time.

So the conclusion is if a long-running software program creates a Memory Leak issue is always considered to be fixed on high priority. doesn‘t matter which system it is.

Programming Languages and Memory Leak

The possibility of a memory leak happening is higher with programming languages like C and C++. why? Because these languages do not have an automatic Memory Manager like Garbage Collection!

So let’s list out programming languages which has automatic garbage collection and those that do not.

Languages have garbage collection!! Java, C#, Python, Ruby, JavaScript, PHP, Swift, Kotlin, Go, Rust.

Languages do not have garbage collection!! C, C++, Ada, Assembly languages.

If memory leaks as an issue which can be responsible for degrading the system performance and C and C++-like languages are more prone to memory leaks, then why do these languages like Java and Python not replace C and C++ so far?

I believe It is not easy for any language to fit into the space where C and C++ work. What is your understanding on this please write a comment!

It is not only one factor that makes a programming language the best fit for system-level development. These languages have manual memory management which is a fact but also they provide various advantages like Performance, Legacy Codebase, Direct Hardware Interaction, Portability, etc. Languages like Java and Python offer automatic memory management but still, they can not be the best fit for system-level programming due to the extraordinary Run Time Overhead and less direct control over low-level resources.

It does not mean nobody wants to replace C and C++ without a reason, Ha Ha Ha! The trouble with substitutes for C & C++ is they arrived so late and are still immature. After 40+ years of C, only one mature language joined the same space that is Rust, and others have strayed to the upside like Golang, Nim has GC.

We can clearly understand that in the programming languages which does not have an auto memory manager like a garbage collector, the reason for memory leaks is for those memory blocks that remain unclear even after GC cleared the unused memory blocks and for the languages that do not have GC the memory leak happens inefficient memory deallocation routine.

Swap Memory or Virtual Memory

The modern system setup with two memories(RAM and SSD/HD) uses the Memory Swapping technique when more memory is requested than RAM has then some part of the disk can be used as swap space to allocate some memory swap memory is also known as virtual memory.

There is a problem with the above swiping memory technique. When a program requests a huge amount of memory, the system swaps some active memory allocations to the swap memory and when the memory is available, the program will get the memory back into RAM.

This process of swapping space needs some time when this swapping happens, so in case the system is exhausted with memory the system will not be able to provide memory to the new program which requested memory and this could be the reason for slowing down the system and again it is an impact on on the system performance but I would say it is the general hang of the system which we certainly face much time with the modern tools.

Swap Memory is considered a supporting mechanism when the system is exhausted with the main memory(RAM).

A few algorithms are also designed to support the above situation like allocating a fixed amount of memory to each process which is running in the system, which certainly requires system memory configuration changes to run some specific high memory-consuming programs.

We have discussed, The System Perspectives, Programming language perspectives and the solutions applied for stabilizing the system performance for memory race conditions, but software performance always matters on the point of what software is ruining in the system and how they utilize the system resources.

Conclusion!!

The motive of this article is to have an understanding of memory leaks as a Software Engineering issue and not just a specific to a particular operating system or a programming language.

For which type of program and system is the memory leak a bigger concern is the performance of the software and the system should be analyzed throughout the development instead of waiting until the system itself says help me, please I am dying.

A memory leak is a shows stopper issue for a system with less size resources, 24/7 running systems, Embedded system. Indirectly if your program runs more or always in the system that can kill the system’s performance if it has a memory leak.

A memory leak used to be hidden in modern systems with sizable resources but in an exceptional case is also risky sometimes, so following the programming rules to prevent the memory leak is important.

When it comes to the comparison between programming languages which have GC the automatic Memory manager, comes with a Runtime overhead and that is why the GC-based languages do not fit for system-level programming.

For Manual Memory, clearing requires the memory de-allocation routines to be implemented by the programmers. that is an overhead for programmers and also creates a possibility of “Human Error” to miss something.

Additionally, I have found a few topics that are related to memory leaks and important to understand. I would like to mention those topics which may need equal attention when you understand memory leaks.

  1. Memory allocation and deallocation strategies.
  2. Dynamic Memory allocation and Unreachable Memory when the program runs.
  3. Garbage Collection and how it works.
  4. Reference counting, cyclic references, and mark & sweep algo techniques with GC.
  5. The swapping between Main memory and Secondary storage.
  6. How manually C and C++ clear the memory.
  7. Some memory analyzer tools are used to debug the memory leak like a memory profiler.

Thanks for Reading!

Please comment with questions and suggestions!! If this blog is helpful for you then hit Please hit the clap! Follow on Medium, Connect on LinkedIn, Follow on Insta and send emails if any discussion is needed on the same topic on copy email.
Please Follow & subscribe to Make Android Publication by Narendra K H for prompt updates on Android platform-related blogs.

Thank you!

--

--

Narendra Harny
Make Android

Connect on https://medium.com/make-android | Write On, Android AOSP & Applications | Python | DevOps | Java | C++ | Kotlin | Shell | Linux | Android Auto | IVI