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. NULL

NULL

Scheduled Pinned Locked Moved C#
performance
13 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.
  • C Christian Graus

    No, you'll simply lose that reference to the object. The garbage collecter is in charge of when it's actually deleted, and either way, there's no performance gain, unless your object is big and you're chronically short of memory ( call the objects Dispose method if this is the case, for example if you deal with images in memory ). Christian Graus - Microsoft MVP - C++

    D Offline
    D Offline
    devin123
    wrote on last edited by
    #3
    1. But I am helping the GC telling the object has no reference, instead of GC finding itself. Is I am not gaining the performance? 2) So I can set a large object to null to achieve performance gain
    C 1 Reply Last reply
    0
    • D devin123
      1. But I am helping the GC telling the object has no reference, instead of GC finding itself. Is I am not gaining the performance? 2) So I can set a large object to null to achieve performance gain
      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #4

      devin123 wrote: But I am helping the GC telling the object has no reference, instead of GC finding itself. Is I am not gaining the performance? You're telling C# that this reference to the object is no longer required. At some point, sometime before your PC totally falls over from lack of memory, the GC will work out that no references are held, and so do garbage collection. I found out the hard way that managed DirectShow leaks memory ( I was loading video files ), and my app was running at a crawl as my memory was full and my disk drive was swapping non stop. GC had not occured. And why would you gain performance from getting back some memory, unless you have severe memory issues already ? devin123 wrote: So I can set a large object to null to achieve performance gain No, you can Dispose of it to free the memory, and then set it to null. The setting to null is actually irrelevant to performance or memory usage, it's just good coding. This will help if you're in a situation like my example above. You should do this when you have large memory objects flying around, but it will only help performance if excess memory usage is hurting it. Christian Graus - Microsoft MVP - C++

      D 1 Reply Last reply
      0
      • C Christian Graus

        devin123 wrote: But I am helping the GC telling the object has no reference, instead of GC finding itself. Is I am not gaining the performance? You're telling C# that this reference to the object is no longer required. At some point, sometime before your PC totally falls over from lack of memory, the GC will work out that no references are held, and so do garbage collection. I found out the hard way that managed DirectShow leaks memory ( I was loading video files ), and my app was running at a crawl as my memory was full and my disk drive was swapping non stop. GC had not occured. And why would you gain performance from getting back some memory, unless you have severe memory issues already ? devin123 wrote: So I can set a large object to null to achieve performance gain No, you can Dispose of it to free the memory, and then set it to null. The setting to null is actually irrelevant to performance or memory usage, it's just good coding. This will help if you're in a situation like my example above. You should do this when you have large memory objects flying around, but it will only help performance if excess memory usage is hurting it. Christian Graus - Microsoft MVP - C++

        D Offline
        D Offline
        devin123
        wrote on last edited by
        #5

        But dispose only releases the resources, it does not reclaims the memory.

        C 1 Reply Last reply
        0
        • D devin123

          But dispose only releases the resources, it does not reclaims the memory.

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #6

          I've done this, and I can assure you, it causes the memory to be released, if right away, or through encouraging the GC to release the object, I don't know. However, setting a reference to null definately doesn't do this, and will not help you in any way. Christian Graus - Microsoft MVP - C++

          D 1 Reply Last reply
          0
          • C Christian Graus

            I've done this, and I can assure you, it causes the memory to be released, if right away, or through encouraging the GC to release the object, I don't know. However, setting a reference to null definately doesn't do this, and will not help you in any way. Christian Graus - Microsoft MVP - C++

            D Offline
            D Offline
            devin123
            wrote on last edited by
            #7

            So you meant to say that using dispose, we can manually reclaim the memory and also the resouces

            C 1 Reply Last reply
            0
            • D devin123

              So you meant to say that using dispose, we can manually reclaim the memory and also the resouces

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #8

              GC reclaims memory, but calling Dispose seems to give it a serious hint, or otherwise, the Dispose methods for bitmaps and movies free the memory in some other way. Christian Graus - Microsoft MVP - C++

              D R 2 Replies Last reply
              0
              • C Christian Graus

                GC reclaims memory, but calling Dispose seems to give it a serious hint, or otherwise, the Dispose methods for bitmaps and movies free the memory in some other way. Christian Graus - Microsoft MVP - C++

                D Offline
                D Offline
                devin123
                wrote on last edited by
                #9

                So dispose does not directly reclaims the memory. It helps GC in more efficient GarbageCollection

                C 1 Reply Last reply
                0
                • D devin123

                  So dispose does not directly reclaims the memory. It helps GC in more efficient GarbageCollection

                  C Offline
                  C Offline
                  Christian Graus
                  wrote on last edited by
                  #10

                  That would be my guess. What I know for sure is that if you call dispose, you'll find your memory situation does not deteriorate where you use a lot of large objects. Christian Graus - Microsoft MVP - C++

                  D 1 Reply Last reply
                  0
                  • C Christian Graus

                    That would be my guess. What I know for sure is that if you call dispose, you'll find your memory situation does not deteriorate where you use a lot of large objects. Christian Graus - Microsoft MVP - C++

                    D Offline
                    D Offline
                    devin123
                    wrote on last edited by
                    #11

                    Thanks Christian, for your help. Devin

                    S 1 Reply Last reply
                    0
                    • D devin123

                      Thanks Christian, for your help. Devin

                      S Offline
                      S Offline
                      Skynyrd
                      wrote on last edited by
                      #12

                      The dispose pattern if implemented correctly should supress calling the finalize method of the object. The GC when collectiong objects always calls the finalizer of these objects, but if dispose is implemented and the object has been disposed, u might gain this small advantage in performance allthough its probably insignificant. The main advantage is as been stated before, the release of unmanaged resources and the fact that ur helping the gc manage memory in a more efficient way.

                      1 Reply Last reply
                      0
                      • C Christian Graus

                        GC reclaims memory, but calling Dispose seems to give it a serious hint, or otherwise, the Dispose methods for bitmaps and movies free the memory in some other way. Christian Graus - Microsoft MVP - C++

                        R Offline
                        R Offline
                        Roger Alsing 0
                        wrote on last edited by
                        #13

                        Here is how it works: you have a reference to your ".net" object instance the memory that object occupies is released by the GC when no references are left. Dispose is used to release native resources , such as filehandles , db connections or memory allocated by some native resource. so ".Dispose" does not kill the .net object itself , it just tells it to drop its expensive resources. if you are working with images , eg in DX there are plenty of unmanaged resources behind the scenes , and your .net object is told to drop those directly ... therefore Christian get a big chunk of memory back. if your object does not hold any references to any native resources (directly or inderectly) you will not gain anything by implementing idisposable on that class. eg if your class has a big arraylist or array and you set it to null in your dispose method , you will not gain anything since those are managed resources taht will be released by the GC. //Roger

                        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