OpenLoco / ObjectEditor

A modern implementation of 'LocoTool' for dat file parsing
7 stars 3 forks source link

Investigate huge memory usage #53

Closed LeftofZen closed 3 months ago

LeftofZen commented 3 months ago

Users are reporting huge (27gb!) memory usage when loading folders and viewing them. This is insane, and whilst I don't get the same issue on my pc (only 700mb usage) I believe the AvaloniaUI TreeView is the issue as it doesn't virtualise the list items. This is confirmed in the memory profiler as all the newly-allocate ram is from Avalonia. This performance page indicates switching to TreeDataGrid may help as it virtualises items.

LeftofZen commented 3 months ago

I have also found that recreating the index simply increases memory usage, meaning the previous index is being leaked. Time to rewrite how it works since I just ported it from the winforms version without thinking about mvvm

LeftofZen commented 3 months ago

For those interested, the main cause here was that I was using a static dictionary to store objects, and this dictionary was never cleared, ever. This is why recreating indexes always consumed more memory - it would just get added to without removing anything. The original reason for this dictionary was that vehicles needed to look up cargo types to load correctly, so you needed to load cargo types first and have a reference to them globally (or otherwise pass to the vehicles), and this is how I did it. However the vehicle loading code has gone through multiple iterations and at some stage I implemented a better solution, meaning the static global dictionary was no longer necessary, but I forgot to delete it's usage. Removing all of that has removed the main memory issue as far as I can tell.