0

One of my friend told me that dynamic method is the only way if you want to at run time construct code that can be garbage collected.

One question in my mind that how garbage collector garbage the object which generated using reflection?

ashes999
  • 9,925
  • 16
  • 73
  • 124
Pankaj
  • 4,419
  • 16
  • 50
  • 72

5 Answers5

5

An object constructed using reflection will be garbage collected like any other type of object, for example when it leaves the scope of a method if it's a method variable.

Gerrie Schenck
  • 22,148
  • 20
  • 68
  • 95
  • ok @@Gerrie Schenck but who will decide that code is manage or unmanaged? Is it decided by CRL? – Pankaj Dec 29 '09 at 11:50
  • 1
    Reflection is managed. And unmanaged resources will never be cleaned up by the garbage collector, since they don't live in the managed heap. – Gerrie Schenck Dec 29 '09 at 11:55
  • Thanks @@Gerrie Schenck, but again my question is that who decide that code is manage or unmaged – Pankaj Dec 29 '09 at 11:57
  • 2
    Any .NET object is managed in this context. If its written in a .NET language, compiled using a .NET compiler and lives within a .NET assembly... – Arjan Einbu Dec 29 '09 at 12:01
  • 1
    ya right. I don't understand the confusion here. If you create a managed type using reflection it will be handled by Garbage collector, an unmanaged type will be handled differently. Once the code is generated and loaded it behaves the same as any other .NET code. – A9S6 Dec 29 '09 at 12:12
  • Really very-2 Thanks @@Arjan.. If i use some 3rd party .dll in my manage code, which consist of unmanaged code.In this condition my manage object will call unmanage dobject. That how CRL will handle this condition... – Pankaj Dec 29 '09 at 12:35
4

Garbage collection will collect any .NET objects. It doesn't differ wether they are created using reflection or not.

Arjan Einbu
  • 13,543
  • 2
  • 56
  • 59
  • Really very-2 Thanks @@Arjan.. If i use some 3rd party .dll in my manage code, which consist of unmanaged code.In this condition my manage object will call unmanage dobject. That how CRL will handle this condition... – Pankaj Dec 29 '09 at 12:08
  • To use .NET reflection and the CLR, the objects will need to at least have a .NET wrapper. GC cleans up after the wrappers, but the wrappers should clean up after themselves, maybe in the finalizer... I'd expect that this is taken care of with the COM wrappers created by .NET... – Arjan Einbu Dec 29 '09 at 19:15
1

how should an object created by reflection be different than one created normally?

you have an instance variable of this object... the runtime exactly knows what type of object it is, and also does the GC.

only the way the object is created is different. the object should be exactly the same as one created with new MyObject()

Atmocreations
  • 9,923
  • 15
  • 67
  • 102
1

There is more complexity than this. For example, search for Tess (good MS employee) blog about xml serializer leaking memory. There are ways to fix this in code pattern. Anyway, this xml serializer memory leak problem won't be fixed by GC. In fact, these types of dynamically generated dll only get deleted when the parent app is unloaded (IIS worker process that hosts web service that use xml serializer).

Bottom line: even for .Net project, don't rely on GC for all cases. There are leaks that need work-around code/code pattern fix.

This bug is still in 3.5 I think.

More link: http://plainoldstan.blogspot.com/2011/04/wcf-memory-leak-with.html read the Tess link an other link inside this: Are there still known memory leaks with XMLSerialization in .Net 3.5?

Community
  • 1
  • 1
0

Any piece of code that runs under the control of CLR is managed code and CLR governs and decides for garbage collection. there are ways in which you can coltrol the Garbage collection but unless it is really required let CLR decide.

Vinay Pandey
  • 8,589
  • 9
  • 36
  • 54