Question

I'm using the twitter-async library to do some oauth calls to twitter but ultimately, a lot of memory is being used up. I tried running the xdebug execution trace and this is where I feel the memory is getting leaked.

json_decode() is using 47077232 bytes. Okay. This is fine.

But gettype() ? Why should this too be using similar amount of memory?

How could this be prevented?

The script runs in a loop and the memory usage keeps going up. I'm using gc_collect_cycles() at number of places and it is showing improvement. But this one, I can't understand why should this happen.

  724.1772   47077232 +2118720             -> json_decode() /var/www/html/includes/classes/twitter/EpiTwitter.php:230
  724.1926   49157104 +2079872             -> gettype() /var/www/html/includes/classes/twitter/EpiTwitter.php:232
  724.1927   49157104       +0             -> property_exists() /var/www/html/includes/classes/twitter/EpiTwitter.php:240
  724.1927   49153520    -3584     -> EpiTwitterJson->__destruct() /var/www/html/includes/classes/twitter/EpiTwitter.php:0
  724.1949   46714808 -2438712     -> in_array() /var/www/html/cron.php:156
Was it helpful?

Solution

From the documentation:

Never use gettype() to test for a certain type, since the returned string may be subject to change in a future version. In addition, it is slow too, as it involves string comparison.

Instead, use the is_* functions.

e.g. use is_array, is_string etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top