Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP can't use 300MB of RAM

Tags:

php

mod-php

I'm trying to increase the allowed memory for certain PHP script. No matter what I do, for instance this:

ini_set('memory_limit', '512M');

... the script always runs out of memory at around 300MB:

Fatal error: Out of memory (allocated 25165824) (tried to allocate 343810589 bytes) in \\Foo\phpQuery\phpQuery.php on line 255

I've verified by several means that memory_limit is actually changed. The issue seems to be that PHP can't physically allocate a total of 300 MB of memory (25165824 bytes + 343810589 bytes = 352 MB).

I've tried both PHP/5.3.0 and PHP/5.3.9 in two different Windows-based computers with the following specs:

  • Windows XP / Windows Server 2003 (both computers are 32-bit boxes with 1GB or RAM)
  • Official PHP 32-bit VC9 binaries
  • Running as Apache 2.2 module (third-party 32-bit VC9 binaries)

I understand that using half of the physical RAM will force swapping and slow things down as hell but I just need to make sure the script actually works so it can be deployed to the live server. I've also tried larger values (which procuded the same error) and smaller values (with either made my script hit the limit or made Apache crash).

What can be the source of this apparently hard-coded memory limit?

Update #1: I've done further testing with the Windows Server 2003 box (which is actually a VMWare virtual machine). I've increased the "physical" RAM to 2 GB and I've verified that the paging file is allowed to grow up to 1152 MB. Task manager shows that current transaction load is 886 MB and there're 1,5 GB of free physical memory. However, I'm getting the same error with exactly the same figures.

Update #2: As I said, the memory_limit directive is fine. It shows up in both ini_get() and phpinfo(). The error message you'd get is slightly different from mine; mine indicates a PHP crash. Please compare:

Out of memory (allocated 25165824) (tried to allocate 343810589 bytes)
Allowed memory size of 25165824 bytes exhausted (tried to allocate 343810589 bytes)

I'll try to compose a script to reproduce the issue and report back.

like image 282
Álvaro González Avatar asked Oct 09 '22 20:10

Álvaro González


1 Answers

An OOM exception is different to the memory limit warninigs.

This means PHP can't actually allocate the memory because insufficient resources are available within your operating system.

You'll need to check the system has sufficient memory/paging available to support this.

like image 148
Paul Bain Avatar answered Oct 12 '22 11:10

Paul Bain