我使用绑定到dataGrid的可观察集合来使用所选客户的库存刷新网格。
我正在循环返回的集合,并使用foreach循环,以便UI了解新项。
请注意,我把这段代码放在一个按钮下只是为了测试,并确保我没有一些时髦的重新进入的东西正在进行。
// Grab the customer ID
int customerID = ((Customer)cboCustomer.SelectedItem).Customer_ID;
ocInventoryItems.Clear();
ObservableCollection<InventoryItem> ocTemp = Inventory.RetrieveObservableCollection(customerID);
foreach (InventoryItem item in ocTemp)
{
ocInventoryItems.Add(item);
}网格的XAML在这里:
ItemsSource="{Binding ocInventoryItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 在清单返回并分配给ocInventoryItems观察集之后,我得到一个内存不足的异常。
我读过关于对象的2Gig限制,但我认为我没有达到这个极限。
从我所知道的情况来看,在将集合加载到后置之前,增加的量只有6 6meg左右。(使用GC.GetTotalMemory(true);)
此外,在库存搜索屏幕上也使用相同的查询和集合。该屏幕返回所有库存,没有任何问题。
我还读过关于潜在内存泄漏的观察,所以我尝试使绑定"OneWay“和"OneTime”。仍然没有joy。
我已经在iNotofiyPropertyChanged类的所有非只读属性上实现了InventoryItem。(我读过这样的文章,如果没有它,就会导致OC内存泄漏)
这似乎是一个真正的内存错误,但是,当我选择一个库存较少的客户时,我就会获得成功。
这让我相信在某个地方内存没有被正确释放或者有漏洞..。
我只是搞不懂在哪里。我确实把它作为一项任务运行,但在我找到问题之前,我已经删除了它。
堆栈跟踪:
Stack Trace: at System.Windows.Media.Composition.DUCE.Channel.CreateOrAddRefOnChannel(Object instance, ResourceHandle& handle, ResourceType resourceType)
at System.Windows.Media.Composition.VisualProxy.CreateOrAddRefOnChannel(Object instance, Channel channel, ResourceType resourceType)
at System.Windows.Media.Visual.AddRefOnChannelCore(Channel channel)
at System.Windows.Media.Visual.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel channel)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle)
at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)
at System.Windows.Media.Visual.Render(RenderContext ctx, UInt32 childIndex)
at System.Windows.Media.CompositionTarget.Compile(Channel channel)
at System.Windows.Media.CompositionTarget.System.Windows.Media.ICompositionTarget.Render(Boolean inResize, Channel channel)
at System.Windows.Media.MediaContext.Render(ICompositionTarget resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
at System.Windows.Media.MediaContext.Resize(ICompositionTarget resizedCompositionTarget)
at System.Windows.Interop.HwndTarget.OnResize()
at System.Windows.Interop.HwndTarget.HandleMessage(WindowMessage msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate zzzzzzz发布于 2015-02-18 15:55:31
的简短回答:--这是一个“重绘”的问题。当我调整网格的布局到一个不同的位置屏幕上-没有问题和快速加载。
Long回答(我是如何做到这一点的):我曾经尝试移动数据集,但是没有结果,所以我开始放弃了。下面是我所经历的步骤的简单列表。
1)将数据存储和加载代码放在一个干净的窗口中。效果很好。
2)将数据处理和加载代码放在从我的普通ucBase继承的干净的用户控件中。效果很好。
3)回到原来的用户控件,注释掉一个正在工作的数据集,并将“坏”的那个放在它的位置上。效果很好。
在那之后,我知道问题与布局和重新绘制有关。我只是一步一步地重新组织了我的布局,直到我有了我想要的两个数据栏,并且没有内存错误。对不起,我不能说得更具体,但这是一个复杂的布局与多个网格划分器,数据,等等。
我的理论:--我猜我把我的控件布置得很糟糕,这导致了每次我在可观察的集合中添加一行时,都会发生一件复杂的重绘事情。
与相关的问题:当滚动查看器中的网格以某种方式导致代码锁定时,我以前见过这个问题。我应该猜到这些问题是相似的。
希望这能帮上忙。对于一个简单的解决办法来说,这是一条漫长的道路。
更新:
今晚我偶然发现了一些更清晰的东西。如果dataGrid位于网格行内,行高为“自动”,并且要向数据集中添加许多项,则数据集绑定到集合中(在我的示例中,超过9,000项).wpf会疯狂地使用您的内存,因为它试图在每次添加到集合后对事物进行大小调整。像"1*“这样的简单的恒星大小能让一切都快乐。
发布于 2015-02-17 20:10:24
也许不是修复,但为什么ocTemp
一个开始
ocInventoryItems.Clear();
foreach (InventoryItem item in Inventory.RetrieveEnemerable(customerID))
{
ocInventoryItems.Add(item);
}发布于 2015-02-17 20:14:41
验证InventoryItem没有在尝试的dispose操作期间将其固定在内存中的任何订阅。
https://stackoverflow.com/questions/28569684
复制相似问题