Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have found many difference in static and normal method, but most of the answers were around accessibility of members. But there are impacts on memory and lifespan as well.
Can any one tell me with a good example that what happens with RAM and lifespan of static methods when we create static method and normal method.
Thanks in advance.

What I have tried:

Already know the difference between static class and normal class, static function/members and normal properties.
Posted
Updated 3-Nov-16 6:46am
Comments
Philippe Mori 3-Nov-16 11:47am    
Essentially a static method or property does not depends on an instance of an object so it would live for the duration of the program and it would exists only once.

Both static methods and instance are just bits of code, the difference being that instance methods always take at least one parameter 'this'.

So, in terms of the methods themselves, they occupy similar amounts of RAM. This RAM will be set when the module loads (first as MSIL, then as native code after its Jitted) and will be there typically until the process dies taking its RAM with it.

It's unusual to think in terms of memory when talking about methods, its the data that gets passed to them and they act upon which is usually the concern.
 
Share this answer
 
Static methods do not have a lifetime. A static method of a class can be called without instantiating the class. In other words, static methods can be called practically any time you want.

The gotcha with static members is they must be instantiated and they are initialized before the program entry point is called. This is just after the various DllMains are called and libraries are initialized. However, the exact timing of these events is unspecified so for this reason one should avoid using RTL functions to initialize static members because they can be unpredictable. This applies to global objects also.
 
Share this answer
 
Comments
Rob Philpott 4-Nov-16 6:29am    
Hi Rick, think you might be looking at that from a C++ perspective. In C#, static members will only get initialized the first time the type is accessed, and live in the scope and lifetime of the AppDomain they reside in.
The main differences are

1) The static variable exists on the type so if you have a static variable on MyObject and have 100 MyObject objects then you only have one instance of the static variable. If the variable wasn't static there would be 100 instances of it.

2) Static variables are always on the heap even if they would be on the stack if they were non-static, so even if your static variable is an int it is still in the heap and accessed like a reference variable so that will have a knock-on effect in terms of memory management etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900