Programming, Resources, Scripting
Leave a comment

Understanding memory leaks – the simple way

Here is how Wikipedia defines a memory leak

In computer science, a memory leak is a particular type of unintentional memory consumption by a computer program where the program fails to release memory when no longer needed. This condition is normally the result of a bug in a program that prevents it from freeing up memory that it no longer needs. Memory is allocated to a program, and that program subsequently loses the ability to access it due to program logic flaws.

A memory leak generally happens if you have a used a part of memory to store some data and do not release it when the work is complete. This kind of programming logic when used within a loop or an infinite loop can be quite disastrous.

A better way to understand this is to try something practically. I am going to explain this by showing you a problem I created myself. I made this mistake when I was initially testing the VMHeartBeat.vbs. Although such a script design was unintentional, it resulted in a very dirty memory leak. Here is the code snippet from that script before I fixed the problem.

[Code lang=”vb”]

 While (1)
 objVMItems = objVMService.GetSummaryInformation(NULL,intValArray,objSummaryInfo)
 for each objItem in objSummaryInfo
  If (objItem.EnabledState=2) Then
   If (objItem.HeartBeat = 6 OR objItem.HeartBeat = 13) Then
    VMHardReset(objItem.ElementName)
   End If
  End If
 Next
Wend

[/Code]

From the above code, you can easily understand that it is an infinite loop. I am using WMI interfaces to call GetSummaryInformation method under Msvm_VirtualSystemManagementService class. I have used objVMItems (line: 2) variable to store an object instance returned by GetSummaryInformation and there are a few other variables used in the same code block. This code block will run forever unless you kill the script engine process. Now, the problem is this. This loops keeps running and start re-allocating memory every time there is a variable assignment but does not release the previously allocated space. There are three such variable assigments in this code block. This, now, results in a memory leak.

Here is the code block that fixes the issue.

[Code lang=”vb”]

While (1)
 objVMItems = objVMService.GetSummaryInformation(NULL,intValArray,objSummaryInfo)
 for each objItem in objSummaryInfo
  If (objItem.EnabledState=2) Then
   If (objItem.HeartBeat = 6 OR objItem.HeartBeat = 13) Then
    VMHardReset(objItem.ElementName)
   End If
  End If
 Next
 Set objVMItems=Nothing
 Set objItem = Nothing
 Set objSummaryInfo = Nothing
Wend

[/Code]

In the above corrected code block, line numbers 10,11 and 12 take care of the memory leak issue. All I needed to do was release the memory allocated towards the end of While loop. Simple..huh? If you want to really see how it actually affects a system, on a Hyper-V system, just run VMHeartBeat.vbs without the clean up as shown in first code block. After starting the script, open Task Manager and look at the memory usuage of your script engine — either cscript.exe or wscript.exe, depending on how you ran the script. You can clearly see that the memory usage of that process getting doubled almost every 5 seconds.

If you leave it like that for a while, it will simply eat up all the free memory and then eventually crash the system. That is a memory leark for you 🙂

Filed under: Programming, Resources, Scripting

by

Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.