Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools to find boxing in code

Background: I'm developing for the xbox and am at the optomising stage. I need to cut down on object allocations. One place to start is finding out where (un)boxing occurs.

I'm very new to IL (in that i've never looked at any!) and would like to avoid running DLLs through a dissassembler, hunting for the (un)box command then trying to work out what line of code it relates to.

Question: Are there any tools which will report this kind of thing (where (un)boxing occurs) for me?

EDIT: Made request below into its own question since it's fairly distinct from this one.

Many, many, bonus points for a way of tieing a GC heap dump back to lines of code where the object creations occured!!

like image 640
George Duckett Avatar asked Jun 20 '11 20:06

George Duckett


People also ask

Which of the following codes is boxing?

Boxing In C#The process of converting a Value Type variable (char, int etc.) to a Reference Type variable (object) is called Boxing.

What is boxing in coding?

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type. When the common language runtime (CLR) boxes a value type, it wraps the value inside a System. Object instance and stores it on the managed heap. Unboxing extracts the value type from the object.

What is boxing in .NET Mcq?

When a value type is converted to object type, it is called boxing.

What is unboxing in C#?

Unboxing is the explicit conversion process. Here, the value stored on the stack copied to the object stored on the heap memory. Here, the object stored on the heap memory copied to the value stored on the stack . Example: // C# program to illustrate Boxing.


2 Answers

This MSDN Magazine article details how to create an FxCop (Code Analysis) rule that detects boxing and unboxing and can present violations as a warning. The article is a little on the old side, but you should be able to adapt it to your needs.

like image 162
Adam Maras Avatar answered Oct 30 '22 16:10

Adam Maras


There is a tool called BoxCop which does exactly that.

It is not really useful when trying to integrate the checks for boxing/unboxing into the build process. For that you would need some rule for FxCop.

like image 22
caldis Avatar answered Oct 30 '22 15:10

caldis