I am saving a lot of entities as part of a script - several thousand.

This is frequently causing out of memory errors.

I've been trying to do something like this:

$entity = NULL;

After entity save ($entity is the variable for the entity object), but nothing is working.

How do I clear the memory for the past entities? There is no use for it after the entity is saved.

  • 3
    Flusing the caches is not going to help when saving thousands of entities. You should use the Batch API. – 2pha Jan 17 '15 at 3:14
  • 1
    Clearing caches is very heavy on memory, it'll make things considerably worse. entity_save will clear the relevant entity and field caches anyway, so it's a pointless exercise. Without seeing your code it's impossible to suggest how it might be performing badly, but regardless, you should prepare yourself for the possibility that your entities are just too complex for the hardware your site's sitting on. Only you can really work this out after a decent profiling session. But as others have said, use the batch Api - it exists for exactly this problem – Clive Jan 17 '15 at 10:58
  • Is there a way to ensure that the processing will be relatively fast (as fast as it can be done?) - this is for a high priority process, and I was under the impression that Batch API didn't necessarily prioritize the processing of its items (isn't it done over cron?) – Jack Ryan Jan 17 '15 at 14:50
  • Batch API doesn't use cron tasks, but it depends on somebody visiting the page that starts the batch operations. – kiamlaluno Jan 18 '15 at 3:30
  • It seems like it can be activated by a function call. Is this not the case? But yes, Batch API is doing precisely what I wanted. – Jack Ryan Jan 18 '15 at 16:27

If you have to perform long "bulk" actions, I suggest you three ways:

  • 1
    "drush commands don't have memory and time limits" - false on most professional systems I worked with. Limits can be set for PHP CLI, and all admins I know did set them. they are independent, and usually significantly higher than limits set on web PHP, but they may be there. – Mołot Jan 18 '15 at 11:29
  • Yes, BatchAPI did what I wanted. I used it before some months ago on a project, but I was under the impression that it queued everything. I guess my memory is faulty here. – Jack Ryan Jan 18 '15 at 16:28
  • @Molot, you are right :) – arrubiu Jan 18 '15 at 21:17
  • Hrm, I'm noticing that Batch commands need to be run with a page open. I want it to run automatically in the background. – Jack Ryan Jan 25 '15 at 19:19
  • @AndrewAlexander, you could try to create a drush command – arrubiu Jan 25 '15 at 19:27

In /etc/php5/apache/php.ini increase the max_execution_time this may solve your problem.

  • And may lead to other problems, like server choking on unfinished tasks, swapping etc.This should never be recommended without a warning. – Mołot Jan 18 '15 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.