By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,272 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,272 IT Pros & Developers. It's quick & easy.

Static object in Global.asax vs Application State

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hello
I think both object have same scope.

bhawin13

Nov 19 '05 #2

P: n/a
On Tue, 5 Jul 2005 20:17:02 -0700, Vishwanathan Raman
<Vi***************@discussions.microsoft.com> wrote:
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.
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


--
Scott
http://www.OdeToCode.com/blogs/scott/
Nov 19 '05 #3

P: n/a
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

"Vishwanathan Raman" wrote:
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

Nov 19 '05 #4

P: n/a
Hi!

I think you'd find out a lot if you check out this page:
http://msdn.microsoft.com/library/en...asp?frame=true

Also, you might get better performance if you use the HttpContext.Cache
object instead of the Application.Contents object.. (Although you'd use some
more memory when the application gets a lot of users)
You find information about both on the link above. :)

HTH,
Lars-Erik

"Vishwanathan Raman" <Vi***************@discussions.microsoft.com> wrote in
message news:94**********************************@microsof t.com...
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

Nov 19 '05 #5

P: n/a
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.

"Lars-Erik Aabech" wrote:
Hi!

I think you'd find out a lot if you check out this page:
http://msdn.microsoft.com/library/en...asp?frame=true

Also, you might get better performance if you use the HttpContext.Cache
object instead of the Application.Contents object.. (Although you'd use some
more memory when the application gets a lot of users)
You find information about both on the link above. :)

HTH,
Lars-Erik

"Vishwanathan Raman" <Vi***************@discussions.microsoft.com> wrote in
message news:94**********************************@microsof t.com...
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


Nov 19 '05 #6

P: n/a
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

"Vishwanathan Raman" wrote:
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.

"Lars-Erik Aabech" wrote:
Hi!

I think you'd find out a lot if you check out this page:
http://msdn.microsoft.com/library/en...asp?frame=true

Also, you might get better performance if you use the HttpContext.Cache
object instead of the Application.Contents object.. (Although you'd use some
more memory when the application gets a lot of users)
You find information about both on the link above. :)

HTH,
Lars-Erik

"Vishwanathan Raman" <Vi***************@discussions.microsoft.com> wrote in
message news:94**********************************@microsof t.com...
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


Nov 19 '05 #7

P: n/a
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" wrote:
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

"Vishwanathan Raman" wrote:
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.

"Lars-Erik Aabech" wrote:
Hi!

I think you'd find out a lot if you check out this page:
http://msdn.microsoft.com/library/en...asp?frame=true

Also, you might get better performance if you use the HttpContext.Cache
object instead of the Application.Contents object.. (Although you'd use some
more memory when the application gets a lot of users)
You find information about both on the link above. :)

HTH,
Lars-Erik

"Vishwanathan Raman" <Vi***************@discussions.microsoft.com> wrote in
message news:94**********************************@microsof t.com...
> 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

Nov 19 '05 #8

P: n/a
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" wrote:
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" wrote:
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

"Vishwanathan Raman" wrote:
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.

"Lars-Erik Aabech" wrote:

> Hi!
>
> I think you'd find out a lot if you check out this page:
> http://msdn.microsoft.com/library/en...asp?frame=true
>
> Also, you might get better performance if you use the HttpContext.Cache
> object instead of the Application.Contents object.. (Although you'd use some
> more memory when the application gets a lot of users)
> You find information about both on the link above. :)
>
> HTH,
> Lars-Erik
>
> "Vishwanathan Raman" <Vi***************@discussions.microsoft.com> wrote in
> message news:94**********************************@microsof t.com...
> > 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
>
>
>

Nov 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.