Fix PHP Memory Limit Issues in Cron Jobs
PHPThe Problem
PHP's default 128MB memory limit is often too low for batch scripts processing large datasets. The script crashes with a fatal error and cron doesn't report it.
The Solution
Set memory_limit in your script or php.ini, and add CronRabbit monitoring to detect when memory issues still cause failures.
Setting Memory Limits
Use ini_set('memory_limit', '512M') at the top of your script, or pass -d memory_limit=512M on the command line.
Monitoring for Memory Issues
Even with higher limits, data growth can eventually exceed them. CronRabbit catches these failures because the success ping never arrives.
Code Examples
Memory-safe cron script
PHP<?php
ini_set('memory_limit', '512M');
ini_set('max_execution_time', 0);
$PING = "https://ping.cronrabbit.com/your-id";
file_get_contents("$PING/start");
try {
processLargeDataset();
file_get_contents($PING);
} catch (Throwable $e) {
file_get_contents("$PING/fail");
error_log($e->getMessage());
exit(1);
}