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. Remove instance

Remove instance

Scheduled Pinned Locked Moved C#
performance
8 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.
  • R Offline
    R Offline
    RishiKasnia
    wrote on last edited by
    #1

    i have created a user control Dynamically and added it to panel as a child. now if i remoove it from panel but it still remains in memory. i want it to be permanently removed from memory. There is no Dispose method available. and i also tried by implementing idisposable interface and overwriting the Dispose() method but it still remain in memory. Is there any way to remove this object(User Control) from memory Regards Rishi

    P S 2 Replies Last reply
    0
    • R RishiKasnia

      i have created a user control Dynamically and added it to panel as a child. now if i remoove it from panel but it still remains in memory. i want it to be permanently removed from memory. There is no Dispose method available. and i also tried by implementing idisposable interface and overwriting the Dispose() method but it still remain in memory. Is there any way to remove this object(User Control) from memory Regards Rishi

      P Offline
      P Offline
      Pedram Behroozi
      wrote on last edited by
      #2

      RishiKasnia wrote:

      There is no Dispose method available.

      AFAIK, UserControls inherited from IDisposable and have a Dispose() method.

      I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

      R 1 Reply Last reply
      0
      • P Pedram Behroozi

        RishiKasnia wrote:

        There is no Dispose method available.

        AFAIK, UserControls inherited from IDisposable and have a Dispose() method.

        I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

        R Offline
        R Offline
        RishiKasnia
        wrote on last edited by
        #3

        Thanks Pedram Behroozi You r right. Actually i was developing a Windows Appl Using WPF. in my case i need to remove this user control from memory . becoz after removing it from canvas or panel , if i try to fire some event then this Control also respond the event (Though it has been removed). for a workaround i detached the Event Handler for user control(when i remove it from canvas/panel). but it still resides in memory and remains throughout the program execution (becoz this control is not local to any Block) thus degrading performance . Regards Rishi

        P B 2 Replies Last reply
        0
        • R RishiKasnia

          Thanks Pedram Behroozi You r right. Actually i was developing a Windows Appl Using WPF. in my case i need to remove this user control from memory . becoz after removing it from canvas or panel , if i try to fire some event then this Control also respond the event (Though it has been removed). for a workaround i detached the Event Handler for user control(when i remove it from canvas/panel). but it still resides in memory and remains throughout the program execution (becoz this control is not local to any Block) thus degrading performance . Regards Rishi

          P Offline
          P Offline
          Pedram Behroozi
          wrote on last edited by
          #4

          Well I know nothing about WPF UserControls and unfortunately I can't help you. I think it's better for you to ask it in WPF / WCF / WF Forum[^]. Regards

          I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

          R 1 Reply Last reply
          0
          • P Pedram Behroozi

            Well I know nothing about WPF UserControls and unfortunately I can't help you. I think it's better for you to ask it in WPF / WCF / WF Forum[^]. Regards

            I died as a mineral and became a plant, I died as plant and rose to animal, I died as animal and I was Man. Why should I fear? When was I less by dying? -- Rumi[^] My blog

            R Offline
            R Offline
            RishiKasnia
            wrote on last edited by
            #5

            It ok Pedram Behroozi i have also posted the problem in WPF section but did't get any resonable ans. anyways thanks.

            1 Reply Last reply
            0
            • R RishiKasnia

              Thanks Pedram Behroozi You r right. Actually i was developing a Windows Appl Using WPF. in my case i need to remove this user control from memory . becoz after removing it from canvas or panel , if i try to fire some event then this Control also respond the event (Though it has been removed). for a workaround i detached the Event Handler for user control(when i remove it from canvas/panel). but it still resides in memory and remains throughout the program execution (becoz this control is not local to any Block) thus degrading performance . Regards Rishi

              B Offline
              B Offline
              Bijesh
              wrote on last edited by
              #6

              Removing instances from memory is the Garbage Collector's job. You don't have much control over when that gets done. But you can do cleanups inside the Dispose method. So basically you remove your event handlers in the Dispose method, and simply calle the Dispose method when you remove your control from the panel. (Typically Dispose is called for all the controls in a Form from within the Close method of the Form). Also if you control is hanging around in memory long after you have removed it, it could mean that you have some references to it somewhere in your program.

              1 Reply Last reply
              0
              • R RishiKasnia

                i have created a user control Dynamically and added it to panel as a child. now if i remoove it from panel but it still remains in memory. i want it to be permanently removed from memory. There is no Dispose method available. and i also tried by implementing idisposable interface and overwriting the Dispose() method but it still remain in memory. Is there any way to remove this object(User Control) from memory Regards Rishi

                S Offline
                S Offline
                Simon P Stevens
                wrote on last edited by
                #7

                I didn't realise you were using WPF. WPF controls don't have Dispose methods because they don't need them. WPF controls don't use any unmanaged resources. To release the memory used by a WPF control all you have to do is make sure you have no references to it, and the garbage collector will automatically clear up all the memory next time it runs. Events are something to watch out for. Remember that the event source holds a reference to the event sink, so yes if you have a control sinking events from a source on a main form, then you will need to make sure you detach the event handlers if you want the control to be released. Note that the garbage collector does not immediately remove the object from memory. the GC only runs occasionally, and when there is a requirement for more memory than is available. This is normal behaviour and there is nothing wrong with it. if you want to test things you can call GC.Collect() to force a collection and see if your memory is cleared up, but you shouldn't leave this in the code when you release it. The GC is carefully balanced, and forcing unnecessary collections can really screw up it's performance. If you are still having problems getting the object to be cleared up, you can use WinDbg and SOS to peer deeper into the objects on the heap and work out why they aren't being collected. (Getting started with WinDbg & SOS[^], How to find a GC leak[^], Memory leak detection in .Net[^])

                Simon

                R 1 Reply Last reply
                0
                • S Simon P Stevens

                  I didn't realise you were using WPF. WPF controls don't have Dispose methods because they don't need them. WPF controls don't use any unmanaged resources. To release the memory used by a WPF control all you have to do is make sure you have no references to it, and the garbage collector will automatically clear up all the memory next time it runs. Events are something to watch out for. Remember that the event source holds a reference to the event sink, so yes if you have a control sinking events from a source on a main form, then you will need to make sure you detach the event handlers if you want the control to be released. Note that the garbage collector does not immediately remove the object from memory. the GC only runs occasionally, and when there is a requirement for more memory than is available. This is normal behaviour and there is nothing wrong with it. if you want to test things you can call GC.Collect() to force a collection and see if your memory is cleared up, but you shouldn't leave this in the code when you release it. The GC is carefully balanced, and forcing unnecessary collections can really screw up it's performance. If you are still having problems getting the object to be cleared up, you can use WinDbg and SOS to peer deeper into the objects on the heap and work out why they aren't being collected. (Getting started with WinDbg & SOS[^], How to find a GC leak[^], Memory leak detection in .Net[^])

                  Simon

                  R Offline
                  R Offline
                  RishiKasnia
                  wrote on last edited by
                  #8

                  Thanks Simon :)

                  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