ASP.NET core web API - OOM on Ubuntu after upgrade to 2.1.
We have a .net core web API running on AWS Ubuntu 16.04 VM. The API is using EF to query a relatively small DB and makes a lot of external requests due to integrations with other systems. After we upgraded to 2.1. we started to have serious OOM problems on production. During the upgrade, we haven't changed much in our execution logic aside from the changes needed to support the changes in the APIs. I made sure we are reusing HttpClients and that the DBContext objects are scoped, honestly, I don't think it is a memory leak from changes in our code. Moreover, the app was running on the same environment with the same user load for roughly a year without problems. We make deployments regularly, but let's say it could be running continuously for two weeks.
After the upgrade, it consumes 6GB of ram for several hours and then crashes with OOM (actually the whole server crashes). The strange thing is that the memory used by the app goes up and barely goes down even in idle times. It's like barely anything gets released.
Looking at memory snapshots I can see a lot of memory cache collections growing big, many of them used by entity framework and the Json.Net lib.
I'm sure that all that memory caching improves performance, but my assumption is that the app should trade off performance for memory use based on the params of the system. Also, it seems like the GC doesn't release memory but I'm not very familiar with how its implemented. I wonder why it used to work fine with 1.1. We are using supervisor to start and motor the service, not sure if it matters, but this is also not new.
Is there some important configuration I could be missing to tell the system how much in-memory caching it could do?
Checked a few issues which might be related, but none seemed to offer a solution so far and I was not sure which is the 'most' related so I created this one.
Possibly related: https://github.com/dotnet/coreclr/issues/18044 https://github.com/dotnet/coreclr/issues/5551 https://github.com/aspnet/Home/issues/1976 (similar to what danports describes here and we are also considering downgrading after days of struggle)
I noticed that most people reporting the problem are reporting it with web api projects. We have another dot net MVC app running on that machine which doesn't seem to have the problem, but it has a significantly lower load, the API is just faster to exhaust the memory.
could be missing to tell the system how much in-memory caching it could do?
There is no setting like this in the runtime itself. The garbage collector is aware of the total amount of memory available and it will try hard to collect garbage when the memory consumption is close to this threshold.
The problem you are seeing is likely caused by a memory leak. It may be bug in the framework or it may be a latent bug in your code that got exposed by upgrading to 2.1.
Looking at memory snapshots I can see a lot of memory cache collections growing big, many of them used by entity framework and the Json.Net lib.
What is keeping these memory cache collections alive? This is the place to keep looking to find the root cause of the problem.
@jkotas thanks for your reply. Looking at the memory dumps one notable difference between 1.1. and 2.1. I can see is that in 2.1 there are a lot of objects of type Closure, which don't get collected by GC.collect.
On 1.1 a lot of Closure objects get generated after some heavy load operations as well, but after GC all disappear. On 2.1 they just keep on piling up. I waited for some idle time and run the collect several times, they are still present in the heap. In 1.1. after some idle time they disappear.
In the picture below I have highlighted the objects which differ in 2.1 compared to the 1.1. build. The rest of the picture is pretty much the same:

As far as I recall those are objects referenced in anonymous methods. Many of them are referenced from the EF memory cache but I'm not sure this matters, I wonder if there is something changed in the way closure is handled?
I wonder if there is something changed in the way closure is handled?
Nothing that I am aware of.
Many of them are referenced from the EF memory cache
I think that this is what you should look into more. Why are these referenced from the EF memory cache?
This seems to be one of the reasons https://github.com/aspnet/EntityFrameworkCore/issues/8223
Expand in the lower panel the "Paths to Root" tree, that may provide more insight into what's keeping the memory around
@benaadams thanks, I did this, this is how I found the issue from my comment above. As I mentioned, the only 'interesting' types I noticed there were EF and Json.Net. So far there is a good chance that 8223 was the main problem, we need to run more tests though.
We upgraded our Web APIs from netcoreapp2.0 to netcoreapp2.1 with all associated nuget package updates to 2.1.1 and we have started to see OOM exceptions on applications that have been running perfectly for over a year and a half. These applications were originally dotnet core 1.1, then upgraded to dotnet core 2.0, now onto 2.1.
Our ASPNETCORE_VERSION is 2.1.1 and we run docker containers in AWS. The OS is reported as Linux 4.9.85-37.55.amzn1.x86_64 dotnet/coreclr#1 SMP Mon Mar 5 19:00:48 UTC 2018., our Dockerfile specifies: FROM microsoft/dotnet:2.1-aspnetcore-runtime-alpine
Even our least-complex web API, which just reads data from SQL Server and yields it over HTTP is now throwing OOM exceptions.
On one of our APIs, at midnight, I see this lone error log statement: Exception thrown from SocketAsyncEngine event loop: System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
We do not use EF.
We're seeing similar issues that, based on timing, cropped up with an upgrade from 2.1.0 to 2.1.1. Details are in dotnet/coreclr#18637.
Similar to @nannanbatman's issue, we were using 2.1-*-alpine images. For us, at least, things were stable on 2.1.0.
Like @ydekova, we are reusing HttpClients. Not using EF in this project, though.
We are also reusing HttpClients.
We've dropped alpine for the time being. I'll report back in about 24 hours with results, or sooner if I observe more OOM exceptions without alpine.
@nannanbatman - for what it's worth, we were stable on Alpine and previously jessie. For our current experiment, changed our Dockerfile FROM lines to:
FROM microsoft/dotnet:2.1.300-sdk-alpine (was previously FROM microsoft/dotnet:2.1-sdk-alpine) and FROM microsoft/dotnet:2.1.0-runtime-alpine (was previously FROM microsoft/dotnet:2.1-runtime-alpine)
This locks us at the 2.1.0 version that was previously stable for us.
@natethegreat44 for a subset of our Web APIs, we changed FROM microsoft/dotnet:2.1-aspnetcore-runtime-alpine to FROM microsoft/dotnet:2.1-aspnetcore-runtime and we have not observed OOM for these Web APIs. We continue to observe OOM for some Web APIs that are still on -alpine, but not all of them.
Thank you @nannanbatman - that's good info. We're trying out a non-alpine base image now as well.
https://github.com/dotnet/coreclr/issues/18637#issuecomment-400872197 has details about the alpine-2.1.1 out of memory problem.
Let's keep this issue focused on the Ubuntu OOM issue.
We are having same issue on some of our projects. memory grows overtime and dose not come down.
calling GC.Collect() frees a lot of memory.
@ydekova are you still having problems with this? from a cursory look it sounds like you did a managed memory leak issue. did you confirm that's the case?
I'm closing this due to inactivity. please feel free to reactivate if you don't believe the issue is fixed.