Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Memory allocation for static members in C#

What I have tried:

Memory allocation for static members in C#
Posted
Updated 18-Sep-17 3:11am
v2
Comments
PIEBALDconsult 18-Sep-17 9:19am    
And don't forget:
http://csharpindepth.com/Articles/General/BeforeFieldInit.aspx

 
Share this answer
 
Comments
knackCoder 18-Sep-17 7:33am    
Thanks for the link. I could understand that static members are stored in Heap itself but still I am not clear about the time of allocation of memory. Could you please share more information about it.
F-ES Sitecore 18-Sep-17 7:41am    
The answer to your question is in the results of that search very close to the top. If you put in at least a little effort yourself rather than expecting people to spoon-feed you you'd have the answer already.
Richard MacCutchan 18-Sep-17 7:59am    
Lol.
Allocation for static data is done when the containing class is used for the first time.
 
Share this answer
 
Comments
knackCoder 19-Sep-17 0:54am    
Public static void Main()
{
Class1 obj = new Class1(1,2);
int val = StaticClassName.StaticMethodname(obj);
}
Above I have initially instantiated Class1 object and memory gets allocated.
After that I have called Static method for the first time; So you mean to say that when I called static method at that moment memory got allocated for Static member?
OriginalGriff 19-Sep-17 3:20am    
No - the member takes no memory and requires no allocation.
But when you first access the static class - via that member call - the static class static constructor is called and all static memory allocations for that class are performed immediately before the call to the constructor.
knackCoder 19-Sep-17 6:22am    
I believe your answer 'No' is in context with the example illustrated by me?.
So for a static member, memory gets allocated only when it is accessed for the first time?; and if I call a static member say static method multiple times, does the static constructor gets called each time?
OriginalGriff 19-Sep-17 6:33am    
No, static methods don't take up memory - and all static allocation is done the first time a class is accessed. So if you call a static method on a class, all static field allocations are done then, and the static constructor (if any) is then called. Finally, the method you originally asked for is executed.

Try it: put some Debug.WriteLine instructions in, and watch the order in which things occur.
Possibly you may find some info here: .NET Framework Internals: How the CLR Creates Runtime Objects[^].
 
Share this answer
 
From Microsoft Docs [^]

Quote:
Static members are initialized before the static member is accessed for the first time and before the static constructor, if there is one, is called.


Also worth noting that this is CLR behaviour rather than anything specific to C#.
 
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