DOMDocument :: save XML():内存分配失败:增长缓冲区

I receive this error while i am trying to combine my xml files.I read other questions and answers put i could not find any solution for my code. I cannot increase ram of computer. Here is my code

  public function mergeXml ($filename,$source){
 $events = array();

 // open each xml file in this directory
foreach(glob("$source/*.xml") as $files) {         
// get the contents of the the current file
$events[] =$files;   // throw all files into an array  .
}

// Replace the strings below with the actual filenames, add or decrease as fit
$out = new \DOMDocument(); 
$root = $out->createElement("documents"); 


foreach ($events as $file) {    //get each file from array
$obj = new \DOMDocument();
$obj->load($file);      //load files to obj.

$xpath = new \DOMXPath($obj);
foreach ($xpath->query("/*/node()") as $node)  
    $root->appendChild($out->importNode($node, true));  }

$out->appendChild($root);        
 file_put_contents("$source/$filename.xml",$out->saveXML());