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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Garbage Collector

Garbage Collector

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpcomperformancehelptutorial
5 Posts 3 Posters 1 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.
  • H Offline
    H Offline
    Het2109
    wrote on last edited by
    #1

    I couldn't find any sample where they have shown the exact use of calling garbage collector explicitely. For example what I think is if I am using Interop, say object of Excel file in .Net, it is good to call GC.Collect to free up the memory. Is what i think correct? What are the other scenarios when we should use Garbage Collector ? Thanks for your help in advance.

    Het Waghela :)Be Humble in Victory and Strong in Defeat.:) Het Waghela, Blog|Het Waghela DotNet Questions Link|More Links

    S 1 Reply Last reply
    0
    • H Het2109

      I couldn't find any sample where they have shown the exact use of calling garbage collector explicitely. For example what I think is if I am using Interop, say object of Excel file in .Net, it is good to call GC.Collect to free up the memory. Is what i think correct? What are the other scenarios when we should use Garbage Collector ? Thanks for your help in advance.

      Het Waghela :)Be Humble in Victory and Strong in Defeat.:) Het Waghela, Blog|Het Waghela DotNet Questions Link|More Links

      S Offline
      S Offline
      Scott Dorman
      wrote on last edited by
      #2

      Het2109 wrote:

      it is good to call GC.Collect to free up the memory.

      It's almost never a good idea to call GC.Collect directly. Just because you are using Interop, it will depend on what you are doing. If you are directly allocating memory or translating structures to pointers (or back again) using any of the methods on the Marshal class then you need to be very careful about allocating and deallocating memory using the appropriate methods on the Marshal class.

      Het2109 wrote:

      What are the other scenarios when we should use Garbage Collector ?

      Any time you are writing managed code you're using the garbage collector. If the class you are using implements the IDisposable interface (or provides a Dispose method) you should call Dispose as soon as you can after you're done using that class. See this article[^] for more information.

      Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


      [Forum Guidelines] [Articles] [Blog]

      H M 2 Replies Last reply
      0
      • S Scott Dorman

        Het2109 wrote:

        it is good to call GC.Collect to free up the memory.

        It's almost never a good idea to call GC.Collect directly. Just because you are using Interop, it will depend on what you are doing. If you are directly allocating memory or translating structures to pointers (or back again) using any of the methods on the Marshal class then you need to be very careful about allocating and deallocating memory using the appropriate methods on the Marshal class.

        Het2109 wrote:

        What are the other scenarios when we should use Garbage Collector ?

        Any time you are writing managed code you're using the garbage collector. If the class you are using implements the IDisposable interface (or provides a Dispose method) you should call Dispose as soon as you can after you're done using that class. See this article[^] for more information.

        Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


        [Forum Guidelines] [Articles] [Blog]

        H Offline
        H Offline
        Het2109
        wrote on last edited by
        #3

        Thanks Scott for your reply and article, it really helped me get rid of my confusion and get to know more of GC.

        Het Waghela :)Be Humble in Victory and Strong in Defeat.:) Het Waghela, Blog|Het Waghela DotNet Questions Link|More Links

        S 1 Reply Last reply
        0
        • H Het2109

          Thanks Scott for your reply and article, it really helped me get rid of my confusion and get to know more of GC.

          Het Waghela :)Be Humble in Victory and Strong in Defeat.:) Het Waghela, Blog|Het Waghela DotNet Questions Link|More Links

          S Offline
          S Offline
          Scott Dorman
          wrote on last edited by
          #4

          Het2109 wrote:

          Thanks Scott for your reply and article, it really helped me get rid of my confusion and get to know more of GC.

          Glad to help.

          Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


          [Forum Guidelines] [Articles] [Blog]

          1 Reply Last reply
          0
          • S Scott Dorman

            Het2109 wrote:

            it is good to call GC.Collect to free up the memory.

            It's almost never a good idea to call GC.Collect directly. Just because you are using Interop, it will depend on what you are doing. If you are directly allocating memory or translating structures to pointers (or back again) using any of the methods on the Marshal class then you need to be very careful about allocating and deallocating memory using the appropriate methods on the Marshal class.

            Het2109 wrote:

            What are the other scenarios when we should use Garbage Collector ?

            Any time you are writing managed code you're using the garbage collector. If the class you are using implements the IDisposable interface (or provides a Dispose method) you should call Dispose as soon as you can after you're done using that class. See this article[^] for more information.

            Scott. —In just two days, tomorrow will be yesterday. —Hey, hey, hey. Don't be mean. We don't have to be mean because, remember, no matter where you go, there you are. - Buckaroo Banzai


            [Forum Guidelines] [Articles] [Blog]

            M Offline
            M Offline
            Mark Churchill
            wrote on last edited by
            #5

            Yeah it can be counterproductive to call GC.Collect directly. IIRC one side effect is that objects that survive collection will move quicker into higher generations, and be less likely to be looked at in following collections.

            Mark Churchill Director Dunn & Churchill Free Download:
            Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.

            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