Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8071935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:50:53+00:00 2026-06-05T13:50:53+00:00

MyObject obj = new MyObject(); I know that ‘new’ keyword will call the constructor

  • 0
MyObject obj = new MyObject();

I know that ‘new’ keyword will call the constructor and initialize the object in managed heap.

My question is how the CLR doing the below.

  1. How the above line is executed by CLR?
  2. How the memeory is allocated for the object by CLR?
  3. How CLR determine the size of the object?
  4. How CLR will come know it, if there is no space to allocate memory for the object from the heap?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T13:50:55+00:00Added an answer on June 5, 2026 at 1:50 pm

    When a reference type is created (class , delegate, interface, string, or object), it’s allocated onto the heap..Net has four different heaps: (gen0, gen1, gen2)(Small object Heap), and LOH (Large Object Heap). Everything that’s 85k or smaller goes on one of the first three heaps, depending on creation time (moved from gen0 to gen1 to gen2, etc). Objects larger than 85k get placed on the LOH. The LOH is never compacted, so eventually, allocations of the type I’m doing will eventually cause an OOM error as objects get scattered about that memory space. These are known as managed heaps.

    To create an object, all you need to do is use the new keyword; .NET will take care of creating, initializing and placing the object on the right heap, and reserving any extra memory necessary. After that you can pretty much forget about that object, because you don’t have to delete it when you’re finished with it.

    when you create a reference type object using new keyword it is placed on heap and its reference is used mostly in current running stack. there are other possible sources where your object could be used as refrence:

    1. global/static object references
    2. CPU registers
    3. object finalization references (more later)
    4. Interop references (.NET objects passed to COM/API calls)
    5. stack references(mostly used here)

    These 5 are actually GC root nodes from where object reference hierarchy is formed.Imagine the classic Customer class, which usually has a collection storing Order classes. When an Order is added to the order collection, the collection itself then holds a reference to the added order. If the instance of the Customer class had a stack reference to it as well.

    this is how hierarchy is formed for a complex object, this is how GC see the references.

    e:g.
    stack-reference for Customer object -> reference to List object of orders -> reference individual orders.

    anything that loose reference from these 5 roots is prone to GC.

    how memory is allocated to objects is bit complex and it usally grow by time as MKK as specified http://msdn.microsoft.com/en-us/magazine/cc163791.aspx

    a simple example could be:

    class MyClass 
    { 
        string Test="Hello world Wazzup!"; 
        byte[] data=new byte[86000];  
    }
    

    It’s easy to assume that the size of MyClass when allocated includes:

    • 19 characters

    • 86,000 bytes.

    In fact, the object’s size will only include general class stuff, and the memory required to store the object pointers to the string and the byte array (class level variables), which are then separately allocated onto the heaps. The string will be allocated on the SOH, and its object reference held by the instance of the class; the byte array will be allocated onto the LOH, as it’s bigger than 85 KB.

    SOH can be compacted and there will be no fragmentation in it, while LOH can have fragmentation in memory.

    Regards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method -(void)myMethod:(MyObject*)obj And I am detaching a new thread [NSThread detachNewThreadSelector:@selector(myMethod)
MyObject obj = new MyObject(para1); In Java I can always do this while switching
I have the following code MyObject * func1() { MyObject * obj = new
This is my object : var obj = { a:{ x:1 y:function(){return this.x //works
i have a MyObject. when my program runs, i create a new MyObject self.myObject
What's the different between var myObject : Sprite = new Sprite(); and var myObject
I would like to know if there is any special way that one would
My question are commented in the code ImageView img = new ImageView(); this.layout.addView(img); MyObject
If I have a loop and create a new object inside it for (
In C++, when you have a function that takes a reference to an object,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.