PHP memory allocation problem

If PHP fails to allocate memory it returns one of the following errors:

  • Allowed memory size of %ld bytes exhausted at %s:%d (tried to allocate %lu bytes)

OR

  • Out of memory (allocated %ld) at %s:%d (tried to allocate %lu bytes)

No 1 occurs when PHP memory limit is reached and it is the limit which is controlled by memory_limit setting at php.ini.
No 2 comes from the operating system, php limit is not reached and changing php.ini will not help whatever people are advising about it.

Just have a look at the source of zend_alloc.c if you have doubts …

        if (segment_size < true_size ||
            heap->real_size + segment_size > heap->limit) {
            /* Memory limit overflow */
#if ZEND_MM_CACHE
            zend_mm_free_cache(heap);
#endif
            HANDLE_UNBLOCK_INTERRUPTIONS();
#if ZEND_DEBUG
            zend_mm_safe_error(heap,
"Allowed memory size of %ld bytes exhausted at %s:%d (tried to allocate %lu bytes)",
            heap->limit, __zend_filename, __zend_lineno, size);
#else
            zend_mm_safe_error(heap,
"Allowed memory size of %ld bytes exhausted (tried to allocate %lu bytes)",
            heap->limit, size);
#endif
        }

        segment = (zend_mm_segment *) ZEND_MM_STORAGE_ALLOC(segment_size);

        if (!segment) {
            /* Storage manager cannot allocate memory */
#if ZEND_MM_CACHE
            zend_mm_free_cache(heap);
#endif
            HANDLE_UNBLOCK_INTERRUPTIONS();
out_of_memory:
#if ZEND_DEBUG
            zend_mm_safe_error(heap,
            "Out of memory (allocated %ld) at %s:%d (tried to allocate %lu bytes)",
            heap->real_size, __zend_filename, __zend_lineno, size);
#else
            zend_mm_safe_error(heap,
            "Out of memory (allocated %ld) (tried to allocate %lu bytes)",
            heap->real_size, size);
#endif
            return NULL;
        }

2 комментария к “PHP memory allocation problem”

Оставьте комментарий