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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:13:39+00:00 2026-05-26T20:13:39+00:00

I have a class that is managed by ninject as a singleton that looks

  • 0

I have a class that is managed by ninject as a singleton that looks roughly like this:

class InMemoryDb
{
    IInitialiser _initialiser;
    SomeBackingStore _store;

    InMemoryDb(IInitialiser initialiser)
    {
        _initialiser = initialiser;
    }

    void Init()
    {
        //once init'd i don't care what happens to the _initialiser field
        _store = _initialiser.Initialise();
    }
}

I have the IInitialiser suppied via Ioc (actually everything is container managed). My problem is, once initialised, the _initialiser object is never garbage collected (and why would it, given it is techinically still being referenced by the InMemoryDb singleton object) and it sits in the application consuming memory (it is upwards of 5gig). How can I cleanly structure my code, so that my objects are still wired up via Ioc, and disposed of after use. Essentially once it is initialised, I can be certain that its safe to dispose the object.

Unfortunately, I have so far resorted to:

void Init()
{
    _store = new Initialiser().Initialise();
}

This will eventually get cleaned up by the GC as it is out of scope once the Init() method finishes executing, but it makes my code untestable and has a smell about it. I also thought of doing:

void Init()
{
    _store = SericeLocator.Get<IInitialiser>().Initialise();
}

But again, this isn’t ideal as it polutes my code with a ServiceLocator call.

Any suggestions on how to structure this better?

  • 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-26T20:13:40+00:00Added an answer on May 26, 2026 at 8:13 pm

    Since it’s only the Init method which requires an initializer it would make more sense to have this method take it as argument instead of using constructor injection:

    class InMemoryDb
    {
        SomeBackingStore _store;
    
        void Init(IInitialiser initialiser)
        {
            _store = initialiser.Initialise();
        }
    }
    

    Then it is the responsibility of the caller of the Init method to provider the initializer. You could also use a boolean isInitialized field which will indicate whether the Init method was called. This field will be used by other methods that potentially rely on the _store field to ensure that it is being properly initialized.

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

Sidebar

Related Questions

I have a managed class that uses a COM that looks like this. Public
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a class that conforms to UISearchDisplayDelegate and contains a UISearchBar. This view
I have an x64 managed C++ class that needs to be tested using Visual
I have managed to serialize a class that inherits from a base class to
I have a base class that owns a managed disposable resource (.NET PerformanceCounter). I
I have a CoreDataUtilities class with a class method that saves a managed object
I have a document class that contains a list of tags. Something like: class
I have a class like this: public final class Foo { public native int
I have a webservice class that will be in a managed package and distributed

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.