![]() |
![]() |
|
Current Projects: PHP on XP Guide — NFO Viewer — Easy Reflections — Photon Storm — HotWire — FileGlider
Monday, May 21. 2007Interesting memory use with GD imagesTrackbacks
Trackback specific URI for this entry
No Trackbacks
Comments
Display comments as
(Linear | Threaded)
If, in the procedural example, you set the jpg and output variables to be empty after running imagedestroy you will squeeze an additional 12-13% out of the memory.
#1
on
2007-05-21 23:50
My test shows, that unset() is faster and uses less memory than imagedestroy()
set things to null is faster than unset() but left more memory than unset()
btw your spam captcha is extremely hard for me....
I tried to verify your claims, I used your procedural sample and my own simple class, here's the result:
procedural
58,684
6,189,568
271,672
object var
52,788
6,183,944
266,088
object array
53,112
6,184,336
266,532
PHP 5.2.2 running on Fedora
Looks like something is wrong with your code, dude.
Post full code sample if still think you are right.
#3
on
2007-05-22 10:26
Here's the result for __autoload
48,712
6,185,780
267,904
btw. these values are for:
init, jpg created, thumb created+jpg destroyed
#4
on
2007-05-22 11:13
Hi Davey,
Two little comments about this post. The first one is about the (to be proved) difference when you use GD functions in an object or only using the native functions. From a gd point of view? There is absolutely no difference. If something differs, it may apply to any other kind of resources, that's a engine issue.
As Antych already stated, your claims seem to be wrong. I can't reproduce the difference. I suspect that you have references being still in used somewhere in your object tree. That will prevent the engine to destroy the object/props and thus prevent gdimagedestroy to be called. That's the only explanation I can think about right now :)
Actually I believe the difference to be platform related. All testing was on a Windows server. I'll test on FreeBSD later today and see what happens.
"Actually I believe the difference to be platform related."
I would be surprised if it has anything to do with the platform, sapi or how you use a resource (GD or whatever else). As I said earlier, how GD destroys its resource is independent from the context or platform.
What may change is when the engine calls a resource destructor (it can be a mysql connection or any kind of other resources) may cause this difference.
Ok I've updated the article to include the actual procedural code used + I've tested it on FreeBSD. I'll add the class version shortly.
Richard, try creating a very simple class just for the purpose of the test. If you get the same results, just post the code for others to evaluate.
#7
on
2007-05-22 14:39
Hm you are using memory_get_usage(true).
I don't think this is very helpful.
I think it gives allocated system memory, not the usage.
I'm not an expert on this, but I suspect it's up to garbage collector to free it and it doesn't necessary have to happen when you invoke destroy or unset.
This is a result of using () vs (true) on my devbox:
Array
(
[init] => 262,144
[load jpeg] => 6,291,456
[create 200x200] => 6,553,600
[resample] => 6,553,600
[destroy] => 6,553,600
)
Array
(
[init] => 56,672
[load jpeg] => 6,187,696
[create 200x200] => 6,399,996
[resample] => 6,400,272
[destroy] => 270,036
)
#8
on
2007-05-22 15:29
Right, if you like to see what GD actually uses and what it has freed, use memory_get_usage(), not (true). GD, when used with the bundled library, relies on the PHP memory manager.
When GD has freed something, it may not be freed for the system but only for php. Other parts of your script will then use these blocks instead of allocating more blocks from the system.
That also explains the difference from one platform to another (see one of the README in the source releases, they contain good explanation about how it works, 5.2+).
That also confirms what I said earlier, the difference has nothing to do with GD or any other resource (mysql, xml or whatever else).
Your class sample gives me identical values as the procedural one when using (true). IMHO it's just allocating some memory in chunks as needed and releases it at its own discretion. Looks like garbage collector is particularly lazy on my machine :)
#10
on
2007-05-22 16:15
"IMHO it's just allocating some memory in chunks as needed and releases it at its own discretion"
It is exactly that :-)
Hello,
probably there's a thing that everybody misread. The problem in this scenario isn't the weird memory usage of GD.
It's your code that causes the huge memory load.
look at this:
$jpg = imagecreatefromjpeg($picture);
$mem['load jpeg'] = number_format(memory_get_usage(true), 0);
$this->img = $jpg;
what happens here is that you set $jpg, and after that pass his data to $this->img... we have this data now 2 times, in $jpg and in $this->img at the moment you call memory_get_usage(true);
so...that the memory load doubles isn't that weird if you write the code like this.
my advise: when working with GD, allways unset your vars when you're done with them.
If you write your code like this:
$this->img = imagecreatefromjpeg($picture);
$mem['load jpeg'] = number_format(memory_get_usage(true), 0);
You will not have the memory problem.
(when you're done, just unset $this->img)
p.s. another thing i noticed too is that unset() makes more memory available than imagedestroy()
Thanks, I was having the same memory problem. This article and comments helped me shortout the prob.
"what happens here is that you set $jpg, and after that pass his data to $this->img... we have this data now 2 times, in $jpg and in $this->img at the moment you call memory_get_usage(true);"
Sorry but that isn't what happens in PHP 5, it creates a reference to the resource handle, not the image data itself.
You can see this if you modify $jpg, the content of $this->img will also have updated. The following code should confirm for you. If what you said is true then the image returned from the load() call should not be effected a all by the draw() call (as you will see though, it is)
The author does not allow comments to this entry
|
My AS3 Blog
Photon Storm Great PHP links
C7Y PHP Podcast CorePHP is hosted by |