Memory usage when converting methods to static methods

后端 未结 2 2130
眼角桃花
眼角桃花 2021-02-20 13:52

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 o

2条回答
  •  花落未央
    2021-02-20 14:41

    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.

提交回复
热议问题