Dot Net For All

Code optimizations to improve the memory footprint

Hello friends, with the advancements of the technology memory has become quite cheap these days. That is the reason that most of the developers don’t think about the memory consumption these days.

But once the code goes to the production, it starts creating memory issues which takes the good night sleep of the developer. By following the below three code improvements or optimizations you can improve the memory footprint of you application. These can be used for both Java and C# programming languages.

Good coding practices to improve the memory footprint

Specifying the capacity while using ArrayList or List

We take the array list and List creation lightly in the programming. The way these data structures work is quite complicated. Whenever we initialize an ArrayList, it blocks some amount of memory for default number of elements.

Let’s consider that the ArrayList blocks the memory for 10 elements. If we are using this ArrayList and suppose it has fully consumed the memory. Now the user wants to add one more element, the array list will double the capacity. And in our example it will increase to 20 items of memory.

Array list does the same every time and extra element is added to it beyond its default capacity. This could be quite memory consuming, in cases suppose we have to just add couple of elements after the default capacity.

We can resolve memory issue by using the array or specifying the capacity while using Array List. Below is the code example for the same.

   IList<int> lstNumbers = new List<int>(100);

Lazy initialization

Lets have a look at the small C# code snippet. What do you think is wrong in the below code.

 public class Standard
    {
        private IList<string> students = new List<string>();

        public void AddStudent(string name)
        {
            students.Add(name);
        }
    }

At first look everything looks fine. But When the Standard class is initialized, the students list is also initialized with the default capacity of List. And it would stay in memory even if we don’t call the AddStudent method. Thus increasing the memory footprint of the application.

How this code can be improved. We can improve the code by using lazy initialization as shown in the below code.

 public class Standard
    {
        private IList<string> students = null;

        public void AddStudent(string name)
        {
            if (students == null)
                students = new List<string>();

            students.Add(name);
        }
    }

With a small code change, the List would not be initialized unless and un till the AddStudent method is not called.

Do not use Clear method for List or ArrayList

Many times we use the Clear method of the List or ArrayList to remove the elements. The problem with this method is that, it will clear the elements but it would still keep the memory intact of the Array List.

The better way to circumvent this problem is to use assign null to the ArrayList as shown in the below code:

 public void ClearStudents()
 {
    students = null;
 }

This will help to clear the memory used by the Array List.

Conclusion:

No matter how cheap the memory becomes, but it would always be a good practice to write a better code which used less memory footprint in the application. Though these points are quite trivial but they can help you to have an application which consumes less memory and having better performance.

Do you want to add some other points which can help to improve the memory footprint. Please let me know in the comments.

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview