Answer the question
In order to leave comments, you need to log in
How to reduce script memory consumption?
There is a script that creates an xml file os 100tys. goods.
Using the memory_get_usage () and memory_get_peak_usage () functions, I measure the amount of memory eaten.
The function that unloads products from the database eats up 200mb.
Next comes the usual foreach, which forms the xml.
After the foreach completes, the memory_get_peak_usage() function shows more than 1gb of memory. Too much ...
When exporting 45 thousand. goods, the script eats up 450mb. This is fine?
Tried to use array_chunk, split by 10,000k and use unset. But consumption is the same.
+ I look in ispmanager, how much memory it eats up - the data is the same.
How can the amount of memory consumed be reduced? The script has been optimized as much as possible. Or for 100 thousand. - 1GB is it normal?
Answer the question
In order to leave comments, you need to log in
Try XMLWriter to create XML if that's not what you're currently using. He has rather modest appetites from memory.
https://habr.com/post/112474/
https://m.habr.com/post/345024/
In short, xml processing is unlikely to be processed normally.
A tree house is being built, if possible, transfer it to csv and read it line by line and process it line by line. The trick is to keep in memory only what we are currently processing.
XMLWriter will suit you. But. The point is not to form a document in memory, but to write directly to a file. Then you won't have any problems at all.
It is strange to judge normally or not, if it is not clear, you only have a product ID or a description of 5000 characters in 10 languages. But I think yes, maybe this is normal, since the document in memory will take up a lot of space, and the garbage collector won’t clean anything up there.
If less memory needs to be used, then XML is just text. It can be written as a string directly to the file as it is read, if you just need to transfer it from the database to the file. Then the entire document will not be in memory with the entire structure.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question