Im my asp.net application i have an object in in memory session which stores the instance of “School” object, In this students collection have 100’s of students. After i finish using the Students object if i assign Students= null / Students.Clear().
My question is, Will it free the memory occupied by Students object ? Or it will wait unit the school object is removed from session completely.
Size obtained through serializing:
Total memory size of school object with 100’s of student: 10kb
school object size After removing Student object : 1 kb
Will the above result reflect in RAM too ?
public class school
{
public string SchoolName { get; set; }
public string SchoolAddress { get; set; }
public List<student> Students { get; set;}
}
public class student
{
public string Name { get; set; }
}
As you correctly assumed and as already pointed out in the comments above, the Garbage Collector will free the memory of the objects if there is no more reference to it in your running application. The Session is no exception here.
If you have a school object in the Session and you set the Students object to null, and have no other reference to the collection objects anywhere, the GC will collect them sooner or later.
This article also has a passage about the topic.