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

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

类页面(后面的代码):

public partial class PageMusicPlayerView : TabbedPage
{
    public PageMusicPlayerView()
    {
        InitializeComponent();
        PageMusicPlayerViewModel viewModel = PageMusicPlayerViewModel.GetInstance();
        BindingContext = viewModel;
    }

    protected override async void OnAppearing()
    {
        await PageMusicPlayerViewModel.GetInstance().InitAsync();
    }

}

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

}

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

类 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 类,其他所有内容都简化为最小示例。运行应用程序时出现错误。此外,调试器开始冻结很多:

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 中进行调试。如果我在不调试的情况下运行调试版本,一切都会正常且快速。为什么会这样? Android 10、华为P30。

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

回答1

具有 8000 个元素的 ObservableCollection 的极度滞后是由于 Hot Reload 造成的。

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/hot-reload解决了性能问题。

相似文章

随机推荐

最新文章