Forum


Replies: 3   Views: 4490
Out of memory error on larger files
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by truth@proposaltech.com  · 18-12-2013 - 22:45

We had the same problem and after many hours of trial and error, I've cut the peak memory usage of phpdocx in half while generating the exact same output.  I generated the same 460kb docx file three times with the following peak memory usages:



1355mb:  3.6 as downloaded from phpdocx.com



1072mb:  Replacing accessing the font properties using the magic __get function with a normal function



631mb:  Also clearing the font properties when no longer needed.



I tried other methods of reducing memory usage, but the other methods only saved ~1% of the memory consumption and required far more changes to the code.  (For example, I swapped out some array usage for splfixedarray usage.)  The changes I made to achieve the memory reduction can be easily applied by following the notes below.  I hope the phpdocx team incorporates this in their next release (so everyone can save on memory consumption and so I don't have to patch my upgrade ;) ).



Add the following to lib/dompdfParser/include/frameparser.cls.php (e.g. near "function set_style")



  // This representation of a DOM element will likely be part of a tree

  // and will continue to be stored after processing this subtree has

  // completed.  The style information consumes a large amount of memory

  // and may be cleared when no longer needed.

  function clear_style() {

    $this->_style = null;

  }





In lib/dompdfParser/include/parserhtml.cls.php



Before each "return" in _render(), add:



                                        // Frame processing has completed.  Free memory.

                                        $frame->clear_style();



Replace



                                try{$sTemp = $properties->$style;}

with

                                try{$sTemp = $properties->nonmagic_get($style);}



In lib/dompdfParser/include/styleparser.cls.php



Replace



  function __get($prop) {



with



  function __get($prop) {

    return $this->nonmagic_get($prop);

  }



  // The magic of __get leaks memory. Calling a normal function avoids the leak.

  function nonmagic_get($prop) {