Thursday, March 22, 2012

Why static objects could not be persistent in the memory

For example, there is a sentence

public static Dictionary<string, string> cache = new Dictionary<string, string>();

I add some string into this static dictory, the count of cache is greater then zero,

But after twenty~thirty minutes, the count of cache is zero, there is nothing in it.

Is there any one kow why?

thanks for your mention,

all of these objects are static variables and stored in another dll, this dll placed at http://abc.com/bin/ and support to other dll.

I had run some test, applcation object in globa.asax or in aspx file or in others dll, all of there static objects will be lost.

But I need a object could be stored long long long time, from server start to server down.

So any solution to staticify this case?


Hi,

Where are you storing this dictionary object? In Session or Application object or as static member of the Application class?

Application objects have a timeout after which they will expire and your static variables would be lost.

Hope this helps,

Vivek


I guess you lose all static objects each time the worker-process recycles or similar.

Persist them to the file system as XML files, or to a database, etc. I guess you might gather values in Application_OnStart and scatter them in Application_OnEnd.

HTH. -LV


LudovicoVan:

I guess you lose all static objects each time the worker-process recycles or similar.

I agree. Static objects are also not thread safe across multiple requests. Also, whenever the worker process recycles, everything is lost including Sessions, Application objects etc.

-Vivek

0 comments:

Post a Comment