Staying within limits.
WHERE MEMORY GOES
Loading large result sets into arrays Processing large files in one operation Image manipulation, which is memory-intensive Frameworks and libraries loaded on every request Accumulating data in a loop
PRACTICAL TECHNIQUES
Process large result sets in chunks rather than loading everything Stream file operations rather than reading whole files into memory Unset large variables when finished with them Use generators where your language supports them Limit image dimensions before processing
MEASURING
Report memory usage at points in your code to find where it climbs. Guessing is usually wrong.
IMAGE PROCESSING
A large image can consume many times its file size in memory when decoded. Resize on upload rather than on display, and set limits on accepted dimensions.
BATCH OPERATIONS
Imports and exports are the usual cause of memory exhaustion. Process in batches with a limit per run, and resume.
RAISING THE LIMIT
Possible in cPanel, and a last resort. If your code needs a very high limit for an ordinary operation, the code is the problem.