Fixed issue of last memory segment not getting cleared