c# - Session.Remove("session_variable"); not working? -
hi in following stuff trying remove session after completing tasks. session.remove() not working here.
if (session["casenumber"] != "" || session["casenumber"] != null) { casenumber = session["casenumber"].tostring(); guid = session["guid"].tostring(); _duration = bal.viewserviceactivity(guid); case1 = bal.viewcasedetail(casenumber); dt = bal.getrelatednotes(guid); session.remove("casenumber"); session.remove("guid"); } else { casenumber = session["searchcase"].tostring(); guid = session["caseguid"].tostring(); _duration = bal.viewserviceactivity(guid); case1 = bal.viewcasedetail(casenumber); dt = bal.getrelatednotes(guid); session.remove("searchcase"); session.remove("caseguid"); }
there's nothing wrong portion of code shown above. understanding required how works.
session.remove(key)
removes memory used key in session dictionary. when using second approach of : session["searchcase"]=null;
, key still exist in session although value null.
try setting session["key_name"]
null
. example:
if (session["casenumber"] != "" || session["casenumber"] != null) { casenumber = session["casenumber"].tostring(); guid = session["guid"].tostring(); _duration = bal.viewserviceactivity(guid); case1 = bal.viewcasedetail(casenumber); dt = bal.getrelatednotes(guid); session["casenumber"] = null; // set null session["guid"] = null; // set null }
session.remove()
doesn't destroys object bypassing basic .net rules of object referencing , memory management. need understand here session.remove()
suggests: removes reference object internal collection. nothing more expect.
if need destroyed, need implement idisposable
, , furthermore, may instruct garbage collector collect if in hurry using gc.collect()
.
check question: proper use of idisposable interface
microsoft forum confirms same.
it seems more optimization or called standard way of implementation. when remove object 'marked removal' not removed...unless space required. it's how garbage collection works. because object goes out of scope doesn't mean it's destructor gets called , memory freed immediately. may gc
can remove object later.
still, last check, this :
make sure not viewing cached version of page, or don't add item session object again somewhere in application.
Comments
Post a Comment