Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what would be the memory allocated on heap if the derived class object is created?
Is the memory allocated to derived class or it includes memory for base class too?
If yes, then what if I consecutively derive 10 times?
Doesn't this affect performance of program?
I mean memory of 10 classes should be created if I call last derived class. Isn't this a overhead on performance? please explain the exact memory allocation pattern

What I have tried:

this is a conceptual question. thus I couldn't find any way for implementing any of strategy
Posted
Updated 11-Jun-18 20:41pm
Comments
Richard MacCutchan 11-Jun-18 9:08am    
You can find the answer by creating a few classes and objects and checking their size at run time.

There is no completely accurate way to determine instantiated object size, but this code shown in this example should get you pretty close:

C#
private static long GetObjectSize(object obj)
{
    long size = 0;
    using (Stream s = new MemoryStream()) 
    {
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(s, obj);
        size = s.Length;
    }
    return size;
}


I created a simple class with two string properties, and then another class that derives from the first one without any additional properties/code/fields.

The non-derived version (with the code above) showed 202 bytes.

The derived version showed 225 bytes.

There is some additional memory consumption (and that's to be expected), but I leave it up to you to determine if the increased memory consumption is linear or geometric with more complex base classes.

In the end, it really is irrelevant. You shouldn't code for (false/imagined?) memory optimization at the expense of maintainability.

If you're interested in memory allocation in .Net, just google it and read for as long as you can maintain interest in the subject.

I forgot to mention that the code I provided will only work if the classes in question have the [Serializable] attribute applied.

EDIT ==================================

After some cursory testing, it appears as if the length of the object names (and the names of their properties and methods) is a determining factor in how much additional memory is allocated to a derived class. Also, despite that, the amount of additional memory consumed by an inheriting class (given my very simple classes) appears to be a reasonably static percentage (around 11%).
 
Share this answer
 
v5
Comments
BillWoodruff 12-Jun-18 2:20am    
+5
No, there is only "one" instance of the "final" derived class.

There are no "intermediate" derived classes "chained" together.

Think of "sub-classing" (in some ways) as a "logical" cut-and-paste of functionality from a related, more primitive, class.
 
Share this answer
 
You might think of the base class, and the classes that descend from the base class, either directly, or descend from a class that has the base class as its root ancestor ...

as being a production line: each time you create an instance of a class: the different parts of the production line are called into "action" to produce the instance.

The memory consumption of an instance is an outcome of what fields, methods, properties, etc., were declared at each "level" in the class chain of ancestors.

I think it's valuable in learning OOP to keep focused on the difference between declaring structure ... creating classes, interfaces, structs ... and instantiating that structure into usable run-tine objects.

The Class is a blue-print for a "house:" instantiation is the process of building the house; how much the house "cost" is a function of the cost of its materials and labor.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900