I need to process some large files. I have found that PHP functions use large part of memory. In the example below, the memory used by PHP functions ends up being four (4) times the file size, but I can understand the transient use of the file's memory size twice, but not four times. Finally stripe out the _limit PHP memory. Although I can increase the PHP memory_limit it is not a good long-term solution, because I may have to process large files, and the PHP per process in the production environment is not worth the 400 MB, is not desirable.
Code:
$ buf = ''; Report_memory (___LINE__); $ Buf = file_get_contents ('./20MB.pdf'); Report_memory (___LINE__); Base64_encode ($ buf); Report_memory (___LINE__); Urlencode ($ buf); Report_memory (___LINE__); Function report_memory ($ line = 0) {echo 'line:'. Str_pad ($ line, 3) "'; Echo 'Mem:' Str_pad (intval (memory_get_usage () / 1024) 'K', 8). ''; Echo 'peak:' str_pad (intval (memory_get_peak_usage () / 1024). 'K', 8). ''; Echo "\ n"; }
Output:
Line: 4 mem: 622 peak: 627 km Line: 7m: 21056 peak: 21074 line: 10m : The top of 21056: line of 48302: 13mm: 21056 peak: 82358
Anyone can see that 21 MB for current memory usage for a 20 MB file, while the extreme Memory usage jumps up to a crazy 82 MB. / P>
The PHP functions used in this example are unchecked, I can easily swap from str_replace, is_string, gettype, etc. with the same result.
The question is how can I do PHP?
You are url-encoding, seeing that your PDF is basically" random "binary waste, without the MANY of the bytes -printing is that it means that you are moving a byte "binary" character to a 3+ byte URL-encoded string. You've got 20 MEG PDFs, it's no surprise that three times the amount of text is bloating your memory, remember that PHP has to keep two copies of your PDF, when it's working: the original " Raw "version, and copying the work you are working on.
One of the worst situations is "Every single character is encoded", your 20 mg PDF will be converted into 60 MEG URL-encoded string, due to which 20 + 60 = 80 mega pixels are used, even if Only the 60 MEG encoded version is thrown away immediately.
No comments:
Post a Comment