Fixing out of memory exceptions