Memory Limit Exhausted Regardless of Allowance

I have set the “memory_limit” in the php.ini file to 25M, and have a file that gives me this error:

Fatal error:  Allowed memory size of 26214400 bytes exhausted (tried to allocate 19218544 bytes) in /link/to/directory/file.php on line 142

Note that the memory size is actually BIGGER than what it tried to allocate. I don’t know how that’s possible. This is line 142 to which it refers:

list($width, $height) = getimagesize("files/".$file_num.".swf");

I did a memory_get_usage() before line 142, and it was 138356 so it isn’t anything else that’s eating up some memory. Not sure what my next step is to fix this. Thoughts?

How large is the file? Perhaps PHP is trying to open the file and running out of memory.

The file is about 8MB.

What I’m most confused about is how the amount of memory it tries to allocate is LESS than how much capacity it actually has.

You’re using getimagesize on a .swf ?

Yes I am. The specific code is in the first post.

The documentation states the function get the size of an image, an .swf is not an image. Are you sure this functionality exists?

Yep, I’ve used the function for that purpose many times in the past. Current problem is just because I’m on a new server.

getimagesize can read the dimensions of a SWF file as long as the canvas isn’t empty.

What happens if you increase the memory_limit even more?

I don’t think it’s wise to rely on memory_get_usage() in this manner as the compiled bytecode may have many more steps between your last memory_get_usage() and when it actually tries to read the dimensions of the file. Perhaps it loads the entire file into memory (+/- 8Mb) and then unzips it and tries to read the canvas (even more memory).

An increase in memory does actually solve the problem. I have it up to 30M right now. It doesn’t seem there’s an easier way to get the dimensions of a swf, so I guess memory increases will have to do. I just found it curious that the error message PHP gave wasn’t really accurate.

Thanks for everyone’s help!

I love this stuff, I’m always learning!

The source code confirms this too, cheers Immerse. :cool:


static void php_getimagesize_from_stream(php_stream *stream, zval **info, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
{
    char *temp;
    int itype = 0;
    struct gfxinfo *result = NULL;

    if (!stream) {
        RETURN_FALSE;
    }

    itype = php_getimagetype(stream, NULL TSRMLS_CC);
    switch( itype) {
        case IMAGE_FILETYPE_GIF:
            result = php_handle_gif(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_JPEG:
            if (info) {
                result = php_handle_jpeg(stream, *info TSRMLS_CC);
            } else {
                result = php_handle_jpeg(stream, NULL TSRMLS_CC);
            }
            break;
        case IMAGE_FILETYPE_PNG:
            result = php_handle_png(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_SWF:
            result = php_handle_swf(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_SWC:
#if HAVE_ZLIB && !defined(COMPILE_DL_ZLIB)
            result = php_handle_swc(stream TSRMLS_CC);
#else
            php_error_docref(NULL TSRMLS_CC, E_NOTICE, "The image is a compressed SWF file, but you do not have a static version of the zlib extension enabled");
#endif
            break;
        case IMAGE_FILETYPE_PSD:
            result = php_handle_psd(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_BMP:
            result = php_handle_bmp(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_TIFF_II:
            result = php_handle_tiff(stream, NULL, 0 TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_TIFF_MM:
            result = php_handle_tiff(stream, NULL, 1 TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_JPC:
            result = php_handle_jpc(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_JP2:
            result = php_handle_jp2(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_IFF:
            result = php_handle_iff(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_WBMP:
            result = php_handle_wbmp(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_XBM:
            result = php_handle_xbm(stream TSRMLS_CC);
            break;
        case IMAGE_FILETYPE_ICO:
            result = php_handle_ico(stream TSRMLS_CC);
            break;
        default:
        case IMAGE_FILETYPE_UNKNOWN:
            break;
    }

    if (result) {
        array_init(return_value);
        add_index_long(return_value, 0, result->width);
        add_index_long(return_value, 1, result->height);
        add_index_long(return_value, 2, itype);
        spprintf(&temp, 0, "width=\\"%d\\" height=\\"%d\\"", result->width, result->height);
        add_index_string(return_value, 3, temp, 0);

        if (result->bits != 0) {
            add_assoc_long(return_value, "bits", result->bits);
        }
        if (result->channels != 0) {
            add_assoc_long(return_value, "channels", result->channels);
        }
        add_assoc_string(return_value, "mime", (char*)php_image_type_to_mime_type(itype), 1);
        efree(result);
    } else {
        RETURN_FALSE;
    }
}
/* }}} */