title: Symfony2: SerializerBuilder and Memory
body: <p>I am working on a small API with Symfony 2 and I am having problems with the serialization of data. </p>
  
  <p>I have this code in my controller:</p>
  
  <pre><code>// Create the $serializer
  $serializer = SerializerBuilder::create()-&gt;build();
  // Put the serialized data in $jsonContent
  $jsonContent = $serializer-&gt;serialize($products, 'json');
  // Generate a Json Response
  $response = new Response($jsonContent);
  $response-&gt;headers-&gt;set('Content-Type', 'application/json');
  return $response;
  </code></pre>
  
  <p>But I have more than 1000 rows in my <code>$products</code> collection, and it seems that my server doesn't have enough memory to handle that, this code returns <code>HTTP Error 500</code>. Here are the Apache log line:</p>
  
  <pre><code>PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 75 bytes) in /var/www/blog/vendor/jms/serializer/src/JMS/Serializer/GenericSerializationVisitor.php on line 152
  </code></pre>
  
  <p>How could I do to optimize memory (and make work my code) in this case?</p>
  
  <p>Thanks a lot for the tips!</p>