You can resolve this memory issue by using the
XMLReader
PHP class instead of
DOMDocument
If you're using the Magento framework, you can use
Laminas\Config\Reader\Xml
instead of
Magento\Framework\Xml\Parser
class.
DOMDocument class will try to load all the XML content to memory at once but XMLReader class will NOT load the content to memory at once. It will read the nodes set by set incrementally.
You can find more info on this one in this article https://medium.com/devops-dev/way-to-read-large-xml-dataset-in-magento-2-using-laminas-config-reader-xml-2b59c936bbcc :)
Cheers!