Check $wgProfiling. This lets long running scripts disable profiling to avoid OOM...