tags:

views:

119

answers:

2

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

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

+5  A: 

I'll rewrite it a little:

ClassA()
{
  memberB = new ClassB();
}

MethodA()
{
  memberB = new ClassB();
}

In this case, memberB holds a reference to an instance of classB.

In the constructor for classA, new instance of classB is created (call it instance 1) and reference to this instance is assigned to memberB.

When you call methodA, another instance of classB is created, and its reference gets assigned to memberB. memberB now points to instance 2. instance 1 is not referenced in code anymore, and the GC cleans it as soon as it gets called.

Therefore I would like to erase, yes you heard me, erase them from existence and then re-creating them again at a later time.

Whole point of C# is that you don't mess with memory. Sure, you can call GC.Collect() anytime you want, but it is not guaranteed to free all your unreferenced instances (though it does, as practice shows).

If you want fine control over your resources, implement your resource demanding classes using C++/CLI, inherit them from IDisposable, and call them in scope of using directive.

Quassnoi
i think you mean "now points" not "not points"
Jason Punyon
+1  A: 

Here's the initial setup:

Instance of ClassA (original)
  Field refers to an instance of ClassB (original)
    Field refers to an instance of ClassC (original)

MethodA gets called on the original instance of ClassA. Here's the after picture

Instance of ClassA (original)
  Field refers to an instance of ClassB (new)
    Field refers to an instance of ClassC (new)

Instance of ClassB (original) - no references can reach
  refers to an instance of ClassC (original)

Since no references in your program can reach the original ClassB and original ClassC, the garbage collector is free to collect it.

David B