Move memory usage check inside the try...finally