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. Object Deletion

Object Deletion

Scheduled Pinned Locked Moved C#
algorithmstutorialquestion
11 Posts 6 Posters 1 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.
  • G Offline
    G Offline
    gauthee
    wrote on last edited by
    #1

    Hi, I have created an object for a class i have written. How to delete this object? I dont want to wait for the garbage collector to run its algorithm neither call the GC explicitly.

    Gautham

    G T C N 4 Replies Last reply
    0
    • G gauthee

      Hi, I have created an object for a class i have written. How to delete this object? I dont want to wait for the garbage collector to run its algorithm neither call the GC explicitly.

      Gautham

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

      Why do you want the object deleted? If you are using some unmanaged resources that you need to release, implement the IDisposable interface.

      --- single minded; short sighted; long gone;

      1 Reply Last reply
      0
      • G gauthee

        Hi, I have created an object for a class i have written. How to delete this object? I dont want to wait for the garbage collector to run its algorithm neither call the GC explicitly.

        Gautham

        T Offline
        T Offline
        Tamimi Code
        wrote on last edited by
        #3

        search google for destructor

        When you get mad...THINK twice that the only advice Tamimi - Code

        C 1 Reply Last reply
        0
        • T Tamimi Code

          search google for destructor

          When you get mad...THINK twice that the only advice Tamimi - Code

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          But the destructor (or Finalizer as it is also called) won't run until the garbage collector does. And then when a destructor is in place it slows garbage collection down because it has additional code to run.


          Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

          T 1 Reply Last reply
          0
          • G gauthee

            Hi, I have created an object for a class i have written. How to delete this object? I dont want to wait for the garbage collector to run its algorithm neither call the GC explicitly.

            Gautham

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            To re-emphasise what Guffa said. If you have unmanaged resources, or otherwise use objects that also implement IDisposable then you should implement IDisposable to free up or dispose of those resources. If those conditions are not met then let the garbage collector do its job - it is better at it than anyone else.


            Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

            G 1 Reply Last reply
            0
            • C Colin Angus Mackay

              But the destructor (or Finalizer as it is also called) won't run until the garbage collector does. And then when a destructor is in place it slows garbage collection down because it has additional code to run.


              Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

              T Offline
              T Offline
              Tamimi Code
              wrote on last edited by
              #6

              thank you for the explanation :)

              When you get mad...THINK twice that the only advice Tamimi - Code

              1 Reply Last reply
              0
              • C Colin Angus Mackay

                To re-emphasise what Guffa said. If you have unmanaged resources, or otherwise use objects that also implement IDisposable then you should implement IDisposable to free up or dispose of those resources. If those conditions are not met then let the garbage collector do its job - it is better at it than anyone else.


                Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

                G Offline
                G Offline
                gauthee
                wrote on last edited by
                #7

                The reason for this explicit object release is in our application in we are creating n number of objects for a specific operation after that operation is finished we no longer require thise objects We thought that the performance would be better if we explicitly release objects than waiting for the GC to do at later stages! So the answer is ti implement IDisposable! thank you!

                Gautham

                C 1 Reply Last reply
                0
                • G gauthee

                  The reason for this explicit object release is in our application in we are creating n number of objects for a specific operation after that operation is finished we no longer require thise objects We thought that the performance would be better if we explicitly release objects than waiting for the GC to do at later stages! So the answer is ti implement IDisposable! thank you!

                  Gautham

                  C Offline
                  C Offline
                  Colin Angus Mackay
                  wrote on last edited by
                  #8

                  gauthee wrote:

                  So the answer is ti implement IDisposable!

                  Depends. Do you have objects that already implement IDisposable to dispose of in the object, or do you manage any unmanaged resources in the object? If the answer is no to either then IDisposable won't do anything for you. Remember, the Garbage Collector knows more about the system state and when it would be an opportune moment to garbage collect things than you do because it can constantly fine tune itself.


                  Upcoming events: * Glasgow: Mock Objects, SQL Server CLR Integration, Reporting Services, db4o, Dependency Injection with Spring ... * Reading: Developer Day 5 Ready to Give up - Your help will be much appreciated. My website

                  1 Reply Last reply
                  0
                  • G gauthee

                    Hi, I have created an object for a class i have written. How to delete this object? I dont want to wait for the garbage collector to run its algorithm neither call the GC explicitly.

                    Gautham

                    N Offline
                    N Offline
                    NassosReyzidis
                    wrote on last edited by
                    #9

                    Hi there, You can use the CarbageCollection static class of the framework. System.GC.Collect(); See msdn for more info http://msdn2.microsoft.com/en-us/library/system.gc.aspx[^]

                    GanDad

                    S 1 Reply Last reply
                    0
                    • N NassosReyzidis

                      Hi there, You can use the CarbageCollection static class of the framework. System.GC.Collect(); See msdn for more info http://msdn2.microsoft.com/en-us/library/system.gc.aspx[^]

                      GanDad

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

                      This is almost always a bad idea as it ultimately ends up slowing down your application.

                      ----------------------------- In just two days, tomorrow will be yesterday.

                      N 1 Reply Last reply
                      0
                      • S Scott Dorman

                        This is almost always a bad idea as it ultimately ends up slowing down your application.

                        ----------------------------- In just two days, tomorrow will be yesterday.

                        N Offline
                        N Offline
                        NassosReyzidis
                        wrote on last edited by
                        #11

                        Scott Dorman wrote:

                        This is almost always a bad idea as it ultimately ends up slowing down your application.

                        I Totaly agree with you there, but since he needs (for some reason) to force the GarbageCollector to collect, this is the way.;)

                        GanDad

                        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