Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage when converting methods to static methods

I started using Resharper and it indicated when a method could be made static. Would converting a few hundred methods to static methods increase the memory footprint over a large period of time?

like image 276
Ian R. O'Brien Avatar asked Jun 25 '10 17:06

Ian R. O'Brien


People also ask

Do static methods take memory?

A class defined with many static methods consumes lots of memory if it is called more than once. Since static members of a class are instantiated only once, why does the memory usage increase the more times the methods are called?

Where is memory allocated for static variables in Java?

Static variable's memory is allocated at the start of the program, in regular memory, instead of the stack (memory set aside specifically for the program). the advantage of this is that it makes your variable or procedure totally constant, and you can't accidentally change the value.

What happens if a method is static?

If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.

Where are static variables and static methods stored in memory?

Since these methods and variables cannot be stored in a normal heap, they are stored in a special area called permanent generation(PermGen).


2 Answers

No - Changing to static methods has no effect on memory.

The first time a type is referenced (whether static or non-statically), any static members are initialized and static constructors are run.

However, if you're just considering switching methods from non-static to static, this will have no effect on garbage collection or total memory footprint.

You only have to worry about memory footprint changing if you change class members to be static members. In this case, static fields will stay rooted once the type is accessed, and will not get collected by the GC. This is typically only done when necessary, and by design - you make a member static because you want it to persist.

like image 101
Reed Copsey Avatar answered Oct 18 '22 18:10

Reed Copsey


From the JIT compiler's point of view, there is no difference between a static and an instance method. The machine code for them is very similar, it gets stored in the same kind heap. The only difference is that an instance method has an extra argument.

That extra argument needs to be passed when the method is called. That can cost an extra machine code instruction but not that often. The CPU register (ECX) frequently already has the correct value. There is a difference if an instance method has more than one argument on x86 or more than three on x64, an extra argument has to be passed on the stack rather than through a CPU register. One extra instruction.

Worst case, you are looking at a bit less than a nanosecond. That's going to be hard to measure, the usual problem with micro-optimizations.

like image 42
Hans Passant Avatar answered Oct 18 '22 16:10

Hans Passant