4

Are static variable values the same within one session or are their values the same at application server level?

2

3 Answers 3

11

They're at an AppDomain level - that's the same for all static variables, whether they're in ASP.NET or not.

So:

  • If you use the same class from different AppDomains, you'll get separate variables
  • If your AppDomain is recycled, you'll get separate variables
  • If two requests go to different machines, you'll get separate variables
  • If two concurrent requests hit the same AppDomain, they can mess with each other (so things like count++ aren't safe)
4
  • I think that last one means there is no locking and that's what may cause dirty reads, right?
    – Ken D
    Jul 26, 2011 at 13:26
  • 1
    @LordCover: Well, not just dirty reads - but non-atomic increments, unless you're careful. count++ is a three-stage operation: read, increment, write. Two threads could both read, both increment, and then both write, resulting in a single increment overall.
    – Jon Skeet
    Jul 26, 2011 at 13:28
  • If two requests go to different machines, you'll get separate variables.. is this means that request initiated from 2 different machines?? Jul 26, 2011 at 14:09
  • @Ahsan: No, I mean if you've got a server farm for the web app, and two requests - whether from the same machine or not - hit different web servers.
    – Jon Skeet
    Jul 26, 2011 at 14:11
0

I think as long as iis process is there the static variable will hold its value

0

The variable will still in memory until the restart of the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.