PHP memory exhausted by Media:create()

Created on 9 August 2023, over 1 year ago

For some reason when I try to create a media entity programmatically, it exhausts PHP's memory. I have 1G allocated to PHP memory, so there should be no problem. This is the code that exhausts PHP's memory:

$image_data = file_get_contents(__DIR__ . '/../../../d7_images/test-image.png');
$file_repository = \Drupal::service('file.repository');
$image = $file_repository->writeData($image_data, "public://test-image.png", FileSystemInterface::EXISTS_REPLACE);
// If I exit here, there is no problem.  The file is created in the correct location.  

$image_media = Media::create([
    'bundle' => 'image',
    'uid' => \Drupal::currentUser()->id(),
    'field_media_image' => [
      'target_id' => $image->id(),
    ],
]);

I have confirmed that my script runs fine up until the above Media:create() function. Here is the error message:

Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 802816 bytes) in /var/www/html/web/core/lib/Drupal/Core/File/FileSystem.php on line 506

Here is what FileSystem.php looks like starting at line 506:

if (file_put_contents($temp_name, $data) === FALSE) {
      $this->logger->error("Temporary file '%temp_name' could not be created.", ['%temp_name' => $temp_name]);
      throw new FileWriteException("Temporary file '$temp_name' could not be created.");
}

I don't get any messages in the Drupal error log. The only error is the the above memory error.

Do you have any idea what the issue might be?

πŸ’¬ Support request
Status

Closed: works as designed

Version

10.1 ✨

Component
MediaΒ  β†’

Last updated about 23 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States arnoldbird

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024