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. Visual Basic
  4. MyObject = New Object memory use

MyObject = New Object memory use

Scheduled Pinned Locked Moved Visual Basic
questionperformancetutorialdiscussion
6 Posts 2 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.
  • J Offline
    J Offline
    Johan Hakkesteegt
    wrote on last edited by
    #1

    A question I should have probably wondered about a little earlier, but anyway: Let's say I dim an object, for example a dataset, fill that object with data, and use the object, but do not explicitly clear/empty it after use. Then reuse it like:

    MyObject = New Object

    Will that automatically clear/empty the data from it, that I filled it with earlier? Or does this depend on the object? Perhaps the important question here is: what is best practice? Can I get problems by not explicitly clearing/emptying an object before reusing it?

    My advice is free, and you may get what you paid for.

    L 1 Reply Last reply
    0
    • J Johan Hakkesteegt

      A question I should have probably wondered about a little earlier, but anyway: Let's say I dim an object, for example a dataset, fill that object with data, and use the object, but do not explicitly clear/empty it after use. Then reuse it like:

      MyObject = New Object

      Will that automatically clear/empty the data from it, that I filled it with earlier? Or does this depend on the object? Perhaps the important question here is: what is best practice? Can I get problems by not explicitly clearing/emptying an object before reusing it?

      My advice is free, and you may get what you paid for.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, assuming VB.NET, if you do

      [1] Dim MyObject As Object
      [2] MyObject = New Object ' object 1
      [3] MyObject = New Object ' object 2

      then object 1 will become collectible when line [3] executes, meaning the garbage collector will collect object 1 the next time it runs (normally it runs when a future memory allocation cannot be satisfied without cleaning up memory first). And this is fine, unless it is a type that implements Dispose or Close, as in:

      [1] Dim MyObject As Font
      [2] MyObject = New Font(...) ' object 1
      [4] MyObject = New Font(...) ' object 2

      now you should insert [3] MyObject.Dispose() to make sure the unmanaged resources that might be held by the object get released right away (or the file or stream or whatever gets closed). If you don't Close/Dispose objects that support it, your app will use more memory and other system resources, and its behavior may deteriorate. Yes, the garbage collector will still find those objects, and its finalizer thread will eventually Dispose of them, but that typically will be much later. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      J 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, assuming VB.NET, if you do

        [1] Dim MyObject As Object
        [2] MyObject = New Object ' object 1
        [3] MyObject = New Object ' object 2

        then object 1 will become collectible when line [3] executes, meaning the garbage collector will collect object 1 the next time it runs (normally it runs when a future memory allocation cannot be satisfied without cleaning up memory first). And this is fine, unless it is a type that implements Dispose or Close, as in:

        [1] Dim MyObject As Font
        [2] MyObject = New Font(...) ' object 1
        [4] MyObject = New Font(...) ' object 2

        now you should insert [3] MyObject.Dispose() to make sure the unmanaged resources that might be held by the object get released right away (or the file or stream or whatever gets closed). If you don't Close/Dispose objects that support it, your app will use more memory and other system resources, and its behavior may deteriorate. Yes, the garbage collector will still find those objects, and its finalizer thread will eventually Dispose of them, but that typically will be much later. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        J Offline
        J Offline
        Johan Hakkesteegt
        wrote on last edited by
        #3

        Hi Luc, Thanks, that is really good to know. So far I just assumed that once I reused the object, it would automatically also dispose of the first object and all related data. So from your explanation, can I surmise that it would be good practise to change my current code (simplified) from:

        Dim ds As DataSet = Nothing
        ds = New DataSet("DataSet1")
        'Run a query and fill the dataset here
        'Run some loops, and do some stuff to the data
        ds = New DataSet("DataSet1")
        'Run another query and fill the dataset here
        'Run some loops, and do some stuff to the data

        to

        Dim ds As DataSet = Nothing
        ds = New DataSet("DataSet1")
        'Run a query and fill the dataset here
        'Run some loops, and do some stuff to the data
        ds.Clear() 'Would this even be necessary ?
        ds.Dispose()
        ds = New DataSet("DataSet1")
        'Run another query and fill the dataset here
        'Run some loops, and do some stuff to the data

        In other words, my app would at least use less resources while it is running? Cheers, Johan

        My advice is free, and you may get what you paid for.

        L 1 Reply Last reply
        0
        • J Johan Hakkesteegt

          Hi Luc, Thanks, that is really good to know. So far I just assumed that once I reused the object, it would automatically also dispose of the first object and all related data. So from your explanation, can I surmise that it would be good practise to change my current code (simplified) from:

          Dim ds As DataSet = Nothing
          ds = New DataSet("DataSet1")
          'Run a query and fill the dataset here
          'Run some loops, and do some stuff to the data
          ds = New DataSet("DataSet1")
          'Run another query and fill the dataset here
          'Run some loops, and do some stuff to the data

          to

          Dim ds As DataSet = Nothing
          ds = New DataSet("DataSet1")
          'Run a query and fill the dataset here
          'Run some loops, and do some stuff to the data
          ds.Clear() 'Would this even be necessary ?
          ds.Dispose()
          ds = New DataSet("DataSet1")
          'Run another query and fill the dataset here
          'Run some loops, and do some stuff to the data

          In other words, my app would at least use less resources while it is running? Cheers, Johan

          My advice is free, and you may get what you paid for.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Calling Dispose()/Close() when done with an object that has such method is good. Calling Clear() or Flush() or ... typically doesn't change a thing when immediately followed by Dispose/Close. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          J 1 Reply Last reply
          0
          • L Luc Pattyn

            Calling Dispose()/Close() when done with an object that has such method is good. Calling Clear() or Flush() or ... typically doesn't change a thing when immediately followed by Dispose/Close. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            J Offline
            J Offline
            Johan Hakkesteegt
            wrote on last edited by
            #5

            Alright, great ! Thanks a bundle for the enlightenment :)

            My advice is free, and you may get what you paid for.

            L 1 Reply Last reply
            0
            • J Johan Hakkesteegt

              Alright, great ! Thanks a bundle for the enlightenment :)

              My advice is free, and you may get what you paid for.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              you're welcome. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              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