Side note: code follows the example provided by the google documentation. Note that ownCloud requires a data stream. Chunked / ranged downloads (which seems to be the natural alternative according to the google documentation) doesn’t fit here and will need to be adapted, which is quite complex to do.
Make sure you have plently of memory available. Other processes running in the server could also eat memory, so the php process might hit a hard limit before reaching the established 4GB memory for php.
Since this is a server problem, it’s way more interesting the server logs. We know the client will keep malfunction until the problem is solved in the server.
Double-check the memory limit is correctly applied. You’ll likely need to restart the web server. The easiest way to check is to download the file and check if there is an error log in apache; you’ll see the Allowed memory size of 536870912 bytes exhausted (tried to allocate 3410408792 bytes) error in the apache logs. There you can see the limit of ~512MB for php
[Fri Jan 22 16:07:12.014813 2021] [php7:error] [pid 2190] [client 187.75.157.120:56859] PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 3410408792 bytes) in /var/www/html/cloud/apps/files_external/3rdparty/guzzlehttp/streams/src/Stream.php on line 135
It seems the php value memory_limit doesn’t is the reference for this process