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. Dispose or not?

Dispose or not?

Scheduled Pinned Locked Moved C#
graphicsdata-structuresquestion
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.
  • C Offline
    C Offline
    cocoonwls
    wrote on last edited by
    #1

    hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,

    private void DrawConnectedLine(Graphics g)
    {
    try
    {
    int[,] drawobjectlist = new int[totaltransporter,n];
    :
    :
    :
    drawobjectlist = null;
    }
    catch(Exception exp)
    {
    }
    }

    thanks in advance, regards cocoonwls

    R N G S C 5 Replies Last reply
    0
    • C cocoonwls

      hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,

      private void DrawConnectedLine(Graphics g)
      {
      try
      {
      int[,] drawobjectlist = new int[totaltransporter,n];
      :
      :
      :
      drawobjectlist = null;
      }
      catch(Exception exp)
      {
      }
      }

      thanks in advance, regards cocoonwls

      R Offline
      R Offline
      Richard Blythe
      wrote on last edited by
      #2

      When you set an object to null, you're not actually disposing it, your just removing it's memory reference. While it's not a bad practice to set it's value to null once you're done with it, Visual Studio automatically removes the pointer for you when the method ends. It's good to see a developer that's concerned about memory management! Cheers! Richard

      My code this week has no errors. But it's Monday morning and I haven't got out of bed.

      1 Reply Last reply
      0
      • C cocoonwls

        hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,

        private void DrawConnectedLine(Graphics g)
        {
        try
        {
        int[,] drawobjectlist = new int[totaltransporter,n];
        :
        :
        :
        drawobjectlist = null;
        }
        catch(Exception exp)
        {
        }
        }

        thanks in advance, regards cocoonwls

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

        Since your array contains integer, no need to dispose it. If you are using any types which has Dispose method, calling it after using is a good practice. Read[^] Scott's article on this subject.

        Navaneeth How to use google | Ask smart questions

        1 Reply Last reply
        0
        • C cocoonwls

          hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,

          private void DrawConnectedLine(Graphics g)
          {
          try
          {
          int[,] drawobjectlist = new int[totaltransporter,n];
          :
          :
          :
          drawobjectlist = null;
          }
          catch(Exception exp)
          {
          }
          }

          thanks in advance, regards cocoonwls

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

          cocoonwls wrote:

          should i dispose it when end the coding or not? It will automate throw into garbage colletor?

          Disposal and garbage collection are two different things. Disposing an object doesn't cause it to be garbage collected. You can't call the Dispose method on an array, as it doesn't have one. An array only uses managed resources, so there is no need for any disposal.

          cocoonwls wrote:

          drawobjectlist = null;

          Setting the reference to null serves no pupose at all in this case. The garbage collector already knows that the array is not used any more, and it can even have collected it before you remove the reference. You are just assigning a null reference to a variable that is not used any longer.

          Despite everything, the person most likely to be fooling you next is yourself.

          1 Reply Last reply
          0
          • C cocoonwls

            hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,

            private void DrawConnectedLine(Graphics g)
            {
            try
            {
            int[,] drawobjectlist = new int[totaltransporter,n];
            :
            :
            :
            drawobjectlist = null;
            }
            catch(Exception exp)
            {
            }
            }

            thanks in advance, regards cocoonwls

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

            Just to reiterate what has already been said, an array itself doesn't support disposal but the objects contained in the array may, in which case you want to iterate over the items and call Dispose on each item. In this particular case, you have an array of int, which doesn't require disposal. Setting the array to null in this scenario will have no effect. The JIT compiler (and by extension the GC) is smart enough to know when the object is no longer in use. The thing to keep in mind is that the GC will only collect this array after it is no longer in use and when enough memory pressure exists to trigger a collection cycle. It won't necessarily be collected when the method goes out of scope.

            Scott Dorman

            Microsoft® MVP - Visual C# | MCPD President - Tampa Bay IASA [Blog][Articles][Forum Guidelines]


            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

            1 Reply Last reply
            0
            • C cocoonwls

              hi all, i have a array object in one of my method, i would like to know, should i dispose it when end the coding or not? It will automate throw into garbage colletor? Below is what i am done in my application,

              private void DrawConnectedLine(Graphics g)
              {
              try
              {
              int[,] drawobjectlist = new int[totaltransporter,n];
              :
              :
              :
              drawobjectlist = null;
              }
              catch(Exception exp)
              {
              }
              }

              thanks in advance, regards cocoonwls

              C Offline
              C Offline
              cocoonwls
              wrote on last edited by
              #6

              Dear all, Thanks your guys. Now i have clearly concept on disposal and GC. :laugh:

              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