During the execution of the necessary to me PHP scripts that I knew required more memory for their execution, they were interrupted regardless of the fact that exactly 4G of memory was available. At first I thought that there was an option in Apache configuration that defines the maximum allowed memory for Apache. And I mistakenly thought that php memory_limit is limited to the remaining "possible allowed memory for Apache".
Later I remembered that this is the exact value I recently set in MySQL settings - myisam_max_sort_file_size = 4G. It was this option that reduced the available memory in the system, which caused the above error "Fatal error: Out of memory (allocated".
So the main solution would be the following - observe the exact amount of free memory on your server at the moment your php script gets an error "Fatal error: Out of memory (allocated". Think about what could be blocking free memory in the system.