Hello! I'm fairly new to C# but I will try to make this quick! ;)
This is just a theoretical situation, but imagine that:
I) One have a class 'ClassA' with a 'ClassB' inside it and a 'ClassC' inside of 'ClassB'.
II) ClassA have a method which gets called. The method itself is (don't know the name for this) 'constructing' a new ClassB with the already existant classB variable. As follows:
/*CLASS A*/
ClassA
{
ClassB classB;
ClassA()
{
classB = new ClassB();
}
MethodA()
{
classB = new ClassB();
}
// Other Methods...
}
/*CLASS B*/
ClassB
{
ClassC classC;
ClassB()
{
classC = new ClassC();
}
// Methods...
}
/*CLASS C*/
ClassC
{
// Loads of variables being used...
}
So what I want to know is:
a) What would happen memory-wise with classB? Would the same space in the heap be used for the class or would new space be allocated? If the latter is the case, will the previous memory be freed somehow by GC?
b) Whatever is the outcome of the above, what would happen to classC, loaded with variables munching memory?
Direct answers or just plain links to a bit of reading would be greatly appreciated. I've tried to find the answer for myself but it seems that no one explains such absurd(?) situations.
Before you start theorizing about the origin of this question I will already tell you. I have indeed put myself in a similar situation which is about this:
- PrimaryClass
- Sub-Class1
- Couple of classes
- Sub-Class2
- Couple of classes
- Sub-Class1
So this three-leveled app is sometimes alterating between Sub-Class1 and 2 which both control a 'scene'. These 'scenes' have some classes to help managing stuff (the usual). This is ( by my thoughts ) unscopeable.
The problem is that both Sub-Class1 and Sub-Class 2 can eat up a bit of memory and is using some 'assets'. Therefore I would like to erase, yes you heard me, erase them from existence and then re-creating them again at a later time. I've done a bit of reading but can't come up with a solution. (As said, I'm new to C# and this whole managed .net thing). Any suggestions on this front would ofcourse be appreciated.
Alright, I wrote a bit more than planned but that's it! Looking forward to some insight into this! :)
Regards, Robert