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. .NET (Core and Framework)
  4. Garbage Collection

Garbage Collection

Scheduled Pinned Locked Moved .NET (Core and Framework)
questioncsharphelpcareer
5 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.
  • K Offline
    K Offline
    KSR81
    wrote on last edited by
    #1

    How many generations of garbage collection are there in .net? Why? I was asked this question in an interview? I didn't know what exactly he meant by generations? can some help me in this question. Thanks KSR

    R N S 3 Replies Last reply
    0
    • K KSR81

      How many generations of garbage collection are there in .net? Why? I was asked this question in an interview? I didn't know what exactly he meant by generations? can some help me in this question. Thanks KSR

      R Offline
      R Offline
      raghu g
      wrote on last edited by
      #2

      http://www.dotnetuncle.com/Framework/58_generations.aspx[^] Pls check this link

      1 Reply Last reply
      0
      • K KSR81

        How many generations of garbage collection are there in .net? Why? I was asked this question in an interview? I didn't know what exactly he meant by generations? can some help me in this question. Thanks KSR

        N Offline
        N Offline
        N a v a n e e t h
        wrote on last edited by
        #3

        AFAIK, There are 3 generations, zero, one and two. All newly allocated objects goes into generation-0. GC walks though the object tree and marks all objects that are reachable. Garbage collection usually takes place in this generation. All the objects that are marked reachable will be promoted to generation-1 assuming these objects are long-living. unreachable objects will be cleaned up and GC compacts the heap. If these objects are again surviving collection, they will be promoted to generation-2. Garbage collection occurs very rarely on generation-2. This generational algorithm helps GC to work efficiently. When memory is required, GC only needs to scan the generation-0 to free up the memory and other generations are scanned only when necessary. This will avoid scanning all objects that are present for an application.

        Navaneeth How to use google | Ask smart questions

        1 Reply Last reply
        0
        • K KSR81

          How many generations of garbage collection are there in .net? Why? I was asked this question in an interview? I didn't know what exactly he meant by generations? can some help me in this question. Thanks KSR

          S Offline
          S Offline
          supercat9
          wrote on last edited by
          #4

          In a simple garbage-collection system, memory is allocated from a pool. The pool always has one contiguous chunk of free memory. Each time a new object is created, the system carves off a suitable-sized piece of memory from the bottom of the free chunk. Even if some previously-allocated objects are no longer needed, and their memory could be reused, the system will carve space from bottom the free chunk until there's insufficient space left there to satisfy a request. When that happens, the system trigger what's called a "garbage collection" cycle, which will check each previously-allocated object in order to see whether it's still used. The first object which is still in use will be moved to the bottom of the pool. The next object which is still in use will be moved immediately after it. Once all the objects have been moved, any space above the last object may be considered part of the free memory pool, from which pieces may again be carved. Some early garbage-collection systems (like the one used in 1980's Microsoft BASIC) were horribly slow to determine which memory areas were still in use. Later systems have become much better in that regard. Nonetheless, every time a garbage collection takes place, it will generally be necessary to copy every single byte of data which still in use to a new address. This may take a considerable amount of time. The .net garbage collection system improves upon basic garbage collection by splitting the memory pool into three parts. New allocations are made from the first part. When the first part fills up, rather than copy still-in-use items to the start of the first part, they are added to the end of the second part and the first part is then erased. When the second part fills up, still-in-use items there are copied to the end of the third part and the second part is erased. If the third part fills up, still-in-use items there will be copied to the start of the third part. Since many allocated objects are only used for a very short time, the second part of the memory pool will fill up more more slowly than the first part, and the third part will fill up even more slowly. The net effect of this is that the system's garbage collection efforts will mostly be directed toward cleaning objects which aren't in use, rather than those which are. Since the former type of operation is faster and more useful, this improves performance.

          K 1 Reply Last reply
          0
          • S supercat9

            In a simple garbage-collection system, memory is allocated from a pool. The pool always has one contiguous chunk of free memory. Each time a new object is created, the system carves off a suitable-sized piece of memory from the bottom of the free chunk. Even if some previously-allocated objects are no longer needed, and their memory could be reused, the system will carve space from bottom the free chunk until there's insufficient space left there to satisfy a request. When that happens, the system trigger what's called a "garbage collection" cycle, which will check each previously-allocated object in order to see whether it's still used. The first object which is still in use will be moved to the bottom of the pool. The next object which is still in use will be moved immediately after it. Once all the objects have been moved, any space above the last object may be considered part of the free memory pool, from which pieces may again be carved. Some early garbage-collection systems (like the one used in 1980's Microsoft BASIC) were horribly slow to determine which memory areas were still in use. Later systems have become much better in that regard. Nonetheless, every time a garbage collection takes place, it will generally be necessary to copy every single byte of data which still in use to a new address. This may take a considerable amount of time. The .net garbage collection system improves upon basic garbage collection by splitting the memory pool into three parts. New allocations are made from the first part. When the first part fills up, rather than copy still-in-use items to the start of the first part, they are added to the end of the second part and the first part is then erased. When the second part fills up, still-in-use items there are copied to the end of the third part and the second part is erased. If the third part fills up, still-in-use items there will be copied to the start of the third part. Since many allocated objects are only used for a very short time, the second part of the memory pool will fill up more more slowly than the first part, and the third part will fill up even more slowly. The net effect of this is that the system's garbage collection efforts will mostly be directed toward cleaning objects which aren't in use, rather than those which are. Since the former type of operation is faster and more useful, this improves performance.

            K Offline
            K Offline
            KSR81
            wrote on last edited by
            #5

            thanks a lot for raghu, navaneeth and supercat9. I got the answer and understood the explanation. Thanks KSR

            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