I'm developing a C# Mono project (in Unity3D) which is using an older version of the garbage collector. Memory leaks have been an unpleasant wake-up call for me.
Because of this I am optimizing as much as I possibly can: avoiding Linq like the plague, recycling collections, etc.
What I would like to know is - is there a tool that will let me know all of the places I am instantiating new objects?
I've been able to grep for things like new List
which is fine, but if I want to expand my search to new
I also get value types, which I want to ignore.
I'm using MonoDevelop, Visual Studio 2013 and Resharper.
There's a relatively new little plug-in that JetBrains released for ReSharper that statically analyses your code for allocations and boxing - the ReSharper Heap Allocations Viewer. It might be exactly what you're looking for. We've had issues with garbage collection, memory usage and performance linked to excessive allocation and over-use of LINQ in performance-critical areas. It can be difficult to track these extra allocations down, as you've found too. This plugin has really helped when we've been refactoring to reduce allocations, and we've kept it on to prevent excessive memory use in new code.
Basically, it adds underlining and an explanation wherever it detects local object allocation, e.g.:
It also flags up LINQ statements, the allocations of iterators and delegates, and allocations caused by delegates.
You can install it from the ReSharper Extension Manager - just search for "heap allocations viewer".