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

  • Home
  • SEARCH
  • 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 114525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:52:28+00:00 2026-05-11T02:52:28+00:00

I’m fairly new to C# but I will try to make this quick! ;)

  • 0

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

  • 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. 2026-05-11T02:52:29+00:00Added an answer on May 11, 2026 at 2:52 am

    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.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.