ReSharper Heap Allocations Viewer plugin
This plugin statically analyzes C# code to find all local object allocations happening.
It can be used to reduce number of heap allocations in hot paths of your C# program.
struct Boxing {
void M(string a) {
object obj = 42; // implicit conversion Int32 ~> Object
string path = a + '/' + obj; // implicit conversion Char ~> Object
int code = this.GetHashCode(); // non-overriden virtual method call on struct
string caseA = E.A.ToString(); // the same, virtual call
IComparable comparable = E.A; // valuetype conversion to interface type
Action action = this.M; // delegate from value type method
Type type = this.GetType(); // GetType() call is always virtual
}
enum E { A, B, C }
}…class Delegates {
static void M(string s) {
Action method = M; // non-cached delegate from method group
Action lambda = () => M("a"); // cached delegate from static lambda
Action closure = () => M(s); // non-cached lambda with closure 's'
}
void M() {
Action generic = () => { }; // non-cached, lambda in generic method
Action closure = () => M(); // non-cached, captures 'this' to closure
}
}class Closures {
IEnumerable StupidExample(string str) {
// ();
if (str.Length == 0) {
return Enumerable.Empty();
} else {
// x == '_');
return Enumerable
.Range(0, str.Length)
.Select(count => // delegate instance allocation
str.Substring(Math.Max(value, count)));
}
}
}No open issues yet, or sync has not completed.