Static object in Global.asax vs Application State

G

Guest

Hi

I have a declared a static DataSet object SOBJ in Global.asax.I also have a
localy defined DataSet LSOBJ in Global.asax which I am storing in Application
State.Is there any technical differences in the way both the objects are
handled by IIS.
Are both objects stored in different memory spaces?
I can access both the objects in my web page.

I will be grateful if some one can help me understand the difference.

Regards
Vishwanathan
 
S

Scott Allen

Hi

I have a declared a static DataSet object SOBJ in Global.asax.I also have a
localy defined DataSet LSOBJ in Global.asax which I am storing in Application
State.Is there any technical differences in the way both the objects are
handled by IIS.

The Application object is basically a hashtable - it stores object
references that you can retrieve with a key value. The Application
object also has some thread safety built-in, whereas a static member
does not.

IIS doesn't know what is happening inside the .NET runtime.
Are both objects stored in different memory spaces?

What do you mean by memory space?
Both objects will live inside the memory space for your application
domain.
 
G

Guest

I'm not sur what is it you call "Application State". What I can say is that a
static variable in the Global.asax file will be shared by the whole ASPNET
application, but there are many instances of the Global class running in the
ASPNET application. Each of them serves one request at a time, but they are
different instances. Could you provide a code sample ?

Regards
François
 
G

Guest

I am facing a problem with the Application State.It expires but I found that
static objects in Global.asax do not expire.I wanted a technical claification
if they were maintained in different mem spaces.
 
G

Guest

Static objects in Global.asax as for any class are created when the class is
first accessed after the application domain has been created, eg when the
first page is accessed after the application started. They are discarded and
recreated if the application domain is recreated (this happens when the
ASPNET application restarts, for example if you have changed your web.config
or if the worker process restarts).

If I read the documentation for HttpApplicationState, it says that one and
only one HttpApplicationState object is created per ASPNET application, and
it is created when a page is first accessed. For me, this is the exact same
thing as a static variable in Global.asax. So, for me, both static variables
in Global.asax and HttpApplicationState are static singletons that reside
inside the application domain of your ASPNET application.

I think that if it looks like HttpApplicationState expires, it may be
because your worker process is recycled (it can happen when it outgrows a
memory limit set in the config file, or lives for longer than a time
specified in the config file), but then again, in this case, your static
variables are recreated too.

Don't forget that since both these variables are static, they are shared by
all instances, and need to be locked before any access (read or write). When
do you create the object you put into the HttpApplicationState ? When do you
create the static object in Global.asax ? I think the solution to your
problem lies here. If you provide the source code, maybe I will be able to
sort it out.

Hope this Helps

François
 
G

Guest

Francois:
Thanks for the insight.I create both the objects in Global.asax file.
eg

public static DataSet DataDictionaryDs = new DataSet();

private DataSet pvtDataDictionaryDs = new DataSet();

Application["pvtDataDictionaryDs "]= pvtDataDictionaryDs ;

pvtDataDictionaryDs =null;

I find that the one in Application State expires and the static remains
active.
 
G

Guest

I'd say it depends on the time you call :
Application["pvtDataDictionaryDs "]= pvtDataDictionaryDs ;
If you call it in the Global.asax constructor, then each time an Global.asax
object is created, it is replaced (and if you have multiple users at the same
time, it can be often). If you call it in Application_Start, it will be
created only when the first HttpApplication is created, thus only once per
application domain creation. I don't think it's a good idea for the
pvtDataDictionaryDs object to be an instance member of the Global.asax class
; it should not be garbage collected when the Global.asax object is, since it
is referenced in your HttpApplicationState object, but who knows, maybe the
link between your object and the HttpApplicationState object is a weak
reference, not valid for the garbage collector.

Try the following :

protected void Application_Start(Object sender, EventArgs e) {
DataSet pvtDataDictionaryDs = new DataSet();
// Load it
Application.Lock();
Application["pvtDataDictionaryDs"]= pvtDataDictionaryDs ;
Application.Unlock();
}

and see what happens... If the object expires, it means that all objects put
into HttpApplicationState that are not referenced outside it end up garbage
collected ! I would call that a bug...

Vishwanathan Raman said:
Francois:
Thanks for the insight.I create both the objects in Global.asax file.
eg

public static DataSet DataDictionaryDs = new DataSet();

private DataSet pvtDataDictionaryDs = new DataSet();

Application["pvtDataDictionaryDs "]= pvtDataDictionaryDs ;

pvtDataDictionaryDs =null;

I find that the one in Application State expires and the static remains
active.




François Lemaire said:
Static objects in Global.asax as for any class are created when the class is
first accessed after the application domain has been created, eg when the
first page is accessed after the application started. They are discarded and
recreated if the application domain is recreated (this happens when the
ASPNET application restarts, for example if you have changed your web.config
or if the worker process restarts).

If I read the documentation for HttpApplicationState, it says that one and
only one HttpApplicationState object is created per ASPNET application, and
it is created when a page is first accessed. For me, this is the exact same
thing as a static variable in Global.asax. So, for me, both static variables
in Global.asax and HttpApplicationState are static singletons that reside
inside the application domain of your ASPNET application.

I think that if it looks like HttpApplicationState expires, it may be
because your worker process is recycled (it can happen when it outgrows a
memory limit set in the config file, or lives for longer than a time
specified in the config file), but then again, in this case, your static
variables are recreated too.

Don't forget that since both these variables are static, they are shared by
all instances, and need to be locked before any access (read or write). When
do you create the object you put into the HttpApplicationState ? When do you
create the static object in Global.asax ? I think the solution to your
problem lies here. If you provide the source code, maybe I will be able to
sort it out.

Hope this Helps

François
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top