How to fix the «Allowed memory size exhausted» / «Out of memory / Bad gateway» failure in Magento/Framework/ObjectManager/Relations/Runtime.php?

Fatal error: Out of memory (allocated 2097152) (tried to allocate 7310293557949198992 bytes) in lib/internal/Magento/Framework/ObjectManager/Relations/Runtime.php on line 38

How to find our the problem class

1. Create the folder _my/log.

2. Locate the line:

3. Add the following code above:

file_put_contents(BP . "/_my/log/{$_SERVER['REQUEST_TIME']}.log", $type . "\n" , FILE_APPEND);

As a workaround, I deploy the static content manually even in the developer mode:

bin/magento setup:static-content:deploy -f

The problem is caused by a lack of memory for the Oyejorge Less compiler (which is used by Magento 2) or for other Magento 2 components.

The memory is limited by the .htaccess and the .user.ini files used by Magento 2.

###How to fix the problem

Increase the memory limit (e.g., up to 2048M) in the following places:

1. .user.ini

2. .htaccess (2 places)

4. pub/.htaccess (2 places)