.NET Windows Form Doesn't Release Memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimmarq
    New Member
    • May 2007
    • 12

    .NET Windows Form Doesn't Release Memory

    I have a button click event on my main application window that opens a form. The form has a lot of controls, loads a lot of data, and uses a lot of memory. When I close the form the memory is no deallocated. Every time I open the form, 7 megabytes of RAM and 7 megabytes of virtual memory are eaten up.

    Here are the only lines of code in the button click event:

    Dim frminv As New FrmInvoice()
    frminv.MdiParen t = Me
    frminv.Show()


    As soon as the form is closed, all objects should be eligible for garbage collection, but they are never garbage collected. (I have run a loop to open the form and close it hundreds of times--the garbage collector never runs).

    What can I be doing wrong? What could possibly be referencing the FrmInvoice that prevents it from being deallocated?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You are forgetting to call
    frminv.Dispose( );

    GC will not collect it until you explicitly release the "frminv" object.



    Optionally, you could probably get away with doing a GC.Collect(), but .Dispose() is what you should be using

    Comment

    • jimmarq
      New Member
      • May 2007
      • 12

      #3
      Thanks for the quick reply.

      I tried your suggestion, but I still wasn't able to free up any memory.

      Neither of these worked for me:

      Dim frminv As New FrmInvoice()
      frminv.MdiParen t = Me
      frminv.Show()
      frminv.Close()
      frminv.Dispose( )

      or

      Dim frminv As New FrmInvoice()
      frminv.MdiParen t = Me
      frminv.Show()
      frminv.Dispose( )

      Calling GC.Collect() doesn't do anything for me, either (even when I use it with GC.WaitForPendi ngFinalizers()) .

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        How do you know it's not freeing up memory?
        Is memory allocated growing continuously?

        Does that form have any threads of custom objects that might also need to be closed/disposed of?

        Comment

        • jimmarq
          New Member
          • May 2007
          • 12

          #5
          Well, I create a loop that created a new form, showed it, and closed it 500 times.

          My process used 1.2GB of RAM, about the same amount of my page file.

          I stressed my computer until I thought I would force the garbage collector to run by using all physical memory, but then the page file just started to grow. Even letting the computer sit for a an hour to wait for garbage collection didn't work.

          Are there objects in the form that need to be disposed? Good question. I thought that in .NET, once an object's parent is disposed, the object would be garbage collected. So, in effect, closing the form should have a cascading effect on all child objects.

          I know I'm closing my DB connections, I'm closing all streams, but I seem to be missing something fairly big. Any ideas?

          Comment

          • jimmarq
            New Member
            • May 2007
            • 12

            #6
            I used the SOS debugger to look at my form object after it was closed to see what is staying around. Can anybody decipher this and tell me what I am doing wrong?

            !dumpheap -type FrmInvoice
            Address MT Size
            01513f1c 03bc749c 1008
            total 1 objects
            Statistics:
            MT Count TotalSize Class Name
            03bc749c 1 1008 FrmInvoice
            Total 1 objects

            !gcroot 01513f1c
            Note: Roots found on stacks may be false positives. Run "!help gcroot" for
            more info.
            Error during command: warning! Extension is using a feature which Visual Studio does not implement.

            Scan Thread 1744 OSTHread 6d0
            Scan Thread 3052 OSTHread bec
            Scan Thread 2524 OSTHread 9dc
            Scan Thread 216 OSTHread d8
            Scan Thread 5288 OSTHread 14a8
            Scan Thread 3708 OSTHread e7c
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1604:Root:0 152f54c(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            0152f47c(System .Windows.Forms. Button)->
            0152f5e8(System .ComponentModel .EventHandlerLi st)->
            0152f5f8(System .ComponentModel .EventHandlerLi st+ListEntry)->
            0152f5c8(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1608:Root:0 152f6dc(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            0152f60c(System .Windows.Forms. Button)->
            0152f778(System .ComponentModel .EventHandlerLi st)->
            0152f788(System .ComponentModel .EventHandlerLi st+ListEntry)->
            0152f758(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1610:Root:0 152f9c4(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            0152f8f4(System .Windows.Forms. Button)->
            0152fa60(System .ComponentModel .EventHandlerLi st)->
            0152fa70(System .ComponentModel .EventHandlerLi st+ListEntry)->
            0152fa40(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae16c0:Root:0 1526ca8(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            01526bd8(System .Windows.Forms. Button)->
            01526d44(System .ComponentModel .EventHandlerLi st)->
            01526d54(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01526d24(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae16c4:Root:0 1526e38(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            01526d68(System .Windows.Forms. Button)->
            01526ed4(System .ComponentModel .EventHandlerLi st)->
            01526ee4(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01526eb4(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae182c:Root:0 1528568(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            015284d4(System .Windows.Forms. HScrollBar)->
            015285cc(System .ComponentModel .EventHandlerLi st)->
            015298b4(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01529880(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01529790(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01529758(System .Windows.Forms. ScrollEventHand ler)->
            015282a0(System .Windows.Forms. DataGridView)->
            0163178c(System .Windows.Forms. DataGridView+Da taGridViewDataC onnection)->
            01522390(System .Windows.Forms. BindingSource)->
            01522464(System .ComponentModel .EventHandlerLi st)->
            01523f2c(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01523f0c(System .ComponentModel .AddingNewEvent Handler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae188c:Root:0 152b4f8(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            0152b440(System .Windows.Forms. TextBox)->
            0152b594(System .ComponentModel .EventHandlerLi st)->
            0152b5a4(System .ComponentModel .EventHandlerLi st+ListEntry)->
            0152b574(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1930:Root:0 152d418(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            0152d360(System .Windows.Forms. TextBox)->
            0152d4d4(System .ComponentModel .EventHandlerLi st)->
            0152d4e4(System .ComponentModel .EventHandlerLi st+ListEntry)->
            0152d4b4(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1958:Root:0 1532b18(System. Windows.Forms.T oolStrip)->
            015333f0(System .Windows.Forms. ToolStripItemCo llection)->
            01533408(System .Collections.Ar rayList)->
            0153c0dc(System .Object[])->
            01533a5c(System .Windows.Forms. ToolStripButton )->
            01533b60(System .ComponentModel .EventHandlerLi st)->
            01533b70(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01533b40(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae19c8:Root:0 1532578(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            015324a8(System .Windows.Forms. Button)->
            01532614(System .ComponentModel .EventHandlerLi st)->
            01532624(System .ComponentModel .EventHandlerLi st+ListEntry)->
            015325f4(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1c10:Root:0 1523cc8(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            01523bf8(System .Windows.Forms. Button)->
            01523d64(System .ComponentModel .EventHandlerLi st)->
            01523d74(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01523d44(System .EventHandler)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1c38:Root:0 1522ec0(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            01522d98(System .Windows.Forms. LinkLabel)->
            01522f24(System .ComponentModel .EventHandlerLi st)->
            01522ffc(System .ComponentModel .EventHandlerLi st+ListEntry)->
            01522fac(System .Windows.Forms. LinkLabelLinkCl ickedEventHandl er)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1c84:Root:0 1513f1c(FrmInvo ice)
            DOMAIN(0014C5D8 ):HANDLE(WeakSh ):ae1c88:Root:0 1514334(System. Windows.Forms.C ontrol+ControlN ativeWindow)->
            01513f1c(FrmInv oice)
            DOMAIN(0014C5D8 ):HANDLE(WeakLn ):ae1df0:Root:0 154ac34(System. Windows.Forms.N ativeMethods+Wn dProc)->
            01514334(System .Windows.Forms. Control+Control NativeWindow)

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by jimmarq
              Are there objects in the form that need to be disposed? Good question. I thought that in .NET, once an object's parent is disposed, the object would be garbage collected. So, in effect, closing the form should have a cascading effect on all child objects.
              It disposes objects that are in it's .Controls collection, but not others made. Streams, file handles, socket handles, and *especially* any threads created will NOT be closed or disposed of when the form is closed.

              Comment

              • jimmarq
                New Member
                • May 2007
                • 12

                #8
                Plater--I appreciate your help.

                The problem is with my datasets that I'm using to populate combox items with. If I comment out the parts of the form's load event that fill the datasets with data, the memory problem goes away. I loaded the form and closed it a few hundred times in a loop, and the memory never went above 50 megabytes (a bif difference from the 1.2 gigabytes it used previously).

                Now I just have to find a way to destroy the datasets when the form closes...

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  You could try this little test.
                  Whereas if you have something like this:
                  Code:
                  DataSet myDataset;
                  myDataset = SomeFunctionThatFillsIt();
                  myobjetc.DataSource=myDataset;
                  Modify it to this
                  Code:
                  DataSet myDataset;
                  myDataset = SomeFunctionThatFillsIt();
                  myDataset.Dispose();
                  myDataset=null;
                  myobjetc.DataSource=myDataset;
                  And see if there is a difference in memory use (it *should* match the situation you described where you never populate the dataset at all)

                  Comment

                  Working...