Reputation: 1104
I'm using an observable collection bound to a dataGrid to refresh the grid with the selected customer's Inventory.
I'm looping through the collection returned and using a foreach loop so that the UI knows about the new items.
Note that I put this code under a button just for testing and to make sure I didn't have some funky re entrant thing going.
// 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);
}
The XAML for the grid is here:
ItemsSource="{Binding ocInventoryItems, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
After the inventory is returned and assigned to the ocInventoryItems observablecollection I get an out of memory exception.
I've read about the 2Gig limit on objects, but I don't think I'm hitting that.
The increase before loading the collection to after is only about 6meg from what I can tell. (Using GC.GetTotalMemory(true);)
Also the same query and collection is used on the Inventory search screen. That screen returns ALL inventory with no problems.
I've also read about potential memory leaks with observablecollections, so I've tried to make the binding "OneWay" and "OneTime". Still no joy.
I have implemented iNotofiyPropertyChanged on all of my non-read-only properties of the InventoryItem class. (I've read that not having that can cause memory leaks with OC)
It does seem to be a true out of memory error though, when I choose a customer with less inventory, I get success.
That leads me to believe that somewhere memory is not being freed up correctly or there is a leak...
I'm just stumped as to where. I did have this running as a task, but removed that for now until I can track down the problem.
Stack Trace:
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
Upvotes: 0
Views: 1323
Reputation: 1104
Short Answer: This was a "redraw" kind of issue. When I tweaked the layout of the grid to a different location on the screen - no problems and fast loading.
Long Answer (How did I get to this): I had tried moving the datagrid around a bit to no avail, so I had initially given up on this. Here is a quick list of steps I went through.
1) put the datagrid and loading code in a clean window. Worked perfectly.
2) put the datagrid and loading code in a clean user control inherited from my common ucBase. Worked perfectly.
3) Went back to the original user control, commented out a datagrid that was working and placed the "bad" one in its place. Worked perfectly.
After that I knew the problem had to do with layout and redrawing. I just reorganized my layout one step at a time until I had both datagrids where I wanted them and no memory errors. Sorry I can't be more specific, but it's a complicated layout with multiple gridsplitters, datagrids, etc.
My Theory: I'm guessing that I had laid out my controls badly, which was causing a complicated redraw kind of thing to happen each time I added a row to the observable collection.
Related Problems: I have seen this problem before when a grid inside a scrollviewer somehow caused code to lock up. I should have guessed that these problems were similar.
Hope this helps someone. It was a long road for a simple fix.
Update:
Some added clarity I stumbled on tonight. If the dataGrid is within a grid row and the row Height is "Auto" and you are adding many items to the collection that the datagrid is bound to (over 9,000 in my case)... wpf goes crazy with your memory as it tries to size things after every add to the collection. A simple star size in height like "1*" makes everything happy.
Upvotes: 3
Reputation: 31616
Verify that InventoryItem
does not have any subscriptions which are pinning it to memory during an attempted dispose operation.
Upvotes: 0
Reputation: 45096
Probably not the fix but why ocTemp
A start
ocInventoryItems.Clear();
foreach (InventoryItem item in Inventory.RetrieveEnemerable(customerID))
{
ocInventoryItems.Add(item);
}
Upvotes: 1