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. C#
  4. how does dispose method actually work?

how does dispose method actually work?

Scheduled Pinned Locked Moved C#
performancequestion
6 Posts 5 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.
  • E Offline
    E Offline
    edel_ong
    wrote on last edited by
    #1

    I'm setting all my variables to null in my dispose method but it doesnt seem to work!!! The memory usage of the program does not go smaller, rather, the huge amount of memory is maintained! Why is it so? Thanks very much!

    W D J 3 Replies Last reply
    0
    • E edel_ong

      I'm setting all my variables to null in my dispose method but it doesnt seem to work!!! The memory usage of the program does not go smaller, rather, the huge amount of memory is maintained! Why is it so? Thanks very much!

      W Offline
      W Offline
      WillemM
      wrote on last edited by
      #2

      Dispose is generally for unmanaged resources. If you set all variables to null the garbage collector will eventually pick them up during one of the cleanup rounds. This may take a while though, this is why you dont notice anything when you set them to null.

      WM.
      What about weapons of mass-construction?

      J 1 Reply Last reply
      0
      • E edel_ong

        I'm setting all my variables to null in my dispose method but it doesnt seem to work!!! The memory usage of the program does not go smaller, rather, the huge amount of memory is maintained! Why is it so? Thanks very much!

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        I bet you're looking at Task Manager for this. You're assuming that the numbers you see in Task Manager is how much memory your app is using. You're not. The simplified version of what's happening... What you are seeing is how much memory the Common Language Runtime that your app is running in (a virtual machine, just like any Java app) is using. This includes memory reserved for future allocations but not yet used by, or just freed by, your app. This is known as the Managed Heap. Memory requested by your application is allocated in the Managed Heap, and when freed by your app, is returned to the Managed Heap, not Windows. The CLR maintains the Managed Heap and, using a self-tuning process, adjusts its size based on what your application has done in the past. If the CLR suspects your app will make large allocations in the near future, it'll maintain a larger Managed Heap, requesting more memory from Windows to do so. If it feels that it doesn't need as much memory in reserve, it'll release some of the Managed Heap memory back to Windows. Or, if Windows wants it back, it'll free up what it can and let Windows have whatever memory the CLR can free up from the Managed Heap. SO, what you're seeing in Task Manager is not how much memory your application is using, but is how much memory the CLR Virtual Machine is using and has reserved for your app.

        Dave Kreskowiak Microsoft MVP - Visual Basic

        1 Reply Last reply
        0
        • W WillemM

          Dispose is generally for unmanaged resources. If you set all variables to null the garbage collector will eventually pick them up during one of the cleanup rounds. This may take a while though, this is why you dont notice anything when you set them to null.

          WM.
          What about weapons of mass-construction?

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          WillemM wrote:

          If you set all variables to null the garbage collector will eventually pick them up during one of the cleanup rounds.

          This is an old myth carried over from Java. In C#, you don't need to set your variable to null in order for the GC to collect them. They will be collected when there's no more references to them, regardless of their null status.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          1 Reply Last reply
          0
          • E edel_ong

            I'm setting all my variables to null in my dispose method but it doesnt seem to work!!! The memory usage of the program does not go smaller, rather, the huge amount of memory is maintained! Why is it so? Thanks very much!

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            You don't need to use dispose; dispose isn't for cleaning up memory. It's for cleaning up unmanaged resources like socket connections, database connections, file streams, etc. Each object in your program will be garbage collected when there are no more references to the object. And even then, it won't be collected until the garbage collector lazily comes along and cleans up (or if you force a collection via GC.Collect())

            Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

            D 1 Reply Last reply
            0
            • J Judah Gabriel Himango

              You don't need to use dispose; dispose isn't for cleaning up memory. It's for cleaning up unmanaged resources like socket connections, database connections, file streams, etc. Each object in your program will be garbage collected when there are no more references to the object. And even then, it won't be collected until the garbage collector lazily comes along and cleans up (or if you force a collection via GC.Collect())

              Tech, life, family, faith: Give me a visit. I'm currently blogging about: Messianic Instrumentals (with audio) The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

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

              The other major thing dispose cleans up are handles. With each control on a form having one, old forms are a prime candidate for disposal.

              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