显式并发复制 GC 已释放,Xamarin.Forms

Explicit concurrent copying GC freed, Xamarin.Forms

我想异步创建数据模型。我正在使用 MVVM。 我的代码看起来像这样。

class 页面(代码隐藏):

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(视图模型):

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(模型):

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(型号):

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); 
        }
    }

}

模型 ListMusic 已绑定到视图。 我按原样提供 MusicItemModel class,其他所有内容都简化为最小示例。 我在 运行 应用程序时遇到错误。此外,调试器开始大量冻结:

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

错误仅在 Visual Studio 中正在调试中。 如果我 运行 一个没有调试的调试构建,一切都运行良好且快速。为什么会这样? Android10、华为P30

P.S.: 如果我将变量 size 的大小减小到 1000 及以下,那么问题就会消失。但是我在真实样本中的数据量是8276个元素。

具有 8000 个元素的 ObservableCollection 的极度延迟是由于 Hot Reload

UNCHECK Hot Reload in Tools/Options 解决了性能问题。