Class declaration inside loops
-
Hello, I would like to know what happens to a class when I declare it inside a loop. Is it eliminated by the GC when the cycle ends, before a new instance of the same is created? An example: namespace Whatever { class Model { my fields and methods } class Program { static void Main { Var declaration ... while (condition) { Model temp = new Model(); ... ... } } } } Am I doing it correctly? Thanks in advance for the attention.
-
Hello, I would like to know what happens to a class when I declare it inside a loop. Is it eliminated by the GC when the cycle ends, before a new instance of the same is created? An example: namespace Whatever { class Model { my fields and methods } class Program { static void Main { Var declaration ... while (condition) { Model temp = new Model(); ... ... } } } } Am I doing it correctly? Thanks in advance for the attention.
It is not garbage collected immediately, but it is flagged for garbage collection. When the next garbage collection occurs, it may (or may not!) be disposed (and then queued for some other things like finalization), depends on the GC to decide when/what to do with it. Classes that use a lot of resources should implement the Dispose design pattern so that they can be explicitly disposed of in the code, releasing expensive resources such as images, file handles, database recordsets, etc... instead of relying on the GC to eventually get to them. If you are allocating expensive resources in a tight loop, it is imperitive that you dispose the objects when you are done using them, or memory usage will explode.
-
It is not garbage collected immediately, but it is flagged for garbage collection. When the next garbage collection occurs, it may (or may not!) be disposed (and then queued for some other things like finalization), depends on the GC to decide when/what to do with it. Classes that use a lot of resources should implement the Dispose design pattern so that they can be explicitly disposed of in the code, releasing expensive resources such as images, file handles, database recordsets, etc... instead of relying on the GC to eventually get to them. If you are allocating expensive resources in a tight loop, it is imperitive that you dispose the objects when you are done using them, or memory usage will explode.
-
It is not garbage collected immediately, but it is flagged for garbage collection. When the next garbage collection occurs, it may (or may not!) be disposed (and then queued for some other things like finalization), depends on the GC to decide when/what to do with it. Classes that use a lot of resources should implement the Dispose design pattern so that they can be explicitly disposed of in the code, releasing expensive resources such as images, file handles, database recordsets, etc... instead of relying on the GC to eventually get to them. If you are allocating expensive resources in a tight loop, it is imperitive that you dispose the objects when you are done using them, or memory usage will explode.
PhilDanger wrote:
t is imperitive that you dispose the objects when you are done using them, or memory usage will explode.
That is true, and I was thinking along the lines of performance hit, as well.
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
It is not garbage collected immediately, but it is flagged for garbage collection. When the next garbage collection occurs, it may (or may not!) be disposed (and then queued for some other things like finalization), depends on the GC to decide when/what to do with it. Classes that use a lot of resources should implement the Dispose design pattern so that they can be explicitly disposed of in the code, releasing expensive resources such as images, file handles, database recordsets, etc... instead of relying on the GC to eventually get to them. If you are allocating expensive resources in a tight loop, it is imperitive that you dispose the objects when you are done using them, or memory usage will explode.
Going a bit deeper, if I have namespace Whatever { class Base { ... } class Model { List myList = new List(); my fields and methods } class Program { static void Main { Var declaration ... while (condition) { Model temp = new Model(); ... ... } } } } Do I have to implement in the Base class the :Idisposable interface? I've tried to implement it on Model class, and I have an error at execution, that has something to do with infinite loops, when the Model.Dispose() method is called. Thanks for your quick answer, previously.
-
Going a bit deeper, if I have namespace Whatever { class Base { ... } class Model { List myList = new List(); my fields and methods } class Program { static void Main { Var declaration ... while (condition) { Model temp = new Model(); ... ... } } } } Do I have to implement in the Base class the :Idisposable interface? I've tried to implement it on Model class, and I have an error at execution, that has something to do with infinite loops, when the Model.Dispose() method is called. Thanks for your quick answer, previously.
No. A List is a managed object, so you don't need to implement IDisposable to take care of that. You only need IDisposable to handle unmanaged resources. When the reference to the object is not used any more, the object, the List inside the object, and every object in the List are all collectable (unless the object are referenced elsewhere, of course). The garbage collector doesn't look for object to collect, it looks for the objects to keep. That means that at the instant that an object gets collectable, all objects that it contains are also collectable.
--- single minded; short sighted; long gone;
-
No. A List is a managed object, so you don't need to implement IDisposable to take care of that. You only need IDisposable to handle unmanaged resources. When the reference to the object is not used any more, the object, the List inside the object, and every object in the List are all collectable (unless the object are referenced elsewhere, of course). The garbage collector doesn't look for object to collect, it looks for the objects to keep. That means that at the instant that an object gets collectable, all objects that it contains are also collectable.
--- single minded; short sighted; long gone;
Let's say you have a List (or something else that is disposable). You will want to give your Base class a Dispose() method so that you can loop through the List and Dispose of everything manually. Basically, if your class contains unmanaged resources OR contains members that implement IDisposable, you'll want your class to implement it as well. --sorry, meant this as a reply to OP.