gpt4 book ai didi

WPF ListView RAM 大幅增加

转载 作者:可可西里 更新时间:2023-11-01 10:41:02 25 4
gpt4 key购买 nike

我有一个包含 100 个项目的列表。我必须每 1 分钟刷新一次列表。每次我更新列表 - RAM 都会增加。

private BindableCollection<MyObject> _list = new BindableCollection<MyObject>()

正如我所说,我必须每 1 分钟更新一次我的列表,所以我有一个每 1 分钟触发一次的事件。

private void OnListChanged(List<MyObject> list) {
_list.Clear();
_list.Addrange(list);
}

事件发生后,RAM 增加大约 10-20mb。问题是 RAM 永远不会减少......看起来 GC 不收集垃圾。

我尝试只更新显示前 10 个项目并只更新前 10 个项目,RAM 仍在增加,但速度较慢。

对于 View ,我使用的是 ListView:

<ListView
BorderThickness="0"
ItemTemplate="{StaticResource ItemTemplate}"
ItemsSource="{Binding ItemView}"
Focusable="False" />

我这样试过:

<ListView
ScrollViewer.CanContentScroll="True"
VirtualizingPanel.CacheLength="15"
VirtualizingPanel.IsVirtualizing="True"
VirtualizingPanel.CacheLengthUnit="Item"
VirtualizingPanel.VirtualizationMode="Recycling"
BorderThickness="0"
ItemTemplate="{StaticResource ItemTemplate}"
ItemsSource="{Binding ItemView}"
Focusable="False" />

但是虚拟化也没有帮助...

如果我在运行代码之前评论 ListView ,我会看到什么。 RAM 不再增加,所以这肯定是 Bindings 的问题......

谁能帮我解释一下问题出在哪里?

附言如果相关,我正在使用 Caliburn.Micro。

编辑

附言我尝试手动运行 GC,但没有任何反应。

P.P.P.S.我尝试使用内存分析器进行监控,但每个人都告诉我这是非托管内存,我不知道下一步该怎么做...

编辑

我发现了什么:

Yes, using List.Clear() clears the references to all objects in the list and sets the Count property to 0. It does not release the reference to the underlying array that stores the object references. You can set the Capacity property to 0 after clearing it to release the reference to that array.

If you actually have OOM problems then doing this is not exactly a cure, it can create more address space fragmentation trouble. Check this answer for hints on how to avoid creating too much garbage in the Large Object Heap.

但 BindableCollection 不具备清除容量的能力或可能性。

编辑

我退订了我订阅的所有事件,并通过单击按钮更新了我的列表。还是一样的问题...

最佳答案

我有一个类似的项目(cockpit builder),我在其中绑定(bind)了很多不同的 View 检查关联的 View 模型和 View 是否从内存中释放的解决方案:

你可以像这样放置析构函数:我在输出 View 中显示消息,但你可以设置断点进行调试

    ~ListViewModel()
{
System.Diagnostics.Debug.WriteLine("Listviewmodel released");
}

ListView.xaml.cs 中的列表也是如此

    ~ListView()
{
System.Diagnostics.Debug.WriteLine("Listview released");
}

如果您正在使用 EventAggregator 事件,如果您订阅了一个事件,请不要忘记取消订阅。如果您不这样做,则不会发布 View 和 View 模型。

如果您怀疑 bindcollection 使用大量 ram,为什么不这样做?:

private void OnListChanged(List<MyObject> list) {
_list.Clear();
_list = new BindableCollection<MyObject>()
_list.Addrange(list);
}

关于WPF ListView RAM 大幅增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57575189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com