title: laravel 4 artisan command memory issue
body: <p>I wrote an artisan command to export exercises from a database, to standalone packages that can be used in an e-learning system like moodle, ...</p>
  
  <p>It is a huge amount of exercises, and after a while the memory gets exhausted.</p>
  
  <p>I tried to unset variables, activate the garbage collector, disabled the query log, and did some profiling, but till now with no success</p>
  
  <p>I attached my script below, with each exercise I process, the memory usage adds up with 300k
  Any idea's what I can do?</p>
  
  <p>
  
  <pre><code>use Illuminate\Console\Command;
  use Illuminate\Filesystem\Filesystem;
  use Symfony\Component\Console\Input\InputOption;
  use Symfony\Component\Console\Input\InputArgument;
  
  set_time_limit(0);
  
  class ExerciseExportCommand extends Command {
  
      /**
       * The console command name.
       *
       * @var string
       */
      protected $name = 'exercises:export';
  
      /**
       * The console command description.
       *
       * @var string
       */
      protected $description = 'Export all exercises of a method or a specific exercise.';
  
      /**
       * Create a new command instance.
       *
       * @return void
       */
      public function __construct()
      {
          parent::__construct();
      }
  
      /**
       * Execute the console command.
       *
       * @return void
       */
      public function fire()
      {
          try {
  
              DB::disableQueryLog();
  
              ini_set('memory_limit','1024M');
  
              $this-&gt;info('Initial: ' . number_format(memory_get_usage(), 0, '.', ',') . " bytes\n");
  
              $base = base_path() . "/export/";
              $playerPath = base_path() . "/public/preview/dist/";
              $uploadsPath = base_path() . "/public/uploads/";
  
              $methodId = $this-&gt;option('method');
              $categoryId = $this-&gt;option('category');
              $exerciseId = $this-&gt;option('exercise');
  
              $this-&gt;info("Swing baby...");
              $this-&gt;info("Let's export some exercises, shall we ?");
  
  
              //we make an array which holds all the exercises we have to export
              $exercises = array();
  
              if($methodId === NULL &amp;&amp; $categoryId === NULL &amp;&amp; $exerciseId === NULL){
                  //we are here now anyways, let's do all exercises at once...
                  $this-&gt;comment("Nothing specified, let's do all exercises ");
                  $exercises2 = Exercise::all();
                  foreach ($exercises2 as $exercise){
                      array_push($exercises, $exercise-&gt;id);
                  }
                  unset($exercises2);
  
              }
  
              //get all exercises for given methodId
              if($methodId !== NULL){
                  $method = Method::with('categories.exercises')-&gt;find($methodId);
                  if($method == NULL) break;
  
                  $this-&gt;comment("We are ready to roll method " . $method-&gt;code);
                  foreach($method-&gt;categories as $category){
                      foreach($category-&gt;exercises as $exercise-&gt;id){
                          array_push($exercises, $exercise);
                      }
                  }
                  unset($method);
              }
  
              //get all exercises for given categoryId
              if($categoryId !== NULL){
                  $category = Category::with('exercises')-&gt;find($categoryId);
                  if($category == NULL) break;
  
                  $this-&gt;comment("We are ready to roll category " . $category-&gt;name_prefix . " " . $category-&gt;name);
                  foreach($category-&gt;exercises as $exercise-&gt;id){
                      array_push($exercises, $exercise);
                  }
                  unset($category);
              }
  
  
              if($exerciseId != null){
                  $exercise = Exercise::find($exerciseId);
                  if($exercise != NULL) {
                      array_push($exercises, $exercise-&gt;id);
                      $this-&gt;comment("Exercise added for export: " . $exercise-&gt;name_prefix . " " . $exercise-&gt;name);
                  } else {
  
                  }
                  unset($exercise);
              }
  
              if(empty($exercises)){
                  $this-&gt;error("No exercises could be found for given method/exercise");
                  exit();
              } else {
                  $this-&gt;comment("Currently counting " . count($exercises) . " exerises to export");
              }
  
              $fs = new Filesystem();
  
              //loop the exercises and publish like a charm
              foreach($exercises as $exerciseId){
                  $exercise = Exercise::find($exerciseId);
                  //determine destination
                  $path = $base . $exercise-&gt;getPath();
                  $this-&gt;comment("starting exercise " . $exercise-&gt;id);
  
                  //check if path exists, if it does, wipe it out
                  if($fs-&gt;exists($path)){
                      $fs-&gt;deleteDirectory($path, true);
                      $this-&gt;comment("wiped out " . $path);
                  }
  
                  //copy player files
                  //echo "copying " . $path . "&lt;br /&gt;";
                  $fs-&gt;copyDirectory($playerPath, $path);
  
                  $fs-&gt;cleanDirectory($path."styles/skins");
  
                      //copy only necesary skin files to save disk space
                      $method = $exercise-&gt;method();
  
                      if($fs-&gt;exists($playerPath."styles/skins/".$method-&gt;code)){
                          $fs-&gt;copyDirectory($playerPath."styles/skins/".$method-&gt;code, $path."styles/skins/".$method-&gt;code);
                      } elseif($method-&gt;code == "kameleonspelling" || $method-&gt;code == "kameleontaalbeschouwing"){
                          $fs-&gt;copyDirectory($playerPath."styles/skins/kameleon", $path."styles/skins/kameleon");
                      }
  
                      if($fs-&gt;exists($playerPath."styles/skins/".$method-&gt;code.".css")){
                          $fs-&gt;copy($playerPath."styles/skins/".$method-&gt;code.".css", $path."styles/skins/".$method-&gt;code.".css");
                      }
  
                  $this-&gt;comment("copied player files to " . $path);
  
                  //copy resources
                  //echo "copying resources " . $path . "&lt;br /&gt;";
                  $fs-&gt;copyDirectory($uploadsPath . $exercise-&gt;id . "/", $path);
                  $this-&gt;comment("copied resources to " . $path);
  
                  //copy slide resources
                  $slides = Slide::where('exerciseID',"=",$exercise-&gt;id)-&gt;get();
                  mkdir($path."slides/");
  
                  foreach ($slides as $slide) {
                      $image = $slide-&gt;image()-&gt;first();
                      if($image != NULL){
                          $this-&gt;info($uploadsPath."slides/".$image-&gt;resourceUri);
                          $this-&gt;info($path."slides/".$image-&gt;resourceUri);
                          $fs-&gt;copy($uploadsPath."slides/".$image-&gt;resourceUri, $path."slides/".$image-&gt;resourceUri);
                      }
                      unset($image);
                  }
                  $this-&gt;comment("copied slide resources to " . $path);
  
                  //save xml file
                  $content = Exercise::getXmlContent($exercise-&gt;id);
                  $fs-&gt;put($path . "exercise.xml", View::make('xml', $content));
                  $this-&gt;comment("saved xml to " . $path);
  
                  $this-&gt;info("finished exercise " . $exercise-&gt;id);
  
                  unset($method);
                  unset($content);
                  unset($slides);
                  gc_collect_cycles();
                  $this-&gt;info('Peak: ' . number_format(memory_get_peak_usage(), 0, '.', ',') . " bytes\n");
                  $this-&gt;info('End: ' . number_format(memory_get_usage(), 0, '.', ',') . " bytes\n");
              }
  
              $this-&gt;info("Awesome Possum =&gt; finished all exercises ");
              $this-&gt;info('Peak: ' . number_format(memory_get_peak_usage(), 0, '.', ',') . " bytes\n");
              $this-&gt;info('End: ' . number_format(memory_get_usage(), 0, '.', ',') . " bytes\n");
  
          } catch(Exception $e){
              $this-&gt;error($e-&gt;getMessage());
              $this-&gt;comment($e-&gt;getTraceAsString());
          }
      }
  
      /**
       * Get the console command arguments.
       *
       * @return array
       */
      protected function getArguments()
      {
          return array(
              //array('example', InputArgument::REQUIRED, 'An example argument.'),
          );
      }
  
      /**
       * Get the console command options.
       *
       * @return array
       */
      protected function getOptions()
      {
          return array(
              array('method', null, InputOption::VALUE_OPTIONAL, 'The id of a method  for which all the exercises to export.', null),
              array('category', null, InputOption::VALUE_OPTIONAL, 'The id of a category for which all the exercises to export.', null),
              array('exercise', null, InputOption::VALUE_OPTIONAL, 'The id of an exercise to export.', null),
          );
      }
  
  }
  </code></pre>
  
  <p>This is a dump of my xdebug trace command, with the 20 most memory consuming statements:</p>
  
  <pre><code>Showing the 20 most costly calls sorted by 'memory-own'.
  
                                                                         Inclusive        Own
  function                                                       #calls  time     memory  time     memory
  -------------------------------------------------------------------------------------------------------
  debug_backtrace                                                   646  0.0420 20353496  0.0420 20353496
  Composer\Autoload\ClassLoader-&gt;loadClass                          259  0.1911 17556224  0.1139 13953824
  PDOStatement-&gt;execute                                             743  0.1184 13729408  0.1184 13729408
  array_merge                                                      4051  0.1282  3894816  0.1282  3894816
  Illuminate\Database\Eloquent\Model-&gt;newInstance                  1534  0.4715  3806344  0.0791  3732712
  PDOStatement-&gt;fetchAll                                            742  0.0323  2364264  0.0323  2364264
  Illuminate\Database\Eloquent\Model-&gt;newBaseQueryBuilder           738  0.6625  2177352  0.0657  1688968
  explode                                                          3396  0.1026  1296960  0.1026  1296960
  Illuminate\Database\Eloquent\Model-&gt;newFromBuilder               1534  0.6883  5139552  0.0944  1259576
  str_replace                                                     10254  0.3176  1228824  0.3176  1228824
  compact                                                           920  0.0339  1181384  0.0339  1181384
  PDO-&gt;prepare                                                      743  0.1403   816488  0.1403   816488
  sprintf                                                          2381  0.0741   802968  0.0741   802968
  implode                                                          5586  0.1722   536688  0.1722   536688
  array_map                                                         864  0.3164   588512  0.0386   477088
  get_class_methods                                                  15  0.0059   472296  0.0059   472296
  Illuminate\Database\Eloquent\Model-&gt;newQuery                      738  0.9783  3044352  0.0656   448488
  include                                                           263  6.7525  5732672  0.0468   410416
  call_user_func_array                                             1585  0.5734  3937936  0.0659   357056
  SplFileInfo-&gt;getPathname                                         2724  0.0847   344768  0.0847   344768
  </code></pre>