开发者

PHP garbage collector on resource?

take this simple loop

while(1) {开发者_如何转开发
    $data = file_get_contents('randomfiles.img');
    $resource = imagecreatefromstring($data);

    //> do some image operation and other stuff
    //> continue

    //> not calling imagedestroy($resource);

}

As you can see I didn't called imagedestroy, but I am using the same variable ($resource) to store the input of imagecreatefromstring(); (type resource)

When a new loop starts the php GC free the previous $resource automatically?

Let's consider only PHP 5.3+

Thanks


Now if the resource was a regular PHP object the it would be freed.

But since you're dealing with GD library objects, it really depends on the implementation of GD. GD lib might be allocating additional memory againt that image "handle", which it would only free when you call imagedestroy(). (I understand that GD Lib is actually a library implemented in C, with PHP bindings).

If you think that you might forget to call imagedestroy(), it is best you create a wrapper class around your $resource, and call imagedestroy() in the destructor.


From http://de2.php.net/manual/en/language.types.resource.php

Freeing resources

Thanks to the reference-counting system introduced with PHP 4's Zend Engine, a resource with no more references to it is detected automatically, and it is freed by the garbage collector. For this reason, it is rarely necessary to free the memory manually.

Note: Persistent database links are an exception to this rule. They are not destroyed by the garbage collector. See the persistent connections section for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜