1

I have a php script that keeps throwing an error on the following line

base64_encode(serialize(array($data, $context)))

Here is the error

Out of memory (allocated 471859200) (tried to allocate 234607507 bytes)

I have added the following two line before calling the base64_encode() method

set_time_limit(0);
ini_set('memory_limit', '-1');

As you can see, I am giving the script all the available physical memory (16GB)

What could be causing this error?

Junior
  • 9,853
  • 16
  • 76
  • 173
  • 2
    Are you using 32-bit PHP? There's a limit on how much memory the interpreter is physically possible to use. – Álvaro González Jan 09 '19 at 14:56
  • Your code is probably causing the issue, but I can't see any. – Dharman Jan 09 '19 at 14:56
  • 1
    It seems that `$data` and/or `$context` are very large – yunzen Jan 09 '19 at 14:56
  • @HerrSerker yes they are. but why would my script runs out of memory? – Junior Jan 09 '19 at 14:57
  • Try checking phpinfo() if your memory limit is being applied – Elias Soares Jan 09 '19 at 15:02
  • 1
    @ÁlvaroGonzález I think I am using 32 bit. I checked the phpinfo() output (PROCESSOR_ARCHITECTURE x86 ) – Junior Jan 09 '19 at 15:03
  • The error message says that you have 450MB allocated and trying to allocate more 223MB (not ever close to 32-bit memory limit). So or your memory limit isn't `-1` or your physical memory is over. – Elias Soares Jan 09 '19 at 15:03
  • I just added `ini_set('memory_limit', '2000M');` but that gave me the following error `Out of memory (allocated 473956352) (tried to allocate 234607507 bytes)` – Junior Jan 09 '19 at 15:11
  • Set memory limit to 128M ( or another reasonable number) and ensure that your available RAM is at least that amount. Then when you get a message that PHP exceeded memory limit redesign your code in such a way that you do not hit the limit anymore. Giving PHP all your RAM is a very bad idea. – Dharman Jan 09 '19 at 15:12
  • @MikeA please tell me whats the memory limit on phpinfo()! – Elias Soares Jan 09 '19 at 16:24
  • Just how gigantic is this data you're trying to serialize? – tadman Jan 09 '19 at 19:00

1 Answers1

0

PHP produces two memory-related error messages:

  • When memory_limit is exceeded, e.g.:

    Allowed memory size of %zu bytes exhausted (tried to allocate %zu bytes)

  • When PHP is unable to get more memory from the OS:

    Out of memory (allocated %zu) (tried to allocate %zu bytes)

… with minor wording variations depending on the exact context (see PHP source code at Zend\zend_alloc.c for details).

Using a simile, you can exhaust your disk quota or the disk can fill up.

The second situation is basically a crash and is relatively easy to find if you run memory-intensive tasks in a 32-bit PHP process. There's no fixed rule but when your needs get close to 1GB bad things happen. If you determine that you're running 32-bit PHP (the OS architecture doesn't matter), and even if you don't, you may want to consider a rewrite.

I have no idea about your use case so these are only vague tips:

  • I presume you base64_encode() your data to submit it elsewhere. Perhaps you don't need to hold the entire data in memory and you can decode it in chunks and send it as you go or store it in a file.

  • You can replace serialize() with an alternative implementation that operates in chunks or switch to another format.

Álvaro González
  • 128,942
  • 37
  • 233
  • 325