Memory Growth in App

Hey all,

Is memory growth in an app normal? Should we expect some - particularly when painting a lot of images in canvases?

For example, in the app pictured below, I am grabbing bit map images from some video devices and pulling them as fast as I possibly can to the computer to display them in as real time a fashion as possible. Right now, the app is drawing a total data rate of about 40 Mb/sec and it could be more than that. When I started the app about 3 hours ago, it was taking around 26 Megabytes of memory. Now, 3 hours later, it is using about 59 Megabytes of memory. Is this normal? I’m processing a lot of picture images and refreshing a number of canvases. Each image is its own canvas.

In terms of video frame rate, I may get as high as 10 or 12 frames or new images per second.

I would love the memory growth to be 0. Is this possible? I am using WeakRefs and WeakAddressOf directors wherever possible so as to avoid any dangling references that are not getting cleaned up. I’m not creating new picture items in code as new image data comes in, but rather putting the data into properties that are of type picture and then doing what I need to do with the image. I’m setting these properties to Nil once I process the data in the objects where they exist.

So I’m not sure what else to do…or if this is simply “normal” behavior and that the framework leaks memory a little bit…

Jon

For what you’re doing, that’s pretty slow growth. What happens after 12 hours? Or 24? Does it continue to expand, or does it level off at some point?

OK. Thanks - that’s what I wanted to know. - So some growth is somewhat accepted.

As for 24 or 48 hours - I don’t know. I haven’t run it that long recently (I may have in the past - not sure). I’ll let it run and see what happens…

Yes, memory growth when handling images is normal. However that said, you should ensure that your application isn’t leaking memory. Makes sure that once you’ve finished with the image, that you’re letting it go.

The easiest way to track down if it’s working correctly is to use some high resolution RAW images, I use some samples from DPReview.com

Pentax 645Z

Canon 5DSR

Phase One IQ180

You’ll very quickly know if you have a memory issue with these images, as they should take just under a gig of RAM (at least the Canon 5DSR uses roughly 800mb of RAM).

[update]I’ve had to remove the links to the sample images, but here’s a link to the page. http://www.dpreview.com/previews/canon-eos-5ds-sr/5 [/update]

Sam, is there someplace in the docs where tips for things like this are covered?

Not that I know of.

Just check with your code that you’re not storing a reference to the image somewhere in memory and not flushing it. So you probably want to go though most of your code that handles images.

Simply setting all references to Nil is enough to tell Xojo you don’t need that image any more. The OS may have a cache of the image, but that shouldn’t be appearing in your apps memory space.

OK, thanks, I’ll take a look, never really thought about this issue before. But more apps are not graphic intensive, so maybe that is why I never really noticed a huge usage of memory over time. Thanks for the tip.

Yes, as long as it levels out at some point. If it continually increases, even slowly, you have a problem.

I just today discovered a leaking bug if you use Min or Max in a for…next loop with a variable.
ie.

for x as integer=min(i,100) to max(i,100) ... next

It appears to leak a copy of the variable on each iteration of the loop.

Might want to check your code for that… Affects Mac/Win/Linux… Xojo 2015r2.2
Feedback #40114

Which is why I use the aforementioned large images, it becomes very obvious very quickly :slight_smile:

[quote=200262:@jim mckay]I just today discovered a leaking bug if you use Min or Max in a for…next loop with a variable.
ie.[/quote]
Some time ago there was a discussion on gaining speed with loops and I seem to recall that if you put the evaluation code in the ‘for’ statement it causes it to be re-evauluated every time.

Dim n as integer = max( i, 100 ) Dim s as integer = min( i, 100 ) Dim x as integer for x=s to n ... next

I don’t have Min,Max in any loops, but it was a good suggestion (and a good bug catch on your part, Jim).

Thanks Sam for the tip on the raw images. I’ll have to modify my code to read them in and run them through the process, but it’s probably worth it to check for sure. So after running for about 15 hours straight now my app is up to about 200 Megabytes of memory.

You can periodically check Runtime.ObjectCount and Runtime.MemoryUsed.
http://documentation.xojo.com/index.php/Runtime

This will only reveal leaks in your pure Xojo code; leaks like Jim found or mismanaged declare use won’t show up.

1 Like

[quote=200315:@Will Shank]You can periodically check Runtime.ObjectCount and Runtime.MemoryUsed.
http://documentation.xojo.com/index.php/Runtime

This will only reveal leaks in your pure Xojo code; leaks like Jim found or mismanaged declare use won’t show up.[/quote]

Thanks, Will. Good to know. That’s really a great idea and I will do that.

My app had a Windows “this program has stopped responding” crash after maybe 20 hours of continuous use but was only using 240 Megabytes of memory. Not sure what the crash was about. Windows is so dang sensitive to anything where a program may appear to be stuck in a loop. Really wish it wasn’t. Any way to control that? Or find out where the hang happened (I doubt that)?

[quote=200315:@Will Shank]You can periodically check Runtime.ObjectCount and Runtime.MemoryUsed.
http://documentation.xojo.com/index.php/Runtime

This will only reveal leaks in your pure Xojo code; leaks like Jim found or mismanaged declare use won’t show up.[/quote]

So this is really interesting. I added some code to my app to display the object count and memory. My object count is remaining pretty steady around 1552 objects. I see it shoot up to about 1558 and then come back down. It’s been running this way now for about 7 or 8 hours.

So based on that, I’m assuming that my picture objects are being correctly handled and not running amuck. Would that be a valid assumption.

Now memory is interesting. When I started the app, the Runtime.Memory used property was reporting about 40 Megabytes of memory being used. Yet Windows was only reporting 26 Megabytes. This morning, the Runtime.Memory property is reporting 145 Megabytes, but Windows is only saying 99 Megabytes.

Why is there such a difference between what Xojo reports as Memory Used and what Windows reports?

Xojo objects often allocate windows framework objects as well, e.g. a “Picture” object may allocate some HBITMAPs. It’s possible for one to be leaking without the other which could explain your findings. I’ve seen these issues before. If you can track it down, definitely report it.

This is good info. Unfortunately, I’m not able to figure out where the framework objects are leaking… Not even sure how I would go about it…

Update: After running for about 30 hours now, the app is still using the same number of runtime objects: about 1552 or so. Memory has increased in Windows to about 300 Meg. Xojo is reporting runtime memory use of 400 Meg. So I’m not sure why there’s such a discrepancy between what Xojo says is its memory use vs. what the OS reports. Is Xojo perhaps including some of the system memory?

Jon, you can quickly look for leading HANDLEs using this technique: http://blogs.technet.com/b/yongrhee/archive/2011/12/19/how-to-troubleshoot-a-handle-leak.aspx

Also, Process Explorer (described also in that first page, available directly here) :
https://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

… can help you figure out just what’s leaking.

[quote=200882:@Michael Diehr]Jon, you can quickly look for leading HANDLEs using this technique: http://blogs.technet.com/b/yongrhee/archive/2011/12/19/how-to-troubleshoot-a-handle-leak.aspx

Also, Process Explorer (described also in that first page, available directly here) :
https://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

… can help you figure out just what’s leaking.[/quote]

Cool. I’ll look into this. But Windows 8 has a different task manager so I can’t figure out where you can set what columns to display. Grr…

And I still need to run this under OS X to see if it has the same problem…