Scenario-
I am running B* instances on App Engine. I\'ve a background ETL related task(written in python) scheduled as a cron job on App Engine. Whe
There are a few questions on StackOverflow describing similar memory issues for tasks when using ndb on app engine. Here is one example.
The issue is that app engine doesn't clear the ndb context cache upon the conclusion of a task so context cache continues to hog your memory long after the task completes.
The solution is to not use or clear the context cache during your tasks. Here are a few ways:
key.get(use_cache=False)ndb.get_context().clear_cache() at appropriate times._use_cache = False to your model definition.From my observations with an app using lots of memory for StringIO operations:
explicitly calling gc.collect() didn't noticeably help (I even suspected for a while that I actually have memory leaks, but it wasn't the case)
the memory is not freed after each and every request, but, if the instance remains alive long enough without running out of memory it does eventual
appears to be freed now and then. Easy to test - just increase the time between requests to reduce the free memory draining rate. But I couldn't figure out a usable pattern. Note that I observed this only after upgrading to B2 instances, my B1 instances were running out of memory too fast, I never noticed a freeing event with them.
using an instance class with more memory (which I tried as a workaround for my instances eventually running out of memory) helped - the memory appeared to be freed more often. It might be because these instances also have a faster CPU (but that's just guesswork).