Improved handling of memory limit