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 7042543
In Process

The Archive Base Latest Questions

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

Consider these two classes: public class A { B b; public A(B b) {

  • 0

Consider these two classes:

public class A
{
     B b;
     public A(B b) { this.b = b; }
}

public class B
{
     A a;
     public B() { this.a =  new A(this); }
}

If I have classes designed like above, would the objects of such classes be collected by Garbage Collector (GC)?

Suppose I do this:

void f()
{
     B b = new B();
}

In this method, I create an instance of B called b, and when the method returns, b goes out of scope, and the GC should be able to collect it, but if it were to collect it, it would have to collect a first which is the member of B, and to collect a, it needs to collect b first which is the member of A. It becomes circular. So my question is : is such circular reference going to prevent GC from collecting the objects?

  • If yes, then how can we avoid this problem? How can we make sure that we don’t have circular reference in our class design? Is there any tool (or compiler option) that helps us detecting circular reference?
  • If no, where and why do we use WeakReference class? What is its purpose?
  • 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-05-28T02:10:52+00:00Added an answer on May 28, 2026 at 2:10 am

    The .NET garbage collector can absolutely handle circular references. The very high level view of how the garbage collector works is …

    • Start with locals, statics and GC pinned objects. None of these can be collected
    • Mark every object which can be reached by traversing the children of these objects
    • Collect every object which is not marked.

    This allows for circular references to be collected just fine. So long as none of them are reachable from an object known to be uncollectable then the circular reference is essentially irrelevant.

    Note: I realize I’ve left out many fun details in order to keep this answer simple and direct

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

Sidebar

Related Questions

Consider the two classes: public class Point { public int x; public int y;
We have two PHP5 objects and would like to merge the content of one
Consider this: class FluffyThing { public: FluffyThing() { m_pMyFur = new Fur; } virtual
Consider these two classes mapped to the same table. One is readonly via mutable=false.
To give an idea of my requirement, consider these classes - class A {
Consider the scenario I have values assigned like these Amazon -1 Walmart -2 Target
I would like to store insances of two classes in a dictionary structure and
Consider, I have the following 3 classes / interfaces: class MyClass<T> { } interface
Today, I've stumbled over the following: Consider two classes NewClass and NewClass1, which have
Consider I have some abstract Vehicle class and car, truck, motorcycle abstract classes which

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.