2

I was thinking about a previous client's work which was well in to development when I went in. I did a print_r($GLOBALS);, saved the file and measured it to be...I think two megabytes? It's been a few months though it had me thinking about the possible performance benefits of using unset() a bit more often. At what general range would extended use of unset() start to noticeably improve performance?

John
  • 1
  • 13
  • 98
  • 177
  • I guess it depends on the size of the variable. 2000000bytes compared to 200 bytes. – Pedro Lobito May 06 '16 at 15:50
  • This isn't an issue where unset is going to be of much help; if $GLOBALS contains that much data, then too much is being defined in the global scope in the first place – Mark Baker May 06 '16 at 15:50
  • PHP isn't C - you're not directly manipulating system memory so it all gets a bit *wiggly fingers* : http://stackoverflow.com/questions/584960/whats-better-at-freeing-memory-with-php-unset-or-var-null – CD001 May 06 '16 at 15:55
  • 1
    @CD001 I don't think the question is about **how** to free the memory, but **whether** to free the memory. – Barmar May 06 '16 at 16:12
  • It's probably not very important, unless your server is having performance problems. If the large variable isn't being used, it will be paged out and doesn't impact performance much. If it is being used, then you can't unset it. – Barmar May 06 '16 at 16:15
  • IMHO: If you may unset a global variable, it shouldn't be global. – Paul Spiegel May 06 '16 at 18:41
  • @Barmar - the question was about performance though. With `unset()` you're telling PHP to free the memory, which it will do as and when it thinks best (not necessarily immediately) - and being an interpreted language there's an additional cost in CPU cycles, moreso than with a compiled program anyway. So, theoretically, unsetting a large variable during execution *could* actually hinder performance - which is what I was getting at with *wiggly fingers* - it's something that would need testing over multiple iterations. That's my understanding anyway - I could be wrong. – CD001 May 11 '16 at 08:26

0 Answers0