Leaking Code

M

MattC

Gents,

I have had to persist the viewstate into the Session object, however once
this was done the W3WP process' memory ramped right up and used 300MB of VM.

Below is my code, can anyone see where a leak may be. I am using the
aspnet_state to store my session info.

TIA

MattC

private string NewViewState
{
get{ return "VIEWSTATE_"; }
}

private string ViewStateKey
{
get{ return "__VIEWSTATE_KEY"; }
}

private string UniqueViewStateKey
{
get{ return NewViewState + Request.UserHostAddress + "_" +
Guid.NewGuid().ToString(); }
}

protected override void SavePageStateToPersistenceMedium(object viewState)
{
try
{
if(Conquest.ConfigurationData.ViewStateStorage ==
ViewStateStorage.Session)//if we are implementing our on server viewstate
{
string str = this.UniqueViewStateKey;
LosFormatter oLosFormatter = new LosFormatter();
StringWriter oStringWriter = new StringWriter();
oLosFormatter.Serialize(oStringWriter, viewState);//serialize viewstate
into string writer
oLosFormatter = null;

Session[str] = oStringWriter.ToString(); //store in state server
oStringWriter = null;

RegisterHiddenField(ViewStateKey, str);
RegisterHiddenField("__VIEWSTATE", String.Empty);
}
else
{
base.SavePageStateToPersistenceMedium(viewState);
}
}
catch(Exception e)
{
throw new
ConquestException("PerformancePage.SavePageStateToPersistenceMedium Failed",
null, e);
}
}

protected override object LoadPageStateFromPersistenceMedium()
{
try
{
if(Conquest.ConfigurationData.ViewStateStorage ==
ViewStateStorage.Session)//if we are implementing our on server viewstate
{
object viewstate = null;//return viewstate
string str = Request.Form[ViewStateKey];
LosFormatter oLosFormatter = new LosFormatter();

if (!str.StartsWith(NewViewState)) //check the viewstate exists
{
Conquest.Events.WriteToLog("Failed to locate VIEWSTATE KEY: '" + str
+"'");
ViewStatePersistanceFailure(str);//failure
}
else//view state does exist
{
try
{
string tmp = Session[str].ToString();//get the serialize viewstate
from session
viewstate = oLosFormatter.Deserialize(tmp);//deserialize into object
oLosFormatter = null;
}
catch(Exception e)//catch all and handle gracefully
{
Conquest.Events.WriteToLog("Failed to Load ViewState from " +
Conquest.ConfigurationData.ViewStateStorage.ToString() +
" " +
ConquestException.HandleConquestException(e),System.Diagnostics.EventLogEntryType.Information);

ViewStatePersistanceFailure(str);//Response.Redirect to homepage
}
}
//return what we got from deserialized viewstate
return viewstate;
}
else //use standard viewstate
{
return base.LoadPageStateFromPersistenceMedium();
}
}
catch(Exception e)
{
throw new
ConquestException("PerformancePage.LoadPageStateFromPersistenceMedium
failed", null, e);
}
}
 
B

bruce barker

every page hit adds its viewstate to the session, but is only purged on
session unload (which you need to do to support reposts of the same page).
i'd use a database instead of session for storing the viewstate.

note: there is no guarantee that Request.UserHostAddress will be the same
between the render and post. any user going thru a proxy with nat
translation will often get a different ipaddress for each request. its
common for the proxy to have a pool of nat addresses and round robin them.
it also common for all users behind the proxy to have the same nat'd
ipaddress.


-- bruce (sqlwork.com)



| Gents,
|
| I have had to persist the viewstate into the Session object, however once
| this was done the W3WP process' memory ramped right up and used 300MB of
VM.
|
| Below is my code, can anyone see where a leak may be. I am using the
| aspnet_state to store my session info.
|
| TIA
|
| MattC
|
| private string NewViewState
| {
| get{ return "VIEWSTATE_"; }
| }
|
| private string ViewStateKey
| {
| get{ return "__VIEWSTATE_KEY"; }
| }
|
| private string UniqueViewStateKey
| {
| get{ return NewViewState + Request.UserHostAddress + "_" +
| Guid.NewGuid().ToString(); }
| }
|
| protected override void SavePageStateToPersistenceMedium(object
viewState)
| {
| try
| {
| if(Conquest.ConfigurationData.ViewStateStorage ==
| ViewStateStorage.Session)//if we are implementing our on server viewstate
| {
| string str = this.UniqueViewStateKey;
| LosFormatter oLosFormatter = new LosFormatter();
| StringWriter oStringWriter = new StringWriter();
| oLosFormatter.Serialize(oStringWriter, viewState);//serialize
viewstate
| into string writer
| oLosFormatter = null;
|
| Session[str] = oStringWriter.ToString(); //store in state server
| oStringWriter = null;
|
| RegisterHiddenField(ViewStateKey, str);
| RegisterHiddenField("__VIEWSTATE", String.Empty);
| }
| else
| {
| base.SavePageStateToPersistenceMedium(viewState);
| }
| }
| catch(Exception e)
| {
| throw new
| ConquestException("PerformancePage.SavePageStateToPersistenceMedium
Failed",
| null, e);
| }
| }
|
| protected override object LoadPageStateFromPersistenceMedium()
| {
| try
| {
| if(Conquest.ConfigurationData.ViewStateStorage ==
| ViewStateStorage.Session)//if we are implementing our on server viewstate
| {
| object viewstate = null;//return viewstate
| string str = Request.Form[ViewStateKey];
| LosFormatter oLosFormatter = new LosFormatter();
|
| if (!str.StartsWith(NewViewState)) //check the viewstate exists
| {
| Conquest.Events.WriteToLog("Failed to locate VIEWSTATE KEY: '" + str
| +"'");
| ViewStatePersistanceFailure(str);//failure
| }
| else//view state does exist
| {
| try
| {
| string tmp = Session[str].ToString();//get the serialize viewstate
| from session
| viewstate = oLosFormatter.Deserialize(tmp);//deserialize into
object
| oLosFormatter = null;
| }
| catch(Exception e)//catch all and handle gracefully
| {
| Conquest.Events.WriteToLog("Failed to Load ViewState from " +
| Conquest.ConfigurationData.ViewStateStorage.ToString() +
| " " +
|
ConquestException.HandleConquestException(e),System.Diagnostics.EventLogEntr
yType.Information);
|
| ViewStatePersistanceFailure(str);//Response.Redirect to homepage
| }
| }
| //return what we got from deserialized viewstate
| return viewstate;
| }
| else //use standard viewstate
| {
| return base.LoadPageStateFromPersistenceMedium();
| }
| }
| catch(Exception e)
| {
| throw new
| ConquestException("PerformancePage.LoadPageStateFromPersistenceMedium
| failed", null, e);
| }
| }
|
|
 
M

MattC

But surely (no Leslie Nielsen jokes please), I would see a sizeable jump in
the aspnet_state process, this is _not_ the case, it is the W3WP process
that is increasing in size, which would indicate that it not related to the
size of the data in the session.

MattC
bruce barker said:
every page hit adds its viewstate to the session, but is only purged on
session unload (which you need to do to support reposts of the same page).
i'd use a database instead of session for storing the viewstate.

note: there is no guarantee that Request.UserHostAddress will be the same
between the render and post. any user going thru a proxy with nat
translation will often get a different ipaddress for each request. its
common for the proxy to have a pool of nat addresses and round robin them.
it also common for all users behind the proxy to have the same nat'd
ipaddress.


-- bruce (sqlwork.com)



| Gents,
|
| I have had to persist the viewstate into the Session object, however
once
| this was done the W3WP process' memory ramped right up and used 300MB of
VM.
|
| Below is my code, can anyone see where a leak may be. I am using the
| aspnet_state to store my session info.
|
| TIA
|
| MattC
|
| private string NewViewState
| {
| get{ return "VIEWSTATE_"; }
| }
|
| private string ViewStateKey
| {
| get{ return "__VIEWSTATE_KEY"; }
| }
|
| private string UniqueViewStateKey
| {
| get{ return NewViewState + Request.UserHostAddress + "_" +
| Guid.NewGuid().ToString(); }
| }
|
| protected override void SavePageStateToPersistenceMedium(object
viewState)
| {
| try
| {
| if(Conquest.ConfigurationData.ViewStateStorage ==
| ViewStateStorage.Session)//if we are implementing our on server
viewstate
| {
| string str = this.UniqueViewStateKey;
| LosFormatter oLosFormatter = new LosFormatter();
| StringWriter oStringWriter = new StringWriter();
| oLosFormatter.Serialize(oStringWriter, viewState);//serialize
viewstate
| into string writer
| oLosFormatter = null;
|
| Session[str] = oStringWriter.ToString(); //store in state server
| oStringWriter = null;
|
| RegisterHiddenField(ViewStateKey, str);
| RegisterHiddenField("__VIEWSTATE", String.Empty);
| }
| else
| {
| base.SavePageStateToPersistenceMedium(viewState);
| }
| }
| catch(Exception e)
| {
| throw new
| ConquestException("PerformancePage.SavePageStateToPersistenceMedium
Failed",
| null, e);
| }
| }
|
| protected override object LoadPageStateFromPersistenceMedium()
| {
| try
| {
| if(Conquest.ConfigurationData.ViewStateStorage ==
| ViewStateStorage.Session)//if we are implementing our on server
viewstate
| {
| object viewstate = null;//return viewstate
| string str = Request.Form[ViewStateKey];
| LosFormatter oLosFormatter = new LosFormatter();
|
| if (!str.StartsWith(NewViewState)) //check the viewstate exists
| {
| Conquest.Events.WriteToLog("Failed to locate VIEWSTATE KEY: '" +
str
| +"'");
| ViewStatePersistanceFailure(str);//failure
| }
| else//view state does exist
| {
| try
| {
| string tmp = Session[str].ToString();//get the serialize
viewstate
| from session
| viewstate = oLosFormatter.Deserialize(tmp);//deserialize into
object
| oLosFormatter = null;
| }
| catch(Exception e)//catch all and handle gracefully
| {
| Conquest.Events.WriteToLog("Failed to Load ViewState from " +
| Conquest.ConfigurationData.ViewStateStorage.ToString() +
| " " +
|
ConquestException.HandleConquestException(e),System.Diagnostics.EventLogEntr
yType.Information);
|
| ViewStatePersistanceFailure(str);//Response.Redirect to homepage
| }
| }
| //return what we got from deserialized viewstate
| return viewstate;
| }
| else //use standard viewstate
| {
| return base.LoadPageStateFromPersistenceMedium();
| }
| }
| catch(Exception e)
| {
| throw new
| ConquestException("PerformancePage.LoadPageStateFromPersistenceMedium
| failed", null, e);
| }
| }
|
|
 
M

MattC

Regarding Request.UserHostAddress, woudl you say DateTime.Now.Ticks is
better?

MattC
bruce barker said:
every page hit adds its viewstate to the session, but is only purged on
session unload (which you need to do to support reposts of the same page).
i'd use a database instead of session for storing the viewstate.

note: there is no guarantee that Request.UserHostAddress will be the same
between the render and post. any user going thru a proxy with nat
translation will often get a different ipaddress for each request. its
common for the proxy to have a pool of nat addresses and round robin them.
it also common for all users behind the proxy to have the same nat'd
ipaddress.


-- bruce (sqlwork.com)



| Gents,
|
| I have had to persist the viewstate into the Session object, however
once
| this was done the W3WP process' memory ramped right up and used 300MB of
VM.
|
| Below is my code, can anyone see where a leak may be. I am using the
| aspnet_state to store my session info.
|
| TIA
|
| MattC
|
| private string NewViewState
| {
| get{ return "VIEWSTATE_"; }
| }
|
| private string ViewStateKey
| {
| get{ return "__VIEWSTATE_KEY"; }
| }
|
| private string UniqueViewStateKey
| {
| get{ return NewViewState + Request.UserHostAddress + "_" +
| Guid.NewGuid().ToString(); }
| }
|
| protected override void SavePageStateToPersistenceMedium(object
viewState)
| {
| try
| {
| if(Conquest.ConfigurationData.ViewStateStorage ==
| ViewStateStorage.Session)//if we are implementing our on server
viewstate
| {
| string str = this.UniqueViewStateKey;
| LosFormatter oLosFormatter = new LosFormatter();
| StringWriter oStringWriter = new StringWriter();
| oLosFormatter.Serialize(oStringWriter, viewState);//serialize
viewstate
| into string writer
| oLosFormatter = null;
|
| Session[str] = oStringWriter.ToString(); //store in state server
| oStringWriter = null;
|
| RegisterHiddenField(ViewStateKey, str);
| RegisterHiddenField("__VIEWSTATE", String.Empty);
| }
| else
| {
| base.SavePageStateToPersistenceMedium(viewState);
| }
| }
| catch(Exception e)
| {
| throw new
| ConquestException("PerformancePage.SavePageStateToPersistenceMedium
Failed",
| null, e);
| }
| }
|
| protected override object LoadPageStateFromPersistenceMedium()
| {
| try
| {
| if(Conquest.ConfigurationData.ViewStateStorage ==
| ViewStateStorage.Session)//if we are implementing our on server
viewstate
| {
| object viewstate = null;//return viewstate
| string str = Request.Form[ViewStateKey];
| LosFormatter oLosFormatter = new LosFormatter();
|
| if (!str.StartsWith(NewViewState)) //check the viewstate exists
| {
| Conquest.Events.WriteToLog("Failed to locate VIEWSTATE KEY: '" +
str
| +"'");
| ViewStatePersistanceFailure(str);//failure
| }
| else//view state does exist
| {
| try
| {
| string tmp = Session[str].ToString();//get the serialize
viewstate
| from session
| viewstate = oLosFormatter.Deserialize(tmp);//deserialize into
object
| oLosFormatter = null;
| }
| catch(Exception e)//catch all and handle gracefully
| {
| Conquest.Events.WriteToLog("Failed to Load ViewState from " +
| Conquest.ConfigurationData.ViewStateStorage.ToString() +
| " " +
|
ConquestException.HandleConquestException(e),System.Diagnostics.EventLogEntr
yType.Information);
|
| ViewStatePersistanceFailure(str);//Response.Redirect to homepage
| }
| }
| //return what we got from deserialized viewstate
| return viewstate;
| }
| else //use standard viewstate
| {
| return base.LoadPageStateFromPersistenceMedium();
| }
| }
| catch(Exception e)
| {
| throw new
| ConquestException("PerformancePage.LoadPageStateFromPersistenceMedium
| failed", null, e);
| }
| }
|
|
 
M

MattC

Would I be better off creating an Application scoped
(Serializer/DeSerializer) that did the work rather than each page creating
its own object?

I'm wondering about the bottle neck on the one Singleton object though?!?

TIA

MattC
bruce barker said:
every page hit adds its viewstate to the session, but is only purged on
session unload (which you need to do to support reposts of the same page).
i'd use a database instead of session for storing the viewstate.

note: there is no guarantee that Request.UserHostAddress will be the same
between the render and post. any user going thru a proxy with nat
translation will often get a different ipaddress for each request. its
common for the proxy to have a pool of nat addresses and round robin them.
it also common for all users behind the proxy to have the same nat'd
ipaddress.


-- bruce (sqlwork.com)



| Gents,
|
| I have had to persist the viewstate into the Session object, however
once
| this was done the W3WP process' memory ramped right up and used 300MB of
VM.
|
| Below is my code, can anyone see where a leak may be. I am using the
| aspnet_state to store my session info.
|
| TIA
|
| MattC
|
| private string NewViewState
| {
| get{ return "VIEWSTATE_"; }
| }
|
| private string ViewStateKey
| {
| get{ return "__VIEWSTATE_KEY"; }
| }
|
| private string UniqueViewStateKey
| {
| get{ return NewViewState + Request.UserHostAddress + "_" +
| Guid.NewGuid().ToString(); }
| }
|
| protected override void SavePageStateToPersistenceMedium(object
viewState)
| {
| try
| {
| if(Conquest.ConfigurationData.ViewStateStorage ==
| ViewStateStorage.Session)//if we are implementing our on server
viewstate
| {
| string str = this.UniqueViewStateKey;
| LosFormatter oLosFormatter = new LosFormatter();
| StringWriter oStringWriter = new StringWriter();
| oLosFormatter.Serialize(oStringWriter, viewState);//serialize
viewstate
| into string writer
| oLosFormatter = null;
|
| Session[str] = oStringWriter.ToString(); //store in state server
| oStringWriter = null;
|
| RegisterHiddenField(ViewStateKey, str);
| RegisterHiddenField("__VIEWSTATE", String.Empty);
| }
| else
| {
| base.SavePageStateToPersistenceMedium(viewState);
| }
| }
| catch(Exception e)
| {
| throw new
| ConquestException("PerformancePage.SavePageStateToPersistenceMedium
Failed",
| null, e);
| }
| }
|
| protected override object LoadPageStateFromPersistenceMedium()
| {
| try
| {
| if(Conquest.ConfigurationData.ViewStateStorage ==
| ViewStateStorage.Session)//if we are implementing our on server
viewstate
| {
| object viewstate = null;//return viewstate
| string str = Request.Form[ViewStateKey];
| LosFormatter oLosFormatter = new LosFormatter();
|
| if (!str.StartsWith(NewViewState)) //check the viewstate exists
| {
| Conquest.Events.WriteToLog("Failed to locate VIEWSTATE KEY: '" +
str
| +"'");
| ViewStatePersistanceFailure(str);//failure
| }
| else//view state does exist
| {
| try
| {
| string tmp = Session[str].ToString();//get the serialize
viewstate
| from session
| viewstate = oLosFormatter.Deserialize(tmp);//deserialize into
object
| oLosFormatter = null;
| }
| catch(Exception e)//catch all and handle gracefully
| {
| Conquest.Events.WriteToLog("Failed to Load ViewState from " +
| Conquest.ConfigurationData.ViewStateStorage.ToString() +
| " " +
|
ConquestException.HandleConquestException(e),System.Diagnostics.EventLogEntr
yType.Information);
|
| ViewStatePersistanceFailure(str);//Response.Redirect to homepage
| }
| }
| //return what we got from deserialized viewstate
| return viewstate;
| }
| else //use standard viewstate
| {
| return base.LoadPageStateFromPersistenceMedium();
| }
| }
| catch(Exception e)
| {
| throw new
| ConquestException("PerformancePage.LoadPageStateFromPersistenceMedium
| failed", null, e);
| }
| }
|
|
 

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

Top