Results 1 to 10 of 10

Thread: [RESOLVED] Drawing program canvas seems to run out of memory

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Resolved [RESOLVED] Drawing program canvas seems to run out of memory

    I am currently making a game with an inbuild bitmap drawing application called "Artpad". The issue is that after drawing for a while mainly on larger canvases over 400 x 400 there seems to be a memory leak or something because the entire canvas turns into a red x. As Artpad is part of a game I had convert it to a windows application that runs alone from the game in order to post the code here for you guys to check for me. It was a dodge port and it's missing features such as saving, loading and changing the colour of the colour pallets. But its just for demonstration purposes anyway...

    Here is the ported project:
    https://www.sendspace.com/file/egwfeb

    Simply make the window bigger, make a new canvas size of 800 x 600 then choose a color and draw with the pencil tool for a while and sudden boom. Red X....

    Whats going on?

    P.S Remember the code may look strange but that's because it has been poorly ported for the purpose of this topic to get help.

    Thanks in advance for any help

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Drawing program canvas seems to run out of memory

    Are you creating new Image objects a lot? If so then that will chew up a lot of memory and, if there's no down-time, the GC will not get a chance to reclaim it. If that is the case, start by making sure that you ALWAYS remove all references to an Image object when you're done with it, e.g. set a variable to Nothing if it refers to an Image you no longer want to use. That will ensure that the memory is at least eligible for garbage collection. Also, make sure that you always call Dispose on an Image object when you're done with it. That will make the garbage collection process more efficient.

    If you've done that and you still have issues then you may need to invoke the GC explicitly from time to time. How you decide to do that is up to you, e.g. monitor the memory in use or the specific number and size of the Images you've discarded. When you want to reclaim memory, call GC.Collect. You'll want to do a bit of testing to make sure that that doesn't cause noticeable freezes.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Drawing program canvas seems to run out of memory

    I'm not creating image objects a lot but I guess I am making a lot of paint tools and not disposing them... could that be the problem?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Drawing program canvas seems to run out of memory

    Quote Originally Posted by 12padams View Post
    I am making a lot of paint tools and not disposing them
    What's a "paint tool"? If it's an object that can be disposed then it should be disposed when you're done with it. That's a given regardless of the type of the object and how it's being used. If you're using up lots of memory and there's no down-time for the GC to clean it up then that is going to be the issue. Images are the objects most often to blame because they eat up memory very quickly but any objects will cause the same problem if you create enough of them.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Drawing program canvas seems to run out of memory

    I was refering to pens and brushes. Also could the following line of code in my paint event be the criminal?

    Code:
      Dim g As Graphics = Graphics.FromImage(previewcanvasbitmap)
            previewcanvasbitmap = New Bitmap(canvasbitmap.Width, canvasbitmap.Height)
            g = Graphics.FromImage(previewcanvasbitmap)
    I don't know how to make my program function without that line of code...

    How do I dispose that because whenever I dispose previewcanvasbitmap I just get a big x on my canvas?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Drawing program canvas seems to run out of memory

    Like I said, anything with a Dispose method should that method called WHEN YOU ARE FINISHED WITH THE OBJECT. There's no use disposing an Image right after creating it because then you can't use it. You dispose it when you're finished using it. If you finish using it when the form closes then you dispose it when the form closes.

    You should definitely be disposing your Graphics objects. I hope you're not creating one and keeping it hanging around. You should create it when you need to use it and then dispose it immediately afterwards.

    That code you posted is weird. You create a Graphics object from an Image, then you create a new Image, then you create a new Graphics object. That doesn't make sense. It also seems to suggest that you did have an existing Image that you simply discarded and replaced with a new one. If that's done multiple times then it could well be the issue.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Drawing program canvas seems to run out of memory

    Thats done hundreds of times...

    The code is executed because when a square, circle or other object is drawn on the forum a preview is shown. So another bitmap is created and drawn onto the canvas to prevent the preview square being applied directly to the canvas.

    Can you download the code and tell me a better way to achieve that without all those images being created.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Drawing program canvas seems to run out of memory

    Quote Originally Posted by 12padams View Post
    Thats done hundreds of times...
    Then that's your issue. You ARE creating lots of Image objects so that is almost certainly what's eating up your memory.
    Quote Originally Posted by 12padams View Post
    The code is executed because when a square, circle or other object is drawn on the forum a preview is shown. So another bitmap is created and drawn onto the canvas to prevent the preview square being applied directly to the canvas.

    Can you download the code and tell me a better way to achieve that without all those images being created.
    If you're using a PictureBox then you can just draw on that for a preview. Anything getting drawn on a PictureBox can be erased easily. That may not be an option in your case though. If not then do what I have already told you to do, i.e. make sure that you clean up the Images as much as you can when you're done with them and, if you still have an issue, call GC.Collect when you need to reclaim memory.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2010
    Posts
    102

    Re: Drawing program canvas seems to run out of memory

    The simple act of putting GC.Collect() in the paint event seemed to be an instant cure. No need to dispose anything....

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Drawing program canvas seems to run out of memory

    Quote Originally Posted by 12padams View Post
    The simple act of putting GC.Collect() in the paint event seemed to be an instant cure. No need to dispose anything....
    The fact that your immediate memory issue went away does not mean that there's no need to dispose anything. Do you want to write good code or do you want to write cr*p? If it's the latter then go right on not disposing anything. Also, simply calling GC.Collect on ever Paint event is not a good solution to anything. It may prevent your app encountering that memory issue but there's a good reason that everyone doesn't call GC.Collect willy-nilly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width