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

The Archive Base Latest Questions

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

How does C#, or other languages for that matter, handle memory allocation (and memory

  • 0

How does C#, or other languages for that matter, handle memory allocation (and memory de-allocation) between these two scenarios:

1.) A method on a static class is invoked.

public Program {
    Foo foo = Loader.load();
}

public static Loader {
    public static Foo load() {
        return new Foo();
    }
}

2.) A method is invoked on an instance, which then falls out of scope.

public Program {
    Foo foo = new Loader().load();
}

public Loader {
    public Foo load() {
        return new Foo();
    }
}

I suppose the static class is loaded, and remains, in memory; whereas the class instance succumbs to garbage collection at C#’s leisure. Are there any pros or cons to these two paradigms? Is there ever a time when you have a class that never needs to be instantiated (i.e. some sort of resource loader or factory), but you use the second methodology anyway to take advantage of garbage collection?

The important part of my question is whether or not the first paradigm, while being conceptually correct in some circumstances, may suffer from holding on to memory unnecessarily.

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

    Your second example doesn’t work, so let’s explore the real options:

    1.) A method on a static class is invoked.

    public Program {
       Foo foo = Loader.Load();
    }
    
    public static Loader {
       public static Foo Load() {
          return new Foo();
       }
    }
    

    2.) A static method in a non-static class is invoked.

    public Program {
       Foo foo = Loader.Load();
    }
    
    public Loader {
       public static Foo Load() {
          return new Foo();
       }
    }
    

    3.) An instance method is invoked on an instance

    public Program {
       Foo foo = new Loader().Load();
    }
    
    public Loader {
       public Foo Load() {
          return new Foo();
       }
    }
    

    The two first are the same. Calling a static method is the same regardless if the class is static or not.

    The third option will create an instance of the class on the heap. As the class has no data members, it will only be something like 16 bytes. It will be garbage collected eventually, but due to the small size it doesn’t matter much when that happens.

    Calling an instance method is also slightly different from a static method. A reference to the class instance is sent along, that you can access through the this keyword. It makes little difference in this case as there is no real data in the object to access.

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

Sidebar

Ask A Question

Stats

  • Questions 351k
  • Answers 351k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer So, Microsofts Sync Framework will help with this. Introduction Couple… May 14, 2026 at 7:21 am
  • Editorial Team
    Editorial Team added an answer Are you using the REST API ? If it's purely… May 14, 2026 at 7:20 am
  • Editorial Team
    Editorial Team added an answer I'm the one that developed the open source Java project… May 14, 2026 at 7:20 am

Related Questions

Is it possible to do CAD/CAM software without having to use C++? My company
This caught my attention last night. On the latest ALT.NET Podcast Scott Bellware discusses
Sadly, I have a legacy PHP4 system that I continuously need to add features
From discussions I've had about language design, it seems like a lot of people
As many young programmers do, I learned the usefulness of inserting numerous print-to-console statements

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.