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. Imaging Application - not enough RAM

Imaging Application - not enough RAM

Scheduled Pinned Locked Moved C#
csharpperformancehelptutorial
9 Posts 6 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.
  • L Offline
    L Offline
    Libor Tinka
    wrote on last edited by
    #1

    Hello everyone, I'm working on imaging software in C#, but there's problem with memory. For example, when I load thumbnail images to album or a sigle image in full size, task manager will show that memory usage of my app has grown from 20 MB to 170 MB or more! What's worse, when I close the MDI form album with thumbnails, usage doesn't go down till closing the whole app. I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory. I'm afraid that times of malloc() and free() are gone, but...

    D J D J C 6 Replies Last reply
    0
    • L Libor Tinka

      Hello everyone, I'm working on imaging software in C#, but there's problem with memory. For example, when I load thumbnail images to album or a sigle image in full size, task manager will show that memory usage of my app has grown from 20 MB to 170 MB or more! What's worse, when I close the MDI form album with thumbnails, usage doesn't go down till closing the whole app. I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory. I'm afraid that times of malloc() and free() are gone, but...

      D Offline
      D Offline
      DavidNohejl
      wrote on last edited by
      #2

      ltinka wrote: I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. You are right that GC doesn't do anything. GC is very lazy. Because of performance it basically collects memory when there isn't enough free memory, or when you tell it to do so (calling GC.Collect()) Here[^] is a lot information about GC. Task manager shows memory allocated for a process, and this is actually bigger than memory used. IIRC that's what execution engine (I think) does - allocating is expansive operation, so it tries to minimalize allocation (by allocation bigger blocks of memory at once). ltinka wrote: When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. You should always do that. Exactly for very reason how GC works. David

      1 Reply Last reply
      0
      • L Libor Tinka

        Hello everyone, I'm working on imaging software in C#, but there's problem with memory. For example, when I load thumbnail images to album or a sigle image in full size, task manager will show that memory usage of my app has grown from 20 MB to 170 MB or more! What's worse, when I close the MDI form album with thumbnails, usage doesn't go down till closing the whole app. I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory. I'm afraid that times of malloc() and free() are gone, but...

        J Offline
        J Offline
        Judah Gabriel Himango
        wrote on last edited by
        #3

        Firstly, make sure you don't have any references to your images: if there are still references to the images in memory, the garbage collector will not collect the in-memory images. Finally, as a way to determine if your images are being cleaned up, try calling System.GC.Collect(); System.GC.WaitForPendingFinalizers(); And the garbage collection will kick into action and reclaim unused memory. If your memory is still high, you've got some references to the images still hanging around. Also, when you're done with an image, call Dispose() on it. It'll free up some resources immediately before the GC ever gets around to reclaiming the memory.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Conversation With a Muslim Judah Himango

        1 Reply Last reply
        0
        • L Libor Tinka

          Hello everyone, I'm working on imaging software in C#, but there's problem with memory. For example, when I load thumbnail images to album or a sigle image in full size, task manager will show that memory usage of my app has grown from 20 MB to 170 MB or more! What's worse, when I close the MDI form album with thumbnails, usage doesn't go down till closing the whole app. I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory. I'm afraid that times of malloc() and free() are gone, but...

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          dhn is partially correct. The GC is pretty lazy, but not as bad as he makes it sound. The GC Scheduler makes it's own determinations on when to run the GC process. MANY factors cause the Scheduler to either slow down the schedule or speed it up. It doesn't run only when your running out of memory. For instance, if you suddenly dispose of a large number of objects, then the GC will kick in early to clean up and compact memory. If you only disposed a few large objects, the GC won't collect them until it's normal schedule. Basically, looking at the Task Manager is not a good indicator of how much memory your application is actually using. What your seeing is appoxmiately how much unmanaged memory is allocated to your processes managed memory heaps, whether your using it or not. In other words, this is NOT a representation of how much unmanaged memory your application is actually using. The .NET Framework Memory Manager is also responsible for returning managed memory back to the unmanaged pool, if and when needed, or if requested by the system. If you want to check to see how much memory check into using the Performance Monitor. For instance, according to the Task Manage, a little analog clock application that I wrote is currently occupying 13MB of Memory. The Performance Monitor says that it's more like 178KB. Don't worry about it. The GC will collect on schedule and when it needs to. Any managed memory that needs to be released back to the system for other processes will be freed. It's just a matter of time and necessity. Oh! BTW: Forcing the GC to collect is not a good idea unless you know exactly what your doing and why. Forcing collections will mess with the Scheduler's optimizer and can actually hurt your applications performance. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          D 1 Reply Last reply
          0
          • L Libor Tinka

            Hello everyone, I'm working on imaging software in C#, but there's problem with memory. For example, when I load thumbnail images to album or a sigle image in full size, task manager will show that memory usage of my app has grown from 20 MB to 170 MB or more! What's worse, when I close the MDI form album with thumbnails, usage doesn't go down till closing the whole app. I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory. I'm afraid that times of malloc() and free() are gone, but...

            J Offline
            J Offline
            JDUK
            wrote on last edited by
            #5

            Make sure you dispose of absolultey every thing the moment you have writen it to the screen. If you write a bitmap to screen... dispose of it. if you draw a line dispose of the pen and/or brush and the graphics object as soon as you have drawn it. Dont think because its on screen you should not dispose of it until you no longer need it on screen... draw it then dispose of every graphics element(Bitmaps, clones, pens, brushes, graphics objects) it used. I wrote a simple app that used cloned portions of a bitmap as brushes and painted them to the screen... i didnt get many brushstokes done before i hit an "Out of memmory" error when depending soley on the GC.

            D 1 Reply Last reply
            0
            • D Dave Kreskowiak

              dhn is partially correct. The GC is pretty lazy, but not as bad as he makes it sound. The GC Scheduler makes it's own determinations on when to run the GC process. MANY factors cause the Scheduler to either slow down the schedule or speed it up. It doesn't run only when your running out of memory. For instance, if you suddenly dispose of a large number of objects, then the GC will kick in early to clean up and compact memory. If you only disposed a few large objects, the GC won't collect them until it's normal schedule. Basically, looking at the Task Manager is not a good indicator of how much memory your application is actually using. What your seeing is appoxmiately how much unmanaged memory is allocated to your processes managed memory heaps, whether your using it or not. In other words, this is NOT a representation of how much unmanaged memory your application is actually using. The .NET Framework Memory Manager is also responsible for returning managed memory back to the unmanaged pool, if and when needed, or if requested by the system. If you want to check to see how much memory check into using the Performance Monitor. For instance, according to the Task Manage, a little analog clock application that I wrote is currently occupying 13MB of Memory. The Performance Monitor says that it's more like 178KB. Don't worry about it. The GC will collect on schedule and when it needs to. Any managed memory that needs to be released back to the system for other processes will be freed. It's just a matter of time and necessity. Oh! BTW: Forcing the GC to collect is not a good idea unless you know exactly what your doing and why. Forcing collections will mess with the Scheduler's optimizer and can actually hurt your applications performance. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

              D Offline
              D Offline
              DavidNohejl
              wrote on last edited by
              #6

              Dave Kreskowiak wrote: dhn is partially correct. The GC is pretty lazy, but not as bad as he makes it sound. Everything I say sounds bad these days cuz I feel like that. That is, bad. :sigh: Yeah I was quite incorrect. Thanks. OTOH I belive he follows that link I gave him... so he can correct my mystifications :) David

              1 Reply Last reply
              0
              • L Libor Tinka

                Hello everyone, I'm working on imaging software in C#, but there's problem with memory. For example, when I load thumbnail images to album or a sigle image in full size, task manager will show that memory usage of my app has grown from 20 MB to 170 MB or more! What's worse, when I close the MDI form album with thumbnails, usage doesn't go down till closing the whole app. I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory. I'm afraid that times of malloc() and free() are gone, but...

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

                ltinka wrote: When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. You need to make sure you do this, or your memory usage will grow ltinka wrote: I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory. If the GC has not kicked in before then, it will do so in time to stop this. ltinka wrote: I'm afraid that times of malloc() and free() are gone, but... No, if you have large objects like media files or images in your app, you need to handle your own memory. malloc and free are long gone though, unless you came to C# from C and not C++. Christian Graus - Microsoft MVP - C++

                1 Reply Last reply
                0
                • J JDUK

                  Make sure you dispose of absolultey every thing the moment you have writen it to the screen. If you write a bitmap to screen... dispose of it. if you draw a line dispose of the pen and/or brush and the graphics object as soon as you have drawn it. Dont think because its on screen you should not dispose of it until you no longer need it on screen... draw it then dispose of every graphics element(Bitmaps, clones, pens, brushes, graphics objects) it used. I wrote a simple app that used cloned portions of a bitmap as brushes and painted them to the screen... i didnt get many brushstokes done before i hit an "Out of memmory" error when depending soley on the GC.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  Well, because you relied on the GC to call the .Dispose() methods for you, you we're using unmanaged system resources faster than the GC could free them. The GC doesn't track unmanaged resources used (short of memory anyway) so it can't tell that the system is running low on stuff like Handles. It's very unlikely you actually ran out of memory, rather it is more likely that you blew through tens of thousands of handles. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

                  1 Reply Last reply
                  0
                  • L Libor Tinka

                    Hello everyone, I'm working on imaging software in C#, but there's problem with memory. For example, when I load thumbnail images to album or a sigle image in full size, task manager will show that memory usage of my app has grown from 20 MB to 170 MB or more! What's worse, when I close the MDI form album with thumbnails, usage doesn't go down till closing the whole app. I thought that Garbage Collector should remove all resources, but it's quite inactive... When I force thumbnail image to dispose (calling Image.Dispose()), nothing change. I have a suspicion that memory usage of my app growing gradually till some moment when system tells user there is not enough virtual memory. I'm afraid that times of malloc() and free() are gone, but...

                    L Offline
                    L Offline
                    Libor Tinka
                    wrote on last edited by
                    #9

                    Thanks a lot. My app is in development for nearly 18 months and there are huge amount of code to repair by Dispose() calls (:sigh:).

                    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