2

I want to release all the attribute's from memory.

Which reference I have to break so these attributes will get cleaned from memory?.

I'm not finding any references at all with these attributes in my application but still they are not releasing.

    [System.AttributeUsage(System.AttributeTargets.Property)]
    public class AttributeEx : System.Attribute
    {
        ....
    }

Reference Chain : enter image description here

Amol Bavannavar
  • 2,062
  • 2
  • 15
  • 36
  • 3
    You might as well ask how to force your _code_ to be garbage-collected. The attributes are an integral part of your assembly; GC is for data dynamically-allocated at run-time. – Peter Duniho May 07 '15 at 08:08
  • Yes @PeterDuniho... Understood.. then, How can I remove them from memory? – Amol Bavannavar May 07 '15 at 08:10
  • 1
    Why are you concerned about it? Use a memory profiler and find who holds the references of the attributes. – Sriram Sakthivel May 07 '15 at 08:11
  • Answer: See comment of @PeterDuniho above – DrKoch May 07 '15 at 08:12
  • @PeterDuniho Instances of an attribute *are* dynamically allocated at runtime, just like any other type. – dcastro May 07 '15 at 08:13
  • @SriramSakthivel Yes I did that with ANTS Memory Profiler and I have not found any releation's with these attributes... – Amol Bavannavar May 07 '15 at 08:14
  • @dcastro Not all attributes. For example security attributes are loaded by CLR itself. Not sure if it keeps a reference to it (it shouldn't though ) – Sriram Sakthivel May 07 '15 at 08:15
  • @dcastro: thanks...maybe I misunderstand the question, as the attribute _instances_ that are returned (e.g. via reflection) seem obviously GC-able to me. I figured the OP was asking about the attribute descriptions in the IL. Maybe the question just isn't clear enough for me to understand it. – Peter Duniho May 07 '15 at 08:25
  • @SriramSakthivel Please check the reference chain... – Amol Bavannavar May 07 '15 at 09:27

2 Answers2

3

Which reference I have to break so these attributes will get cleaned from memory?.

Instances of attributes are created only when you inspect them, not when you create instances of types decorated with those attributes. E.g.

var attrs = typeof(ClassWithAttribute).GetCustomAttributes();

So long as attrs is a local variable, the attribute will have a really short lifespan and soon be eligible for garbage collection.

Note that these attributes are instantiated each and every time you call GetCustomAttributes.

dcastro
  • 66,540
  • 21
  • 145
  • 155
3

Attributes exist in GC memory only when you ask for them (see for example When is a custom attribute's constructor run?):

[Obsolete]
public class MyClass
{

}

// Here the ObsoleteAttribute is created
var att1 = typeof(MyClass).GetCustomAttributes(false)[0];

// Here the ObsoleteAttribute is created again
var att2 = typeof(MyClass).GetCustomAttributes(false)[0];

// false!
bool refeq = object.ReferenceEquals(att1, att2);

So if you don't ask for them, they aren't created. And even when you ask for them, they can be GC collected when they aren't referenced anymore (so after the bool line in this case)

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280