Static objects in ASP.NET a waste of memory?

The other day I'm just wondering. I'm not quite sure how ASPX handles garbage disposal, but as far as I can tell, "finished loading" does not delete static memory values ​​or after a page reload. Static, at least in terms of C, means that memory allocation follows your program until the program itself is shut down. Is it the same in aspx? If I have a static value, and I go from page A to page B, is the static value still stored in RAM until they leave the application or this value is deleted if I'm no longer on page A? (go to another website by deleting your instance from the application pool on the server).

From what I experienced:

public static class foo { public static int x; } protected void Page_Load(object sender, EventArgs e) { foo.x++; //This will continue to increment from the last value before reload } 
+6
source share
2 answers

In ASP.NET, static classes should be avoided. They remain in memory until the application is restarted and exposed to many concurrency errors and race conditions.

And closing a user session (browser session) does not restart the application! They remain in memory even if the user leaves and returns. So really really avoid static classes!

+5
source

This is the standard CLR execution model; it is no different for asp.net. A static object is considered the root of the application and does not collect garbage.

This is an old article on how garbage collection works in .net, but I think all the principles are the same: http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

0
source

Source: https://habr.com/ru/post/918603/


All Articles