Browser memory usage increase on SaveChangesAsync()
myabuta opened this issue · 0 comments
Describe the bug
I'm not exactly sure if this is considered a bug, but SqliteWasmHelper my app to crash due to high memory usage.
I have an app that functions while offline, so I have to pre-load some data to the sqlite, incase the device loses internet connection.
I noticed that my app crashed consistently, and after some extensive investigation, I realized that every time the app save or update data in Sqlite, the browser memory increases. The bigger the database, the faster the memory usage increases. The wasm app that I'm running on an ipad crashes when it reaches a certain memory usage.
To Reproduce
Steps to reproduce the behavior:
Basically, you need to create an app where you can add data to the sqlite fairly fast. So for this example, I'll help you reproduce it using the project available to download directly from this repo.
- Download the sample app directly from this repo
- Open index.razor in (BlazorWasmExample proj -> Pages)
- Modify AddThingAsync() to this:
private async Task AddThingAsync()
{
string random = GenerateName(5);
if (!string.IsNullOrWhiteSpace(random))
{
using var ctx = await Factory.CreateDbContextAsync();
ctx.Things.Add(new Thing { Name = random });
busy = true;
await ctx.SaveChangesAsync();
busy = false;
await RefreshUiAsync();
}
}
- Create this method:
public static string GenerateName(int len)
{
Random r = new();
string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "l", "n", "p", "q", "r", "s", "sh", "zh", "t", "v", "w", "x" };
string[] vowels = { "a", "e", "i", "o", "u", "ae", "y" };
string Name = "";
Name += consonants[r.Next(consonants.Length)].ToUpper();
Name += vowels[r.Next(vowels.Length)];
int b = 2;
while (b < len)
{
Name += consonants[r.Next(consonants.Length)];
b++;
Name += vowels[r.Next(vowels.Length)];
b++;
}
return Name;
}
- Run App.
- On Chrome, Open Task Manager by pressing "shift + esc" or manually open by pressing on the browser's vertical ... on the top right, -> More tools => Task Manager
- Take a note on the apps' memory usage
- Now in the index page, without entering any string in the entry box, just click "Add". It should generate a random string and add to the list.
- Spam the "Add" button to replicate a rapid data entry (or data being saved from an API result).
- You will see that the memory usage starts to increase rapidly, and it never releases the space.
- This issue does not happen on reads. Only writes.
Expected behavior
Be able to write to the sqlite without exponential memory increase.
Desktop (please complete the following information):
- OS: Win 11
- Browser Latest Chrome. Same issue on all browsers
- Version [e.g. 22]
Additional context