'Memory consumptions skyrocketed after porting from Marten 3 to 5
We use MartenDB Event Store and project events to read model. We have an ASPNET Core API that queries the model and sends it to the client. Now, there is one endpoint that retrieves a huge document graph from the read model being queried every 10-15 seconds (the whole Postgress DB dump is around 9 MB), like that:
public async Task<Content> GetContent()
{
var layouts = await GetShallowLayouts();
var pages = await GetAllPages();
// ...
// Other calls to DB
// Combine all results into one Content object
return content;
}
private async Task<IReadOnlyList<Layout>> GetShallowLayouts()
{
using async var session = DocumentStore.LightweightSession();
return await session.Query<Layout>()
.OrderByDescending(x => x.Brand)
.ToListAsync();
}
private async Task<IReadOnlyList<ExperimentWithVariants>> GetExperiments()
{
using async var session = DocumentStore.LightweightSession();
return await session.Query<ExperimentWithVariants>()
.ToListAsync();
}
...
When we were using .NET Core 3.1 and Marten 3.X the whole app consumed around 250MiB in kubernetes pod (see screen below).

But when porting to Marten 5.x and NET6 the memory consumption grew to around 1.5 GiB (!) We tried to do a bit of refactoring to make all the calls one single opened session, like that:
public async Task<Content> GetContent()
{
using(var session = DocumentStore.LightweightSession())
{
var layouts = await GetShallowLayouts(session );
var pages = await GetAllPages(session );
// ...
// Other calls to DB
// Combine all results into one Content object
return content;
}
}
private async Task<IReadOnlyList<Layout>> GetShallowLayouts(IQuerySession session)
{
return await session.Query<Layout>()
.OrderByDescending(x => x.Brand)
.ToListAsync();
}
private async Task<IReadOnlyList<ExperimentWithVariants>> GetExperiments(IQuerySession session)
{
using async var session = DocumentStore.LightweightSession();
return await session.Query<ExperimentWithVariants>()
.ToListAsync();
}
but the memory is still arounf 1 GiB.
I know there is a bunch of config settings that are in Marten and some of them changed between ver 3 and 4 but did any had a similar problem and could shed some light on where should we look for the solution?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

