title: Lifetime of ASP.NET Static Variable
body: <p>I am holding some information in static variables defined in page class (not in Global.asax). I only declare variable in code like:</p>
  
  <pre><code>protected static int SomeGlobalUnsecureID;
  protected static string SomeGlobalUnsecureString;
  </code></pre>
  
  <p>and define the variable in PageLoad event. For example, I check ID from the database, if it's different from SomeGlobalUnsecureID, I update SomeGlobalUnsecureID and String from somewhere else, otherwise leave them as is. This is perfectly safe in my app. logic (i.e. those data are not secure, everybody can access them, no problem); only thing I want to accomplish is </p>
  
  <ol>
  <li><p>Hold the same amount of memory regardless of users connected</p></li>
  <li><p>Change if and only if persistent info is different from the one in
  'memory' (because actually reading the string is time consuming for
  me.</p></li>
  </ol>
  
  <p>Now, since I make the check in PageLoad, I have no problems in reloaded pages. However my page is full of WebMethods, and sometimes I see that the static variables are zeroed. And the strange part is; the session is still active even when the static variables are zeroed (so-> no server or app. pool restart etc.)</p>
  
  <p>This is really strange for me. I assume that static variable will hold its value until the application (somehow) ends. But even the Session did not expire, the static variable is zeroed. What do you suggest? Is using application variables a better choice? All documents I've read on web suggest static variables instead of application variables, do I need to declare them somehow different?</p>