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. Garbage Collection in C#.

Garbage Collection in C#.

Scheduled Pinned Locked Moved C#
17 Posts 9 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.
  • A Offline
    A Offline
    Ashwani Dhiman
    wrote on last edited by
    #1

    After how much time later the garbage collector is called automatically to recycle the objects?How the garbage collector Knows to which object has to be recycled in the memory? And where we have to Call the garbage collector? Example: If i have write a code of 500 lines and define a object in the starting and use the object in second last line of the code. Is the garbage collector recycle the object before its use or not.

    CPalliniC A C L 4 Replies Last reply
    0
    • A Ashwani Dhiman

      After how much time later the garbage collector is called automatically to recycle the objects?How the garbage collector Knows to which object has to be recycled in the memory? And where we have to Call the garbage collector? Example: If i have write a code of 500 lines and define a object in the starting and use the object in second last line of the code. Is the garbage collector recycle the object before its use or not.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      Hey, lmgtfy! :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • A Ashwani Dhiman

        After how much time later the garbage collector is called automatically to recycle the objects?How the garbage collector Knows to which object has to be recycled in the memory? And where we have to Call the garbage collector? Example: If i have write a code of 500 lines and define a object in the starting and use the object in second last line of the code. Is the garbage collector recycle the object before its use or not.

        A Offline
        A Offline
        akhilonly007
        wrote on last edited by
        #3

        hey Ashwani Dhiman what in your case when you create an object in first line and used in last line it does not matter that you are using the object or not in your code, the thing is the memory which is used by the object is referenced by any variable or not i.e accessible or not by your code when memory location is dangling garbage collector will recollect those hope this will help u :-)

        akhilonly007@gmail.com

        modified on Monday, April 6, 2009 6:31 AM

        C 1 Reply Last reply
        0
        • A Ashwani Dhiman

          After how much time later the garbage collector is called automatically to recycle the objects?How the garbage collector Knows to which object has to be recycled in the memory? And where we have to Call the garbage collector? Example: If i have write a code of 500 lines and define a object in the starting and use the object in second last line of the code. Is the garbage collector recycle the object before its use or not.

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Ashwani Dhiman wrote:

          After how much time later the garbage collector is called automatically to recycle the objects?

          When it needs to, or when you ask it (but even then it may not).

          Ashwani Dhiman wrote:

          How the garbage collector Knows to which object has to be recycled in the memory?

          When garbage collection is run it maps all objects in the system. Any that are not referenced somewhere down the object graph from a known root are discarded. Root objects are those that are statically declared.

          Ashwani Dhiman wrote:

          And where we have to Call the garbage collector?

          You don't. It happens when it needs to.

          Ashwani Dhiman wrote:

          If i have write a code of 500 lines and define a object in the starting and use the object in second last line of the code. Is the garbage collector recycle the object before its use or not.

          If it needs to then yes. Otherwise then no.

          *Developer Day Scotland - Free community conference Delegate Registration Open

          S 1 Reply Last reply
          0
          • A akhilonly007

            hey Ashwani Dhiman what in your case when you create an object in first line and used in last line it does not matter that you are using the object or not in your code, the thing is the memory which is used by the object is referenced by any variable or not i.e accessible or not by your code when memory location is dangling garbage collector will recollect those hope this will help u :-)

            akhilonly007@gmail.com

            modified on Monday, April 6, 2009 6:31 AM

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            akhilonly007 wrote:

            hope this will help u

            Please write properly. It is very difficult to read text-speak and not everyone on this forum has a sufficient knowledge of English to interpret poorly written text-speak laiden prose.

            *Developer Day Scotland - Free community conference Delegate Registration Open

            A 1 Reply Last reply
            0
            • C Colin Angus Mackay

              akhilonly007 wrote:

              hope this will help u

              Please write properly. It is very difficult to read text-speak and not everyone on this forum has a sufficient knowledge of English to interpret poorly written text-speak laiden prose.

              *Developer Day Scotland - Free community conference Delegate Registration Open

              A Offline
              A Offline
              akhilonly007
              wrote on last edited by
              #6

              hey Colin Angus Mackay :-( sorry if i hv written incomplete ans, as others on this forum me too poor in writing english but try to give ans as u hv given i.e point to point

              akhilonly007@gmail.com

              C D A 3 Replies Last reply
              0
              • A akhilonly007

                hey Colin Angus Mackay :-( sorry if i hv written incomplete ans, as others on this forum me too poor in writing english but try to give ans as u hv given i.e point to point

                akhilonly007@gmail.com

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                My issue was the use of text-speak which you continue to do. For example: hv = have ans = answer u = you People who do not have a good level of English will be confused by the use of text-speak as it relies on a good level of understanding of the language.

                *Developer Day Scotland - Free community conference Delegate Registration Open

                1 Reply Last reply
                0
                • A akhilonly007

                  hey Colin Angus Mackay :-( sorry if i hv written incomplete ans, as others on this forum me too poor in writing english but try to give ans as u hv given i.e point to point

                  akhilonly007@gmail.com

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Please try to use complete words, it makes it so much easier for everyone.

                  akhilonly007 wrote:

                  sorry if i hv written incomplete ans, as others on this forum me too poor in writing english but try to give ans as u hv given i.e point to point

                  hv = have (x2) ans = answer (x2) u = you

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  1 Reply Last reply
                  0
                  • A akhilonly007

                    hey Colin Angus Mackay :-( sorry if i hv written incomplete ans, as others on this forum me too poor in writing english but try to give ans as u hv given i.e point to point

                    akhilonly007@gmail.com

                    A Offline
                    A Offline
                    akhilonly007
                    wrote on last edited by
                    #9

                    ok thanks for guiding me from now onwards i will not repeat such mistakes :-)

                    akhilonly007@gmail.com

                    1 Reply Last reply
                    0
                    • A Ashwani Dhiman

                      After how much time later the garbage collector is called automatically to recycle the objects?How the garbage collector Knows to which object has to be recycled in the memory? And where we have to Call the garbage collector? Example: If i have write a code of 500 lines and define a object in the starting and use the object in second last line of the code. Is the garbage collector recycle the object before its use or not.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      The garbage colector is indeterministic when it runs. This means there is no set interval or 'state' that will trigger it to run. If you are having a specific issue with the GC then you may want to post that as the GC is a beast that many have tried to tame and many have failed. It usually depends on what you are doing and what you are expecting to happen with your available memory. Some simple stuff about the GC. There are 3 levels of objects. G0, G1, G2, where G2 have been around the longest. Statitistics show if an object gets to G1 it will likely get to G2. G2 Collection is BAD. You can have the GC run in Concurrent (runs in the background) mode or in Server mode (pauses all threads while GC is running). To reiterate again, you need to know what you are wanting from the GC before you start messing with it. Oh and Microsoft claims the the GC in VS 2010 is "Better", whatever the heck that means (I do not recall a time when microsoft released a product and claimed something about it was "worse" or the same for that mattter).

                      C D 2 Replies Last reply
                      0
                      • L Lost User

                        The garbage colector is indeterministic when it runs. This means there is no set interval or 'state' that will trigger it to run. If you are having a specific issue with the GC then you may want to post that as the GC is a beast that many have tried to tame and many have failed. It usually depends on what you are doing and what you are expecting to happen with your available memory. Some simple stuff about the GC. There are 3 levels of objects. G0, G1, G2, where G2 have been around the longest. Statitistics show if an object gets to G1 it will likely get to G2. G2 Collection is BAD. You can have the GC run in Concurrent (runs in the background) mode or in Server mode (pauses all threads while GC is running). To reiterate again, you need to know what you are wanting from the GC before you start messing with it. Oh and Microsoft claims the the GC in VS 2010 is "Better", whatever the heck that means (I do not recall a time when microsoft released a product and claimed something about it was "worse" or the same for that mattter).

                        C Offline
                        C Offline
                        Colin Angus Mackay
                        wrote on last edited by
                        #11

                        Collin Jasnoch wrote:

                        G2 Collection is BAD

                        It isn't bad. It just takes the longest because it is where all the large or aged objects are placed. This means it doesn't have the same performance as a G0 collection. Saying it is bad is like saying an ADSL connection to the internet is bad because you can transfer data from your hard disk faster.

                        Collin Jasnoch wrote:

                        Microsoft claims the the GC in VS 2010 is "Better", whatever the heck that means

                        Technology has moved on. Hardware has changed. Now the garbage collector supports the features of newer hardware that it didn't in the past.

                        *Developer Day Scotland - Free community conference Delegate Registration Open

                        L D 2 Replies Last reply
                        0
                        • C Colin Angus Mackay

                          Collin Jasnoch wrote:

                          G2 Collection is BAD

                          It isn't bad. It just takes the longest because it is where all the large or aged objects are placed. This means it doesn't have the same performance as a G0 collection. Saying it is bad is like saying an ADSL connection to the internet is bad because you can transfer data from your hard disk faster.

                          Collin Jasnoch wrote:

                          Microsoft claims the the GC in VS 2010 is "Better", whatever the heck that means

                          Technology has moved on. Hardware has changed. Now the garbage collector supports the features of newer hardware that it didn't in the past.

                          *Developer Day Scotland - Free community conference Delegate Registration Open

                          L Offline
                          L Offline
                          led mike
                          wrote on last edited by
                          #12

                          Colin Angus Mackay wrote:

                          Technology has moved on

                          WHAT! You mean I'm not typing this into my commodore? :omg: :wtf:

                          1 Reply Last reply
                          0
                          • C Colin Angus Mackay

                            Ashwani Dhiman wrote:

                            After how much time later the garbage collector is called automatically to recycle the objects?

                            When it needs to, or when you ask it (but even then it may not).

                            Ashwani Dhiman wrote:

                            How the garbage collector Knows to which object has to be recycled in the memory?

                            When garbage collection is run it maps all objects in the system. Any that are not referenced somewhere down the object graph from a known root are discarded. Root objects are those that are statically declared.

                            Ashwani Dhiman wrote:

                            And where we have to Call the garbage collector?

                            You don't. It happens when it needs to.

                            Ashwani Dhiman wrote:

                            If i have write a code of 500 lines and define a object in the starting and use the object in second last line of the code. Is the garbage collector recycle the object before its use or not.

                            If it needs to then yes. Otherwise then no.

                            *Developer Day Scotland - Free community conference Delegate Registration Open

                            S Offline
                            S Offline
                            S Senthil Kumar
                            wrote on last edited by
                            #13

                            Colin Angus Mackay wrote:

                            or when you ask it (but even then it may not).

                            Hmm, I thought GC.Collect forces a garbage collection?

                            Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

                            1 Reply Last reply
                            0
                            • C Colin Angus Mackay

                              Collin Jasnoch wrote:

                              G2 Collection is BAD

                              It isn't bad. It just takes the longest because it is where all the large or aged objects are placed. This means it doesn't have the same performance as a G0 collection. Saying it is bad is like saying an ADSL connection to the internet is bad because you can transfer data from your hard disk faster.

                              Collin Jasnoch wrote:

                              Microsoft claims the the GC in VS 2010 is "Better", whatever the heck that means

                              Technology has moved on. Hardware has changed. Now the garbage collector supports the features of newer hardware that it didn't in the past.

                              *Developer Day Scotland - Free community conference Delegate Registration Open

                              D Offline
                              D Offline
                              Dan Neely
                              wrote on last edited by
                              #14

                              Colin Angus Mackay wrote:

                              It isn't bad. It just takes the longest because it is where all the large or aged objects are placed. This means it doesn't have the same performance as a G0 collection.

                              Large objects (IIRC >85k) have their own heap separate from the G0/1/2 heaps. It's managed differently in that objects are fixed and the heap doesn't compact after a collect. Instead it uses a c++eqsue traversal algorithm to find a suitable place to store new objects.

                              Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                              C 1 Reply Last reply
                              0
                              • D Dan Neely

                                Colin Angus Mackay wrote:

                                It isn't bad. It just takes the longest because it is where all the large or aged objects are placed. This means it doesn't have the same performance as a G0 collection.

                                Large objects (IIRC >85k) have their own heap separate from the G0/1/2 heaps. It's managed differently in that objects are fixed and the heap doesn't compact after a collect. Instead it uses a c++eqsue traversal algorithm to find a suitable place to store new objects.

                                Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                                C Offline
                                C Offline
                                Colin Angus Mackay
                                wrote on last edited by
                                #15

                                Every day is a school day!

                                *Developer Day Scotland - Free community conference Delegate Registration Open

                                D 1 Reply Last reply
                                0
                                • C Colin Angus Mackay

                                  Every day is a school day!

                                  *Developer Day Scotland - Free community conference Delegate Registration Open

                                  D Offline
                                  D Offline
                                  Dan Neely
                                  wrote on last edited by
                                  #16

                                  And despite reading about it a week or two ago, I didn't quite get it right. The allocator prefers the end of the heap to free space except when it would otherwise be forced to grow the heap. In certain unexpected circumstances this can bite you badly. http://www.simple-talk.com/dotnet/.net-framework/the-dangers-of-the-large-object-heap/[^]

                                  Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                                  1 Reply Last reply
                                  0
                                  • L Lost User

                                    The garbage colector is indeterministic when it runs. This means there is no set interval or 'state' that will trigger it to run. If you are having a specific issue with the GC then you may want to post that as the GC is a beast that many have tried to tame and many have failed. It usually depends on what you are doing and what you are expecting to happen with your available memory. Some simple stuff about the GC. There are 3 levels of objects. G0, G1, G2, where G2 have been around the longest. Statitistics show if an object gets to G1 it will likely get to G2. G2 Collection is BAD. You can have the GC run in Concurrent (runs in the background) mode or in Server mode (pauses all threads while GC is running). To reiterate again, you need to know what you are wanting from the GC before you start messing with it. Oh and Microsoft claims the the GC in VS 2010 is "Better", whatever the heck that means (I do not recall a time when microsoft released a product and claimed something about it was "worse" or the same for that mattter).

                                    D Offline
                                    D Offline
                                    Dan Neely
                                    wrote on last edited by
                                    #17

                                    Collin Jasnoch wrote:

                                    To reiterate again, you need to know what you are wanting from the GC before you start messing with it. Oh and Microsoft claims the the GC in VS 2010 is "Better", whatever the heck that means (I do not recall a time when microsoft released a product and claimed something about it was "worse" or the same for that mattter).

                                    Well obvious possibilities occur: One would be doing fewer G1 collects to reduce the number of objects making it to G2. The second would be to change the large object heap to avoid the sort of problems in the article I linked elsewhere in the thread. Changing the end of the free memory chain would mitigate the pathological case described. Another option would be to suck it up and compact it (at least partially) if the utilization fraction got too low. In extremis only attempting to do so when an out of memory error would otherwise occur, but would still generate large amounts of flack for making the poor management visible. "My app gobbled xGB of unused ram before releasing 99% of it." This would also be impractical if running x64 code because of the larger address space.

                                    Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                                    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