views:

74

answers:

2

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?

+5  A: 

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.

Reed Copsey
Is there a copy of the static method stored anywhere in memory? My assumption was that when you referenced a static method, it would be stored somewhere, and then each subsequent reference would use that instance of the method, but I understand this assumption may be wrong.
iobrien
"Methods", whether static or instance methods, are loaded a single time in memory when they're brought into the process space, and compiled by the JIT. There really is no difference in method usage between static and non-static. Focus on whether static methods are appropriate in your specific case - and use that to determine whether a method should be a static or an instance method.
Reed Copsey
There is a very tiny effect on memory - as extra code is generated to pass a "this" pointer to methods that don't use it.
Qwertie
@Qwertie: That effect is just changing the memory requirements of the stack during the method call, but not changing the "long term" effect on memory storage. That being said, the stack is preallocated per thread, so the "real" memory requirement doesn't change. ;)
Reed Copsey
+2  A: 

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.

Hans Passant
This may go beyond the scope of the original question, but what is stored on the heap when a method, static or not, is called? Is it just the method itself, or an instance of the class containing the method, or something else altogether?Is this memory ever collected by garbage collection?
iobrien
Nothing gets stored on the heap during a method call. Arguments are passed through CPU registers and the stack. Nothing needs to be collected. Petzold's book "Code" may interest you.
Hans Passant
Ok that makes a lot of sense. So really, the only reason to convert my methods from non-static to static would be to access them without class instantiation? Thanks for your recommendation.
iobrien
The reason to *write* a static method is because it doesn't need to reference any instance members. Don't go hogwild "converting" methods that should really be instance methods, that isn't nearly worth the payoff. If you have a perf problem then measure first.
Hans Passant