Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# class instance with static method vs static class memory usage

Tags:

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.

like image 756
jtb Avatar asked Feb 27 '10 17:02

jtb


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

Why do we write C?

We write C for Carbon Because in some element the symbol of the element is taken form its first words and Co for Cobalt beacause in some elements the symbol of the element is taken from its first second letters, so that the we don't get confuse.


2 Answers

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.

like image 174
Guffa Avatar answered Sep 27 '22 20:09

Guffa


The second form creates a temporary Loader object (which is very cheap). You will always have to load the Loader class, no matter which approach you choose.

There is very little performance (memory saving) to gain here. You would normally choose for a static member in a static class if there is no 'state' needed outside the methods local vars.

like image 21
Henk Holterman Avatar answered Sep 27 '22 20:09

Henk Holterman