How to bring down the memory usage using interface?

Vishal Pathak
Abhima C# Programming
4 min readJul 16, 2023

Have you been in any situation, where you don’t want to use some functions of the class but then also that function is occupying the space in the memory through object instantiation. If yes, then you are at the place 🙌.

Maharishi Sushruta — World`s First Surgeon

Introduction:-

There are many ways we can lower down the space utilization but by using interface is the most simple and recommended way. So let’s first understand what is interface.

Interface:-

An interface defines a contract. Any class or struct that implements contract must provide an implementation of the members defined in the interface.

In this blog we will only discuss interface in C# but the solution is applicable in all the OOP languages.

Class Instantiation:-

Now let’s understand how memory allocation to the class properties are being done. When we instantiate class we create object and that object holds the memory address of the class properties.

Example:

Company class:-

public class Company
{
public IDictionary<string, string> GetOpenPositions()
{
IDictionary<string, string> openPostions = new Dictionary<string, string>()
{
{"Job-123","Software Developer- Java" },
{"Job-124","Oracle Adminstrator" },
{"Job-125","Asp.Net Full Stack Developer" },
{"Job-126","MEAN Stack Developer" },
};
return openPostions;
}

public double GetSalary(int empId)
{
IDictionary<int, double> staffSalary = new Dictionary<int, double>()
{
{1,20000},
{2,80000},
{3,200000},
{4,200000},
{5,250000},
};

if (staffSalary.TryGetValue(empId, out double salary)) {
return salary;
}
return 0;

}

public IDictionary<string, string> InternalPositions()
{
IDictionary<string, string> openPostions = new Dictionary<string, string>()
{
{"Job-123","Software Developer- Java" },
{"Job-124","Oracle Adminstrator" },
{"Job-125","Asp.Net Full Stack Developer" },
{"Job-126","MEAN Stack Developer" },
{"Job-127","MERN Stack Developer" },
};
return openPostions;
}
}

In the above code we have created a class and provided three concrete methods i.e InternalPositions, GetSalary, GetOpenPositions.

MainService class:-

    public  class MainService
{
public IDictionary<string, string> GetBothTheDetailsUsingClassInstantiation() {
Company company = new Company();
var openPostionsForCandidate = company.GetOpenPositions();
var opendPostions = company.InternalPositions();
var salary = company.GetSalary(2);
return opendPostions;
}
}

Here we have added new class called MainService which is having method GetBothTheDetailsUsingClassInstantiation. This method is instantiating the class Company and calling all it’s all the three methods.

This looks fine in the current scenario but let’s say I just want to call GetOpenPositions method, other methods are required when employee is using the application. Now the issue with this approach is that it is loading other methods as well in the memory and some storage is getting allocated to those methods which is unnecessary.

So to resolve the above issue we will use multiple inheritance through interfaces.

ICandidate interface:-

public interface ICandidate
{
public IDictionary<string, string> GetOpenPositions();
}

Here in the above code we have added ICandidate interface with GetOpenPostions method declared. Which can be called when Candidate is using the application.

IEmployee interface:-

public interface IEmployee
{
public double GetSalary(int empId);
public IDictionary<string,string> InternalPositions();

}

In the above code we have added IEmployee interface with GetSalary and InternalPositions method declared. It can be called when Employee is using the application

Company class:-

Now let’s modify Company class and implement both the interfaces as below.

public class Company : IEmployee, ICandidate
{
public IDictionary<string, string> GetOpenPositions()
{
IDictionary<string, string> openPostions = new Dictionary<string, string>()
{
{"Job-123","Software Developer- Java" },
{"Job-124","Oracle Adminstrator" },
{"Job-125","Asp.Net Full Stack Developer" },
{"Job-126","MEAN Stack Developer" },
};
return openPostions;
}

public double GetSalary(int empId)
{
IDictionary<int, double> staffSalary = new Dictionary<int, double>()
{
{1,20000},
{2,80000},
{3,200000},
{4,200000},
{5,250000},
};

if (staffSalary.TryGetValue(empId, out double salary)) {
return salary;
}
return 0;

}

public IDictionary<string, string> InternalPositions()
{
IDictionary<string, string> openPostions = new Dictionary<string, string>()
{
{"Job-123","Software Developer- Java" },
{"Job-124","Oracle Adminstrator" },
{"Job-125","Asp.Net Full Stack Developer" },
{"Job-126","MEAN Stack Developer" },
{"Job-127","MERN Stack Developer" },
};
return openPostions;
}
}

MainService class:-

Now let’s modify MainService class also and instantiate Company class with both the interfaces.

public  class MainService
{

public IDictionary<string,string> GetOnlyCandidateDetails() {
var before = GC.GetTotalMemory(true);
ICandidate candidate = new Company();
var after = GC.GetTotalMemory(true);
Console.WriteLine($"Total size occuppied by {nameof(GetOnlyCandidateDetails)} is {after - before}");
var opendPostions = candidate.GetOpenPositions();
return opendPostions;
}

public IDictionary<string, string> GetOnlyEmployeeDetails() {
var before = GC.GetTotalMemory(true);
IEmployee employee = new Company();
var after = GC.GetTotalMemory(true);
Console.WriteLine($"Total size occuppied by {nameof(GetOnlyEmployeeDetails)} is {after - before}");
var opendPostions = employee.InternalPositions();
var salary = employee.GetSalary(2);
return opendPostions;
}

public IDictionary<string, string> GetBothTheDetailsUsingClassInstantiation() {
var before = GC.GetTotalMemory(true);
Company company = new Company();
var after=GC.GetTotalMemory(true);
Console.WriteLine($"Total size occuppied by {nameof(GetBothTheDetailsUsingClassInstantiation)} is {after - before}");
var openPostionsForCandidate = company.GetOpenPositions();

return openPostionsForCandidate;

}
}

Output:-

Below is the output where it is clear that class instantiation is getting more space then Interface instantiation as class object has to load all three methods but in Interface we have less methods to load.

output

Thank you for reading please comment your suggestions, share the article, follow me and Abhima C# Programming publication.

Bhagavad Gita Chapter 4, Verse 35

यज्ज्ञात्वा न पुनर्मोहमेवं यास्यसि पाण्डव |

येन भूतान्यशेषेण द्रक्ष्यस्यात्मन्यथो मयि || 35||

BG 4.35: Following this path and having achieved enlightenment from a Guru, O Arjun, you will no longer fall into delusion. In the light of that knowledge, you will see that all living beings are but parts of the Supreme, and are within me.

--

--

Vishal Pathak
Abhima C# Programming

love ❤ coding, solving some industry problems technologies: JavaScript, C#, Angular, PLSQL, Docker Want to learn: Python, Go language, AI, ML and Cloud