Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Class declaration inside loops

Class declaration inside loops

Scheduled Pinned Locked Moved C#
tutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rsaint27
    wrote on last edited by
    #1

    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.

    P 1 Reply Last reply
    0
    • R rsaint27

      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.

      P Offline
      P Offline
      PhilDanger
      wrote on last edited by
      #2

      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.

      R P 3 Replies Last reply
      0
      • P PhilDanger

        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.

        R Offline
        R Offline
        rsaint27
        wrote on last edited by
        #3

        Thanks! :)

        1 Reply Last reply
        0
        • P PhilDanger

          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.

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • P PhilDanger

            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.

            R Offline
            R Offline
            rsaint27
            wrote on last edited by
            #5

            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.

            G 1 Reply Last reply
            0
            • R rsaint27

              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.

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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;

              P 1 Reply Last reply
              0
              • G Guffa

                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;

                P Offline
                P Offline
                PhilDanger
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups