2

I've read in PHP Advanced and Object Oriented Programming by Larry Ullman that it is good programming practice to delete object immediately after use but reason is given nowhere.

I am a student web developer and objects in web applications are deleted automatically as soon as the script finishes. So, there seems no good reason to delete them manually.

So, my question is what are the good reasons to delete unnecessary objects after use other that blindly following a convention?

  • where did you read this? – gnat Jan 20 '14 at 18:27
  • 2
    Depends heavily on language. Most languages with GC don't even have option to delete an object. – Euphoric Jan 20 '14 at 18:28
  • @gnat in books. – user88873 Jan 20 '14 at 18:28
  • 1
    in which books? name at least one – gnat Jan 20 '14 at 18:29
  • @gnat PHP Advanced and Object Oriented Programming by Larry Ullman – user88873 Jan 20 '14 at 18:34
1

No, you do not need to delete an object after its use in PHP. Let PHP's garbage collection take care of it for you.

There may be some obscure performance reasons to unset an object after you are done with it in very specific situations. But such cases are not everyday events, and they will be clear when you see them.

  • 1
    Database connections / result sets? Very Large strings that you won't touch again? – user40980 Jan 20 '14 at 21:01
  • They still get freed when they go out of scope and/or the script ends. There's no necessity to manually delete them unless there's some specific problem you're encountering, like building memory usage. I've had to do that a couple times when reading very large xml files, for example. – GrandmasterB Jan 20 '14 at 21:09
  • I mention the database because I've seen code that has left the database result set in scope for much longer than needed... which meant that the cursor was open on the database which was then hitting resource constraints on another machine (this was back in the perl.cgi days - different, but similar). – user40980 Jan 20 '14 at 21:11
  • @GrandmasterB Can you give me an example of obscure performance reasons. – user88873 Jan 21 '14 at 9:17
  • @MichaelT I saw that sort of thing as well back in my powerbuilder days. The PHP data classes I've worked with all pull the full results back in one shot and close the cursor, so that you dont have one hanging around while the php script executes. – GrandmasterB Jan 21 '14 at 17:05
1

PHP's GC was hit and miss prior to 5.3 with many arrays and objects becoming unfreeable until the script ended. Because of this, PHP development basically took on an unspoken “who needs GC anyways” attitude because vars were only guaranteed to be freed when the script ended.

Two things are happening which change this:

First, PHP's GC has advanced to where its only unreliable with arrays that contain copies of other arrays. PHP's GC will be totally functional and reliable once this issue is fixed .

Secondly, PHP's pthread extension is coming along pretty well. It's very likely that PHP will have full thread support within the next couple years, making long running scripts as usefull as many Java appliations.

These two things are likely to change many standards in the way PHP is used. For example, your already starting to see serious efforts to create PHP based network services (such as Ratchet) now that the GC is reliable under most circumstances.

So to make a long story short - no, your currently not required to unset any form of resources in PHP because they will all be freed when the script ends. However, unsetting objects and other resources will make sure that each unneeded resource is freed before the script ends, potentially freeing up highly contented resources and thus improve performance.

Moreover, the practice of unsetting objects and resources is likely to become the norm in the near future, and likely an absolute requirement in many cases.

Note that many PHP objects/resources don't fall out of scope until the script ends. This means that unset() is the only way to ensure that their ref count is set to 0 in order to become a candidate for freeing by the GC (before the script ends, that is.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy