c# - How did I solve this memory leak-ish issue? -


i have .net 2.0 app heavy processing, uses lots of memory, , more. takes tasks web service, it's job, returns results, , repeats infinity.

main code structure can simplified , illustrated this

while (true)  {      task t=serviceaccess.gettask();      if (t.tasktype == 1)      {           brutalmemoryconsumerprocessor b=new brutalmemoryconsumerprocessor();           b.dotask(t);      }       else if (t.tasktype == 2)      {           heavycpuconsumerprocessor h=new heavycpuconsumerprocessor();           h.dotask(t);      } } 

fortunatelly me, died after few cycles outofmemoryexception somewhere in inner code. due fact both objects have been calibrated use of ram there (for x86 app), , having old object instance while new 1 being created sure way process snuff out.

ok, tried few tricks first. namely:

gc.collect(); 

at beginning of loop, after while (true).

no luck.

next, remembered old vb6 com based days, , tried b = null; , h = null; inside scope of if statement blocks.

no luck again.

combined, no dice.

fired memory profiler. didn't use time needed go through several pages of 'program files' find it. yourkit profiler. nice toy...

anyway, after meddling it, told me references b , h staying inside local 'stack scope'. did monkey do, , rewrote above as:

while (true)  {      task t=serviceaccess.gettask();      if (t.tasktype == 1)      {           dotasktype1(t);      }       else if (t.tasktype == 2)      {           dotasktype2(t);           heavycpuconsumerprocessor h=new heavycpuconsumerprocessor();           h.dotask(t);      } }  private void dotask1(task t) {     brutalmemoryconsumerprocessor b=new brutalmemoryconsumerprocessor();     b.dotask(t); }  private void dotask2(task t) {     heavycpuconsumerprocessor b=new heavycpuconsumerprocessor();     b.dotask(t); } 

o my. solved memory leak issue.

so, moving object creation inside function surely go out of scope, despite fact if code block scope in itself, object got freed , deleted.

why?

you running unoptimized (in debug mode, or debugger attached). in case jit extends lifetime of locals end of method debugging.

with separate methods, locals exist in separate stack frame goes away quickly.

try in release mode without debugger.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -