php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71093 GD: Very high memory consumption with bundled library 2.1.0
Submitted: 2015-12-11 14:38 UTC Modified: 2015-12-12 10:31 UTC
From: jeep dot gaudreau at gmail dot com Assigned:
Status: Not a bug Package: GD related
PHP Version: 5.6.16 OS: Fedora 23
Private report: No CVE-ID: None
 [2015-12-11 14:38 UTC] jeep dot gaudreau at gmail dot com
Description:
------------
I found out that the GD library bundled with PHP 5.6.16 (version 2.1.0) is using a lot of memory when calling functions "imagecreatefromjpeg" or "imagecreatefromstring" on JPEG files.

A JPEG file of about 4MB can generate up to 300MB of memory while it take not more than a few KB when using the OS package of GD version 2.1.1.

I can also reproduce the bug on RHEL6.5

Maybe the solution would only be to upgrade the bundled PHP GD library to 2.1.1.

Here's the GD info when memory consumption is very high :
GD Support 	enabled
GD Version 	bundled (2.1.0 compatible)
FreeType Support 	enabled
FreeType Linkage 	with freetype
FreeType Version 	2.6.0
T1Lib Support 	enabled
GIF Read Support 	enabled
GIF Create Support 	enabled
JPEG Support 	enabled
libJPEG Version 	6b
PNG Support 	enabled
libPNG Version 	1.6.19
WBMP Support 	enabled
XPM Support 	enabled
libXpm Version 	30411
XBM Support 	enabled
WebP Support 	enabled 
GD Support 	enabled

Here's the GD info when memory consumption is low :
GD headers Version 	2.1.1
GD library Version 	2.1.1
FreeType Support 	enabled
FreeType Linkage 	with freetype
FreeType Version 	2.6.0
T1Lib Support 	enabled
GIF Read Support 	enabled
GIF Create Support 	enabled
JPEG Support 	enabled
libJPEG Version 	6b
PNG Support 	enabled
libPNG Version 	1.6.19
WBMP Support 	enabled
XPM Support 	enabled
libXpm Version 	30411
XBM Support 	enabled
WebP Support 	enabled 


Test script:
---------------
Very easy to reproduce:
* Create the following PHP script:
<?php

$path = 'img.jpg';
//$image = imagecreatefromjpeg($path);
$image = imagecreatefromstring(file_get_contents($path));
var_dump(memory_get_usage());

* Execute with the following JPEG image: https://drive.google.com/open?id=0B0H8CbGn160eMHg3cGZrSmtzRTA

You should have very high memory usage with bundled library 2.1.0
You should have very low memory usage with OS package GD version 2.1.1



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-12 03:24 UTC] danack@php.net
The linked image has dimensions of 16,313 x 3,621. With 4 bytes per pixel, this comes to 236,277,492 bytes of image data.

This is much bigger than the jpeg file size, because the JPEG file holds compressed data, whereas GD has to decompress the data to be able to use it.

I'm not sure what is happening to make it look like "not more than a few KB" of memory is being used in other scenarios, but it sounds like that something is misleading you, as it is normal for large amounts of memory to be used, when a large image is being converted.
 [2015-12-12 10:31 UTC] pajoye@php.net
-Status: Open +Status: Not a bug
 [2015-12-12 10:31 UTC] pajoye@php.net
Both bundled or system library will use the same amount of memory  (as explained by Danak).


The bundled library uses the php memory management system while the system library does not.

It explains why get_memory_usage will show different values. Use a ps or other to see how much memory is used by the php process and you will see the actual usage (php and all libs).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 04:01:28 2024 UTC