title: Short PHP using lots of memory - cURL + XMLreader
body: <p>I have a php script that runs a simple cURL request and parses through it for some data. Four of these run per web user on my site, and in ps I see these processes using 12M of RAM. I have basically stripped everything I could out of this script as far as loops and decision making go, so I figure the memory is leaking in cURL but I'm not really sure what to do.</p>
  
  <p>Am I missing a key opt for CURL? Is there something I can do to make this use less memory or processor time?</p>
  
  <pre><code>&lt;?php
  //Caching
  $cachefile = '../../playlistcache/curplayingBOSUN_cache.html';
  $cachetime = 18;
  // Serve from the cache if it is younger than $cachetime
  if (file_exists($cachefile) &amp;&amp; time() - $cachetime &lt; filemtime($cachefile)) {
  include($cachefile);
  echo "&lt;!-- Cached copy, generated ".date('H:i', filemtime($cachefile))." --&gt;\n";
  exit;
  }
  ob_start(); // Start the output buffer
  
  
  // URL to your Shoutcast server, including port
  // don't put in the http:// part!  do it like you see here
  // also, reverse DNS lookup needs to be on, or this probably won't work
  $server = "SERVERADDRESS";
  
  // Admin password for your Shoutcast server
  $password = "SERVERPASSWORD";
  
  
  $mysession = curl_init();
  curl_setopt($mysession, CURLOPT_URL, "http://$server/admin.cgi?sid=1&amp;mode=viewxml");
  curl_setopt($mysession, CURLOPT_HEADER, false);
  curl_setopt($mysession, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($mysession, CURLOPT_POST, false);
  curl_setopt($mysession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($mysession, CURLOPT_USERPWD, "admin:$password");
  curl_setopt($mysession, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($mysession, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
  $xml = curl_exec($mysession);
  curl_close($mysession);
  
  $songs = "\n";
  $xmlreader = new XMLReader();
  $xmlreader-&gt;xml($xml);
  
  //$xmlreader-&gt;next('SHOUTCASTSERVER');
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  $xmlreader-&gt;read();
  
  $songs .= "\n";
  $songs .= $xmlreader-&gt;readString();
  echo $songs;
  
  
  // Cache the output to a file
  $fp = fopen($cachefile, 'w');
  fwrite($fp, ob_get_contents());
  fclose($fp);
  ob_end_flush(); // Send the output to the browser
  
  
  ?&gt;
  </code></pre>
  
  <p>Here is the XML it parses:</p>
  
  <pre><code>&lt;SHOUTCASTSERVER&gt;
  &lt;CURRENTLISTENERS&gt;2&lt;/CURRENTLISTENERS&gt;
  &lt;PEAKLISTENERS&gt;29&lt;/PEAKLISTENERS&gt;
  &lt;MAXLISTENERS&gt;300&lt;/MAXLISTENERS&gt;
  &lt;UNIQUELISTENERS&gt;2&lt;/UNIQUELISTENERS&gt;
  &lt;AVERAGETIME&gt;16318&lt;/AVERAGETIME&gt;
  &lt;SERVERGENRE&gt;Misc&lt;/SERVERGENRE&gt;
  &lt;SERVERURL&gt;&lt;/SERVERURL&gt;
  &lt;SERVERTITLE&gt;&lt;/SERVERTITLE&gt;
  &lt;SONGTITLE&gt;The Velvet Underground - White Light/White Heat&lt;/SONGTITLE&gt;
  &lt;NEXTTITLE/&gt;
  &lt;STREAMHITS&gt;5093&lt;/STREAMHITS&gt;
  &lt;STREAMSTATUS&gt;1&lt;/STREAMSTATUS&gt;
  &lt;STREAMPATH&gt;/urbos.mp3&lt;/STREAMPATH&gt;
  &lt;BITRATE&gt;96&lt;/BITRATE&gt;
  &lt;CONTENT&gt;audio/mpeg&lt;/CONTENT&gt;
  &lt;VERSION&gt;2.0.0.29 (posix(linux x86))&lt;/VERSION&gt;
  &lt;LISTENERS&gt;
  &lt;LISTENER&gt;
  &lt;HOSTNAME&gt;&lt;/HOSTNAME&gt;
  &lt;USERAGENT&gt;unregularradio/1.0 CFNetwork/548.0.4 Darwin/11.0.0&lt;/USERAGENT&gt;
  &lt;CONNECTTIME&gt;114&lt;/CONNECTTIME&gt;
  &lt;UID&gt;0xb3a0a0cc&lt;/UID&gt;
  &lt;/LISTENER&gt;
  &lt;LISTENER&gt;
  &lt;HOSTNAME&gt;&lt;/HOSTNAME&gt;
  &lt;USERAGENT&gt;
  iTunes/10.5.3 (Windows; Microsoft Windows Vista Business Edition Service Pack 2 (Build     6002)) AppleWebKit/534.52.7
  &lt;/USERAGENT&gt;
  &lt;CONNECTTIME&gt;32522&lt;/CONNECTTIME&gt;
  &lt;UID&gt;0xb4104f0c&lt;/UID&gt;
  &lt;/LISTENER&gt;
  &lt;/LISTENERS&gt;
  &lt;SONGHISTORY&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329390441&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;The Velvet Underground - White Light/White Heat&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329390265&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;Kim Hiorthøy - Torture Happiness&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329390087&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;Wild Adriatic - The Writer&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329390058&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;UNregular Radio - Lift Fest www.liftfest.com&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329389770&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;Mister Vertigo - Spear Hill&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329389627&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;The Pinkerton Thugs - Together&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329389360&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;Jack Johnson - Go On&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329389154&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;
  Yung Damon!_My Show ft. Gucci Mane prod. by Big Hurt (Dirty)
  &lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329388975&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;Gayle Skidmore - Bad For Me&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;SONG&gt;
  &lt;PLAYEDAT&gt;1329388741&lt;/PLAYEDAT&gt;
  &lt;TITLE&gt;Dubb Zero - Track two&lt;/TITLE&gt;
  &lt;/SONG&gt;
  &lt;/SONGHISTORY&gt;
  &lt;/SHOUTCASTSERVER&gt;
  </code></pre>