Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are static methods always held in memory?

Tags:

My whole development team thinks, static methods are a terrible thing to use.

I really don't see any disadvantages in some cases. When I needed a stateless method before, I always used static methods for that purpose.

I agree with some of their points, e.g. I know they are quite hard to test (although it's not impossible).

What I don't get is, that they claim, static methods are always held in memory and will fill the basic memory usage. So, if you are using 100 static methods in your program, when the program starts all methods are loaded into memory and will fill the memory unnecessarily. Furthermore static methods increase the risk of memory leaks.

Is that true?

It's quite inconvenient to have to create a new instance of a class just to call the method. But thats how they do it right now, the create a instance in mid of a method and call that method, that could be just a static one.

like image 697
Jannik Avatar asked Dec 08 '15 06:12

Jannik


People also ask

Where are static methods stored?

Static Methods,Primitives and Reference Variables are stored in Java MetaSpace. The actual objects reside in the JAVA heap.

Are static variables always in memory?

Static variables are variables that remain in memory while the program is running i.e. their lifetime is the entire program run. This is different than automatic variables as they remain in memory only when their function is running and are destroyed when the function is over.

Do static method take up memory?

Static data and static methods do not take up memory in individual instances.

How many times are static fields allocated in memory?

Memory allocation for a static variable happens only once in the class area when the class is loaded in the memory. It is also known as a class variable. It is common to all the objects of the class.


1 Answers

There is no distinction between static and instance methods as far as memory is concerned. Instance methods only have an extra argument, this. Everything else is exactly the same. Also the basic way in which extension methods were easy to add to C#, all that was needed was syntax to expose that hidden this argument.

Methods occupy space for their machine code, the actual code that the processor executes. And a table that describes how the method stores objects, that helps the garbage collector to discover object roots held in local variables and CPU registers. This space is taken from the "loader heap", an internal data structure that the CLR creates that is associated with the AppDomain. Happens just once, when the method first executes, just-in-time. Releasing that space requires unloading that appdomain. Static variables are also allocated in the loader heap.

Do not throw away the big advantage of static methods. They can greatly improve the readability and maintainability of code. Thanks to their contract, they cannot alter the object state. They can therefore have very few side-effects, makes it really easy to reason about what they do. However, if they make you add static variables then they do the exact opposite, global variables are evil.

like image 136
Hans Passant Avatar answered Sep 28 '22 01:09

Hans Passant