Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:35:56+00:00 2026-05-28T05:35:56+00:00

I am holding some information in static variables defined in page class (not in

  • 0

I am holding some information in static variables defined in page class (not in Global.asax). I only declare variable in code like:

protected static int SomeGlobalUnsecureID;
protected static string SomeGlobalUnsecureString;

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

  1. Hold the same amount of memory regardless of users connected

  2. Change if and only if persistent info is different from the one in
    ‘memory’ (because actually reading the string is time consuming for
    me.

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.)

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:35:57+00:00Added an answer on May 28, 2026 at 5:35 am

    Static variables persist for the life of the app domain. So the two things that will cause your static variables to ‘reset’ is an app domain restart or the use of a new class. In your case with static variables stored in an aspx Page class, you may be losing the static variables when ASP.NET decides to recompile the aspx Page into a new class, replacing the old page class with the new one.

    For those reasons if the system decide to restart or replace the class (.NET doesn’t kill or unload classes/assemblies in a running app domain) then your static variables will reset because you are getting a new class with the restart or replacement. This applies to both aspx Pages and classes in the App_Code folder

    ASP.NET will replace a class if for any reason thinks that need to recompile it (see ASP.NET dynamic compilation).

    You can’t prevent the loss of static variables from an app domain restart, but you can try to avoid it from class replacement. You could put your static variables in a class that is not an aspx page and is not in the App_Code directory. You might want to place them on a static class somewhere in your program.

    public static class GlobalVariables
    {
        public static int SomeGlobalUnsecureID;
        public  static string SomeGlobalUnsecureString;
    }
    

    The static variables are per pool, that is means that if you have 2 pools that runs your asp.net site, you have 2 different static variables. (Web garden mode)

    The static variables are lost if the system restarts your asp.net application with one of this way.

    1. the pool decide that need to make a recompile.
    2. You open the app_offline.htm file
    3. You make manual restart of the pool
    4. The pool is reach some limits that you have define and make restart.
    5. For any reason you restart the iis, or the pool.

    This static variables are not thread safe, and you need to use the lock keyword especial if you access them from different threads.

    Since an app restart will reset your statics no matter what, if you really want to persist your data, you should store the data in a database using custom classes. You can store information per-user in Session State with a database session state mode. ASP.NET Application State/Variables will not help you because they are stored in memory, not the database, so they are lost on app domain restart too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a WPF application I use .txt files for holding some information. An application
I have a class holding complex scientific computations. It is set up to only
In my database I have a table holding some subscription information. I have among
Hello I have table in my database holding some browser information I'm currently using
Greetings, I am trying to play some audio files without holding up the GUI.
I have an app holding an array of instances of a class called SwimmingPool.
I have a variable holding values separated by a comma (Implode), and I'm trying
I'm reviewing a piece of code I wrote not too long ago, and I
Im making a mysql table which will be holding my blog posts and some
I would like some opinion, example and code on how everyone dose this.. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.