4

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vesuvian
  • 697
  • 1
  • 7
  • 22

2 Answers2

4

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.:

enter image description here

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".

Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
  • This is looking excellent. Is there any way to output all of the instances of object allocations? For example, in a format like the code issue explorer. – Vesuvian Sep 24 '14 at 19:24
  • You should be able to alt+enter on an allocation and show all similar code issues. – citizenmatt Sep 24 '14 at 22:13
  • I gave that a go, it didn't seem to work for me. I don't know of a way to output all of the instances of object allocations. Perhaps that would make a good feature request? We used the plug-in after profiling, so we knew approximately where our excessive allocations were occurring. – Chris Mantle Sep 25 '14 at 08:51
  • 1
    I've been using the module and scanning through each script by eye. It's a bit of a pin, but it's taught me some very interesting things. For example, I had NO idea that calling params[] methods instantiated a new array every time. – Vesuvian Sep 25 '14 at 22:23
0

[2021 Update]

It looks like the Resharper heap allocation viewer extension has been merged into Resharper Usages of Symbol generic tool.

If you are using Resharper in 2021 and look for instantiation of a class, do the following steps :

  • Go to the class definition and query Usages of Symbol, cf. screenshot below
  • Click Show in Find Results for better readability
  • Filter results by New instance creation

That's it, all your instantiations are now listed.

enter image description here

XavierAM
  • 1,627
  • 1
  • 14
  • 30