Search code examples
c#performancecustom-attributes

Is usage of Custom Attributes in C# is memory/performance bottleneck?


I wanna decorate my methods and GUI controls with custom attributes. I want to know how attributes consume memory or affect the application performance. What is the life-cycle of attributes. Means when an object of a class with custom attributes in methods, properties and on its own. Is instantiated and then disposed. If all custom attributes instances are also disposed with the deconstruction of object, or do they still remain in memory?


Solution

  • Attributes exist on the types, not the object instances and thus the lifetime of an attribute instance is not related to the lifetime of an object instance.

    Heavy use of attributes in code can be a bottleneck if you use reflection repeatedly to access the same attributes in heavily used code. This is easily remedied by caching. Exactly how and when you cache will depend on your application and environment.

    In general though, attributes provide a great solution to a common programming scenario and when used properly will not cause a memory or performance bottleneck.