'Explicit concurrent copying GC freed, Xamarin.Forms
I want to create the data model asynchronously. I am using MVVM. My code looks like this.
class Page (code behind):
public partial class PageMusicPlayerView : TabbedPage
{
public PageMusicPlayerView()
{
InitializeComponent();
PageMusicPlayerViewModel viewModel = PageMusicPlayerViewModel.GetInstance();
BindingContext = viewModel;
}
protected override async void OnAppearing()
{
await PageMusicPlayerViewModel.GetInstance().InitAsync();
}
}
class PageMusicPlayerViewModel (view model):
public class PageMusicPlayerViewModel : NotifyProperty
{
private static PageMusicPlayerViewModel instance = null;
public static PageMusicPlayerViewModel GetInstance()
{
if (instance == null)
{
instance = new PageMusicPlayerViewModel();
}
return instance;
}
public MusicPlaylistModel MusicPlaylist { get; set; } = MusicPlaylistModel.GetInstance();
public async Task InitAsync()
{
await MusicPlaylist.InitAsync();
}
}
class MusicPlaylistModel (model):
public class MusicPlaylistModel : NotifyProperty
{
private static MusicPlaylistModel instance = null;
ObservableCollection<MusicItemModel> _listMusic;
public ObservableCollection<MusicItemModel> ListMusic
{
get { return _listMusic; }
set { SetProperty(ref _listMusic, value); }
}
public static MusicPlaylistModel GetInstance()
{
if(instance == null)
{
instance = new MusicPlaylistModel();
}
return instance;
}
private MusicPlaylistModel()
{
ListMusic = new ObservableCollection<MusicItemModel>();
}
public async Task InitAsync()
{
int size = 8000;
for(int i = 0;i<size;i++)
{
ListMusic.Add(new MusicItemModel { SoundName = "TEST" });
}
}
}
class MusicItemModel (model):
public class MusicItemModel : NotifyProperty
{
long _id = -1;
string _soundName = string.Empty;
string _soundDuration = string.Empty;
bool _cached = false;
string _downloadingLabel = string.Empty;
public long Id
{
get { return _id; }
set { SetProperty(ref _id, value); }
}
public string SoundName
{
get { return _soundName; }
set { SetProperty(ref _soundName, value); }
}
public string SoundDuration
{
get { return _soundDuration; }
set { SetProperty(ref _soundDuration, value); }
}
public bool Cached
{
get { return _cached; }
set { SetProperty(ref _cached, value); }
}
[NotMapped]
public string DownloadingLabel
{
get { return _downloadingLabel; }
set
{
SetProperty(ref _downloadingLabel, value);
}
}
}
Model ListMusic is bound to a view.
I provide the MusicItemModel class as is, everything else is reduced to a minimal example.
I am getting errors while running the application. Also, the debugger starts to freeze a lot:
Explicit concurrent copying GC freed 9420(1219KB) AllocSpace objects, 0(0B) LOS objects, 71% free, 2406KB/8550KB, paused 33us total 10.883ms
Explicit concurrent copying GC freed 717(39KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 24us total 8.730ms
Explicit concurrent copying GC freed 15(48KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 20us total 8.058ms
Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 24us total 8.521ms
Explicit concurrent copying GC freed 6(32KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 24us total 8.279ms
Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 22us total 8.190ms
Explicit concurrent copying GC freed 3(16KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 22us total 8.067ms
Explicit concurrent copying GC freed 32(48KB) AllocSpace objects, 0(0B) LOS objects, 72% free, 2382KB/8526KB, paused 22us total 7.973ms
Errors are only under debugging in Visual Studio. If I run a debug build without debugging everything works fine and fast. Why is this happening? Android 10, Huawei P30.
P.S.:
If I reduce the size of the variable size to 1000 and below, then the problems go away. But the amount of my data in the real sample is 8276 elements.
Solution 1:[1]
The extreme lagginess with an ObservableCollection having 8000 elements, was due to Hot Reload.
UNCHECK Hot Reload in Tools/Options resolved the performance issue.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ToolmakerSteve |
