Re: When does PHP free-up memory allocated to it?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, May 21, 2007 4:23 am, Richard Davey wrote:
> Quick question - does anyone know when PHP actually frees allocated
> memory during the process of a script?

When it can be sure that a variable is no longer needed.

Specifically, when the .refcount of a zval turns to 0.

> I'm using 5.2.2. For example I
> see the following results when profiling:
>
> Memory: 256 KB
> Load a 1024x768 JPEG (via GD)
> Memory: 4 MB
> Create thumbnail (via GD)
> Memory: 5.2 MB
> Destroy original image resource (imagedestroy)

Try unset($image) as well, so PHP "knows" you no longer need access to
that variable that was holding all that stuff...

> Memory: 5.2 MB
>
> I would have expected (or rather, hoped) that the memory use would
> have dropped down quite dramatically after destroying the image
> resource that was using up nearly 4MB of memory, but it didn't seem to
> change.

How are you measuring this?

What version of PHP?

The Memory Manager changed over the course of time.

I'm also not at all sure that you *can* measure it as precisely as you
are trying to do.

> Is this a limitation of the memory_get_usage(true) function, or does
> imagedestroy not actually do what it implies?

Almost for sure imagedestroy() does what it should -- and GD frees up
the internal memory structures for that image.

PHP still has a variable sitting around pointing to that resource,
however, and you may need to notify PHP through unset() that you are
"done" with it.

That said:
PHP doesn't really allocate that 5.2M of RAM.
GD does.
PHP may not be able to tell you accurately about what GD is doing
internally -- but it's probably not *really* losing that 5.2M or you'd
hear an awful lot more screaming about it from all quarters. :-)

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux