c# - How to destory instance of class when finished Action -
i use mvvm in project . first @ see edit action:
[httpget] public async virtual task<actionresult> edit(int code) { var attributemodel = await _attributeservice.getasync(code); editattributeviewmodel attributeviewmodel = mapper.map(attributemodel, new editattributeviewmodel()); return view(attributeviewmodel); }
in viewmodel count instance :
public class editattributeviewmodel { private static int counter = 0; public editattributeviewmodel() { interlocked.increment(ref counter); } ~editattributeviewmodel() { interlocked.decrement(ref counter); } }
when finished edit
action , change controller , comeback again edit
action , when see counter
increase , while moving between pages.i not want use memory, override
dispose
method in controller :
protected override void dispose(bool disposing) { base.dispose(disposing); }
but doest chnage , counter increase .
how can clear instance when finshed actionmethod
unlike in c++, can't free memory on demand. dispose pattern there release unmanaged resources (file handlers, connections, or pictures unmanaged, read: reserved outside of .net memory management uses intptr
).
all can is, set references null , call .dispose()
on disposable types in class, wait until garbage collection pick them up.
depending no how application utilize memory, may sooner or later. if application instantiates , unreferencing objects may happen sooner, if application doesn't may take longer.
you shouldn't depend on finalizer (that looks deconstructor in c++) called or when. gc calls it, when it's cleaning up. if haven't suppressed (which typically inside dispose method).
Comments
Post a Comment