Memory Management in .Net

AID: 4202
  • Status: Published

1160 points

  • Bysanr_85
  • TypeGeneral
  • Posted on2010-12-07 at 05:19:25
A basic question.. “What is the Garbage Collector?”

The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.”

I wondered why the interviewers changed their facial expressions when I finish saying this answer... :)
It’s because they expect something different from us..

For answering this, we should start with the Managed Heap concept in .Net. It’s not new to us; we all know the basics.

First of all, when we start running our program, the .Net runtime reserves a continuous region of space in memory. This space will be free at this moment. This space region is what we call the Managed Heap.
The Heap also has a pointer, whose job is to point to the region in the heap, where the next object is to be allocated. At this point, since the whole Heap is free, this pointer will point to the base address of the Heap.

Now, consider our program contains a line of code: Object tempObject = new Object();

The “new” operator which we saw in the line has an important role in memory allocation.

The “new” operator first makes sure that the bytes required for allocating the object is free in the Heap. If the required bytes are present, it allocates the memory.

But still, “tempObject” has not become an object yet. This is because .NET runtime has not got any information regarding this entity (“tempObject” is not yet considered an object)

.NET runtime doesn’t know even what type the entity is. By reading the line of code, we now know that the TYPE of tempObject’s is System.Object. But .NET runtime doesn’t know it right now.

All that .NET runtime knows now is, tempObject is some kind of thing for which memory has been allocated.

Then the “new” operator calls the constructor of the class Object, since tempObject is of type Object. This is what we call Object Initialization.

Now since the “new” operator has finished running the constructor of the class Object, it has got all the required information. This is the point, where .NET runtime realizes that tempObject is an object of type System.Object. So now tempObject is not an entity anymore, it’s a fully fledged .NET object.

Now the final task of “new” operator is, it returns the address of this object to the .NET runtime. So now .NET runtime has got the address of this tempObject also, by which it can access this object.

Now as tempObject has been stored in the Heap, the Heap pointer (which we mentioned above) will now point to the next free space in the Heap.

In the same manner, all the objects are stored.

This whole process which is designed by Microsoft, was built under one assumption. The assumption was “Memory space is infinite”.

As we read the process above, .NET runtime keeps on allocating memory on the Heap infinitely, no matter how many objects are present in the program.
As a solution to this, they developed a mechanism which frees up memory where possible, so that their assumption remains true and the system continues to work. This mechanism was named Garbage Collector.
Asked On
2010-12-07 at 05:19:25ID4202
Tags

Garbage Collection

,

Memory management

,

Microsoft

,

.NET

Topic

.NET

Views
615

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top .NET Programming Experts

  1. CodeCruiser

    588,856

    Sage

    6,000 points yesterday

    Profile
    Rank: Genius
  2. kaufmed

    377,702

    Wizard

    10 points yesterday

    Profile
    Rank: Genius
  3. BuggyCoder

    268,007

    Guru

    1,600 points yesterday

    Profile
    Rank: Sage
  4. TheLearnedOne

    232,552

    Guru

    4,900 points yesterday

    Profile
    Rank: Savant
  5. Idle_Mind

    193,005

    Guru

    0 points yesterday

    Profile
    Rank: Savant
  6. JamesBurger

    156,812

    Guru

    2,000 points yesterday

    Profile
    Rank: Sage
  7. wdosanjos

    124,308

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  8. Dhaest

    115,720

    Master

    0 points yesterday

    Profile
    Rank: Genius
  9. sedgwick

    112,918

    Master

    1,600 points yesterday

    Profile
    Rank: Genius
  10. nepaluz

    101,325

    Master

    0 points yesterday

    Profile
    Rank: Sage
  11. MlandaT

    95,921

    Master

    2,100 points yesterday

    Profile
    Rank: Genius
  12. navneethegde

    74,442

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  13. Masteraco

    70,367

    Master

    0 points yesterday

    Profile
    Rank: Wizard
  14. binaryevo

    70,365

    Master

    0 points yesterday

    Profile
    Rank: Guru
  15. ambience

    69,104

    Master

    0 points yesterday

    Profile
    Rank: Sage
  16. emoreau

    68,230

    Master

    0 points yesterday

    Profile
    Rank: Genius
  17. PaulHews

    49,486

    0 points yesterday

    Profile
    Rank: Genius
  18. AndyAinscow

    45,290

    0 points yesterday

    Profile
    Rank: Genius
  19. Chinmay_Patel

    43,411

    0 points yesterday

    Profile
    Rank: Genius
  20. ged325

    41,700

    2,600 points yesterday

    Profile
    Rank: Genius
  21. RolandDeschain

    41,317

    0 points yesterday

    Profile
    Rank: Sage
  22. nishantcomp2512

    39,486

    0 points yesterday

    Profile
    Rank: Wizard
  23. tommyBoy

    36,550

    0 points yesterday

    Profile
    Rank: Genius
  24. mroonal

    35,000

    0 points yesterday

    Profile
    Rank: Sage
  25. santhimurthyd

    34,650

    0 points yesterday

    Profile
    Rank: Wizard

Hall Of Fame