简体   繁体   中英

Understanding threading in asp.net and how server memory is allocated

If for instance, 3 requests are made to the server, does that mean 3 instances of the web application are put into memory?

What if you used a locked singleton or cached a class so each time it checks to see if the object exists or not and creates the object if it doesn't exist, does that mean that the object is cached for each request? or is it cached once and reused for each request?

If I locked a singleton, does that mean The object using the singleton is created once and each request uses the same object? or does it mean the object is created using the singleton for each request but is not created again by the same thread?

If I used System.Web.HttpRuntime.Cache["key"]; to cache a 1 mb object and 10 requests are made, am I using up 10 mb's of memory on the server? if I created a new object using a singleton... what occurs in terms of threading and memory allocation?

In IIS an AppDomain is created for each Application and will share a process with other applications in the same AppPool.

So 3 requests to the server within the same Web Application will share the same AppDomain.

How that affects caching and singletons:

From the docs on System.Web.Caching.Cache :

One instance of this class is created per application domain, and it remains valid as long as the application domain remains active.

When you lock an object (like a singleton) the lock's scope is an AppDomain as well.

Yours instance - process and yours 3 requests - executed by threads. So singleton object will be shared between all 3 requests. Your cache also will be shared between requests. In your example you will allocate only 1 Mb (it will update 10 times). Garbage collector will collect unused space.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM