Define memeory cleanup function.