Garbage collection takes forever! after running this code Garbage collection doesnt seem to end. I'm creating app in .net maui and have 17 json files for this class to better store data. Problem is accuring whenever i start the app. Debugger console fills up with:
[ame.codilitiapp] Explicit concurrent copying GC freed 277(158KB) AllocSpace objects, 0(0B) LOS objects, 24% free, 3125KB/4167KB, paused 11us,6us total 4.024ms
It spams itself like forever and i dont know what to do.
Ive tried:
Increasing ram size of android emulator, size of disc space, heap size, java heap size inside of project.
Class code:
namespace Resources.Classes
{
public class Lessons
{
public int Id { get; set; }
public string? Title { get; set; }
public string? pdf { get; set; }
public string[]? Tasks { get; set; }
}
}
Repository code :
internal class Repo
{
public Lessons[] LessonsList = new Lessons[17];
public void RepositoryVoid()
{
for (int i = 1; i < 18; i++)
{
LoadMauiAssetLessons($"{i}.json", i--);
}
}
private async void LoadMauiAssetLessons(string Path, int i)
{
try
{
using var stream = await FileSystem.OpenAppPackageFileAsync(Path);
using var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
if (JsonSerializer.Deserialize<Lessons>(contents) != null)
{
LessonsList[i] = JsonSerializer.Deserialize<Lessons>(contents);
}
}
catch (FileNotFoundException ex)
{
Debug.WriteLine("File not found :" + ex);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}
Sample of json file to load into lesson object:
{
"Id": 1,
"Title": "Iterations",
"pdf": "https://codility.com/media/train/Iterations.pdf",
"Tasks": [
"BinaryGap"
]
}
There are a few problems here, but garbage collection isn't one of them.
The first problem is not applying the async-await pattern (or Task Asynchronous Pattern) correctly. Instead of using async void
and calling the asynchronous code in a fire-and-forget fashion, you should be using async Task
and apply the async-await pattern all the way.
The second problem is your for-loop:
for (int i = 1; i < 18; i++)
{
LoadMauiAssetLessons($"{i}.json", i--);
}
This probably isn't doing what you're expecting it to do. You're effectively counting i
up and down in the same iteration, which causes an infinite loop and always yields the same index being passed to LoadMauiAssetLessons()
. You should be using i-1
instead of i--
here:
for (int i = 1; i < 18; i++)
{
LoadMauiAssetLessons($"{i}.json", i-1);
}
Finally, your code should look something like this:
internal class Repo
{
public Lessons[] LessonsList = new Lessons[17];
public async Task RepositoryVoid()
{
for (int i = 1; i < 18; i++)
{
await LoadMauiAssetLessons($"{i}.json", i-1);
}
}
private async Task LoadMauiAssetLessons(string Path, int i)
{
try
{
using var stream = await FileSystem.OpenAppPackageFileAsync(Path);
using var reader = new StreamReader(stream);
var contents = reader.ReadToEnd();
if (JsonSerializer.Deserialize<Lessons>(contents) != null)
{
LessonsList[i] = JsonSerializer.Deserialize<Lessons>(contents);
}
}
catch (FileNotFoundException ex)
{
Debug.WriteLine("File not found :" + ex);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
}