i’m launching thousands of lambda functions, concurrently. They each pull a big chunk of data from S3, do some manipulation, and then write results.
(Yes, I understand that there are other ways to do this - this particular use case is not a good candidate for EMR, etc)
Each of these lambdas typically takes about 10 minutes, and maxes out at about 8-9GB of RAM (configured for 10GB / max RAM). They are very similar.
I’m seeing some behavior that I don’t understand:
When I have many hundreds or thousands of containers running, some of these lambda instances will fail for lack of memory. I’ll request to perform an operation on a data frame, and get an error about “unable to allocate x mb for an array with blah blah”
But, I can re-run with the exact parameters and sometimes succeed. Especially if I wait several minutes until there aren’t as many containers running,
Since the size of my input does not change, and the size of my memory does not change, why would I only *sometimes* fail for lack of memory?
Logically, it leads me to think that the memory on the host machine is over-provisioned and I can’t get my full requested amount unless it is available. Or, possibly, there’s some kind of scaling that isn’t keeping up with the promised memory allocation.
Can anyone confirm that or shed any light on this mystery for me?
Logically, it leads me to think that the memory on the host machine is over-provisioned
It’s not.
I can’t get my full requested amount unless it is available.
You always can.
Or, possibly, there’s some kind of scaling that isn’t keeping up with the promised memory allocation.
There isn’t.
Since the size of my input does not change, and the size of my memory does not change, why would I only *sometimes* fail for lack of memory?
Because at that particular point in time there wasn’t enough memory in your Lambda. Something from the rest of your code was using it.
But, I can re-run with the exact parameters and sometimes succeed. Especially if I wait several minutes until there aren’t as many containers running,
This almost confirms that you have a memory leak.
Lambda launches “execution environments”, each with the amount of memory that you configured (and corresponding CPU and memory). Once your code finishes processing a request, the “execution environment” may be reused - that’s the foundation of how Lambda works, so it stays around, in a “sleep” state. If you don’t send requests within a certain amount of time that would use that “execution environment” it’s then destroyed. The subsequent request will create a new one (called “cold start”), that’s completely clean.
Also note that if your Lambda explodes with an out of memory error, that particular “execution environment” will be reclaimed.
So, if you have a memory leak, from one execution to the next you’ll have the impression of having less memory available. Not because of any of the reasons you described, but because your own code is consuming memory.
You are right in saying it’s a mystery on how the host allocates memory - i don’t think AWS publishes much on that. But to your point of some jobs failing - it could be due to the specifics of the data in that invocation. The dataframe you are dealing with in that specific instance may be such that it needs more memory than others. Each Lambda container is independent of the other and is dedicated the RAM you allocated to it.
Btw an AWS account can only run a max of 1000 concurrent Lambda by default - so I doubt you can run 1000s of concurrent containers. There’s a metrics you can monitor to see how many concurrent Lambdas you are running. Once you hit 1000, you’ll be throttled. You can request for your account limit to be increased of course (via AWS support ticket)
I know you know this but I’ll say it again - your use case doesn’t seem to be a good fit for Lambda. You probably spend more on Lambda compute time and memory than if you’d move your jobs to say AWS Glue. Or break your Lambda into multiple Lambdas and orchestrate it’s execution using AWS Step Functions.
Perfect explanation. One of the previous execution environments was reused and hadn’t released the memory it had used, probably because there’s a global reference that hasn’t been cleaned up.
The entire firecracker container is open source, so you can go and see for yourself how it provisions memory. I’d say that was publishing a fair amount
But following that logic, if they are all launched at exactly the same time, nothing would be re-used. The subsequent run at a later time, that they say succeeds, is the run more likely to get run on a re-used environment.
I don’t know what programming language you’re using or how you’re allocating memory, but I can make a reasonable guess…
When a program asks the underlying OS to allocate memory, the OS can only satisfy that request if that much contiguous memory is available.
As an example, let’s say you make ten 1MB memory allocations labeled A through J in a 10MB lambda execution environment.
Assume that when they are allocated, the OS arranges them in order so that A is allocated from memory address 0, then B immediately follows that, etc. through J.
Now let’s say your code frees allocations B, D, F, H, and J.
You now have a total of 5MB free memory. However, the largest allocation you’ll be able to make is 1MB, because that’s the largest contiguous block of memory the OS has.
Yeah he mentioned dataframes so probably python. Python has some known memory leak issues when using underlying C libraries that don’t clean up their memory properly (it doesn’t get garbage collected). I ran into this recently with celery tasks (a similar execution environment that reuses the execution environment) that were using some cell biology libraries with a bad memory leak. In contexts where you’re running your code in a stand-alone process and then it exits it’s fine because memory is released properly but when the process/task is reused it is not.
Chances are it’s not even OPs code with the issue it is some underlying library that doesn’t clean itself up properly because it just assumes the process will die.
That said, do you know of any python tricks to help free this memory?
I call “del X” against all my data structures / variables when I’m done with them, and also do a gc.collect() at the end of the function.
I even added a loop at the end of the lambda that checks the memory footprint, and if it’s more than 2 GB, it will call gc.collect() and then sleep for 30 seconds.
What I’m seeing is that about 4GB stays tied up no matter how long that loop runs. And I see that it’s still tied up in the next lambda iteration that re-uses the container.
I see that I could call an API to update lambda definition and force a cold start. But surely there’s a better option…
Why would leak of customer provided code impacts the next execution? Isn’t it then a Lambda side bug? Do you have document explaining that?
Are you saying that Lambda reuses python process? So technically I can keep state between executions?
You have a memory leak. Rather than sleeping for 30 seconds, I’d track down why your process is leaking memory. You could start by looking into tracemalloc, and if you’re using third party libraries, consider something like valgrind to see what’s being used by who.
If you really want a “trick”, you could just do something like this:
Forgive my python ignorance - are you saying that by invoking code through a __main__, the memory will be cleaned up by simply leaving the function (like in Java), where running in the lambda_handler will not?
For example, if lambda_handler loads up a large data frame DF, and then calls del DF, that will be different than if helper() does the same thing?