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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:24:01+00:00 2026-06-10T11:24:01+00:00

I have a custom type MyClass and a factory class Factory that creates objects

  • 0

I have a custom type MyClass and a factory class Factory that creates objects of type MyClass upon request.

The Factory stores created objects in a dictionary because objects are expensive to create and there are times when same object can be asked to be created multiple times. Each object with the same tag must be created one time only.

class MyObject
{
    // .. not important
}

class Factory
{
    private Dictionary<int, MyObject> m_objects = new Dictionary<int, MyObject>();

    public MyObject CreateObject(int tag, params object[] parameters)
    {
        MyObject obj;
        if (!m_objects.TryGetValue(tag, out obj))
        {
            obj = new MyObject();
            // .. some initialization
            m_objects.Add(tag, obj);
        }

        return obj;
    }
}

Objects might be and might be not stored somewhere outside Factory for unknown amount of time. Objects might be changed after creation. There are times when Factory stores reference to an object and the object is not stored anywhere else.

Now I want let garbage collector to do its job. I want objects not stored anywhere outside Factory and not changed to be collected.

My first thought was to use weak references for values in dictionary inside Factory but it seems like this approach does not handle “changed and unreferenced” case.

How should I store created objects in a dictionary so they are:

  • Could be garbage collected
  • Are not garbage collected when not referenced but changed?
  • 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-06-10T11:24:02+00:00Added an answer on June 10, 2026 at 11:24 am

    Expanding on CodesInChaos’ answer, I think you’re on the right track using weak references in your original Dictionary and I think it makes sense to use a second data structure – probably a HashSet would work well here – that just holds references to objects which have been changed.

    Remember, storing a reference to an object is cheap. I’d think you’d have to be on the order of millions or billions of references before it starts becoming a concern.

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

Sidebar

Related Questions

I have a custom type that I created, which should be persisted in a
I have mapped class, that have ID with custom type <id name=idPersistent type=objectID column=aa_id
I have an array myArr which contains objects of custom class..e.g. objs of type
I have a custom class that looks like: function myClass(){ var thing; var thingtype;
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
I have created a custom post type named People. I have created a page
i have created a custom post type named Books & i have a widget
I want to distinctly determine if the type that I have is of custom
I have a class MyClass . If I serialize it without implementing custom converter
I have a java class that looks like public class MyClass { private final

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.