Liberty BASIC Community Forum
« CLS and FLUSH confusion »

Welcome Guest. Please Login or Register.
Sep 20th, 2017, 09:50am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1 2 3  Notify Send Topic Print
 veryhotthread  Author  Topic: CLS and FLUSH confusion  (Read 417 times)
Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx CLS and FLUSH confusion
« Thread started on: Aug 18th, 2017, 10:23am »

My simulation program normally updates several graphicboxes about once per second, with the number of simulations user selectable from 1 to over a million. Everything works as it should EXCEPT when the number of simulations exceeds around a half million or so; then without mincing words, the program crashes! Obviously, it can take hours to run that many simulations, so troubleshooting is anything but fast and easy. Typical display code looks like this:

#main.text, "cls; down; fill yellow; backcolor yellow; place 2 13"
#main.text, "\" ; text
#main.text, "flush"

I suppose I could simply limit the maximum number of simulations (graphicbox writes) to maybe 10,000, but there's obviously an underlying problem. Anyway, after wasting many hours trying to figure this out, it appears that the repeated FLUSH statements associated with the graphicboxes are the culprits. I know this issue has been discussed before and that flushes can use up memory, but without the flushes, the displayed data "disappears" after the screen has been minimized or hidden. Furthermore, I've never seen anything more specific toward determining what the flush limit might be. I thought the CLS command was supposed to clear memory and prevent this type of problem. What am I missing here? Thanks!
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: CLS and FLUSH confusion
« Reply #1 on: Aug 18th, 2017, 11:37am »

Cls does erase all graphic segments and memory. So it must be program flow, you need to be sure you are cls all graphicboxes and not letting some build.
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: CLS and FLUSH confusion
« Reply #2 on: Aug 18th, 2017, 12:10pm »

Rod: Thanks for your help, but EVERY graphicbox instruction starts with CLS! The program can run a million simulations just fine with the flush statements commented out, but otherwise crashes on a high number of sims (>500K). I can't image what's causing this. Is there any way to monitor or check memory usage? Likewise, is there any way to detect when a screen has been restored after being minimized so the graphicboxes can be refreshed?
« Last Edit: Aug 18th, 2017, 12:52pm by Turtleman » User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: CLS and FLUSH confusion
« Reply #3 on: Aug 18th, 2017, 3:22pm »

If you start task manager with CtrlAltDel then you can watch the memory use of your Liberty program.

It will raise and fall as garbage collection kicks in so it looks a little random.

But you should find that it stabilises if you have your segment management right.

Are you changing color a lot, or changing fonts a lot? Be meticulous in watching where you draw, print, change font or draw, any drawing operation builds in the current segment, each graphicbox will have an initial segment. Are you leaving the initial segment open and deleting subsequent? If you do that 500k times you will consume memory.

That said cls should erase all drawing memory. It's usually program flow. Dummy up an extract drawing example in the looping style you are employing. Is that using memory? If so show us.
« Last Edit: Aug 18th, 2017, 3:27pm by Rod » User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: CLS and FLUSH confusion
« Reply #4 on: Aug 18th, 2017, 3:32pm »

The best way to get the refresh and redraw you need is to have a single "last drawn" segment flushed. So rather than 500k segments, keep getbmping, cls, drawbmp, flush.

So you grab the image you want to preserve cls drawbmp and flush to preserve a single segment that you want to see restored.

500k segments take too long to redraw, obviously.
User IP Logged

tenochtitlanuk
Board Moderator

member is offline

Avatar




Homepage PM

Gender: Male
Posts: 1162
xx Re: CLS and FLUSH confusion
« Reply #5 on: Aug 18th, 2017, 5:51pm »

The following example runs happily for over quarter million draws... couldn't be bothered to run it longer.
Code:
    nomainwin

    WindowWidth  =300
    WindowHeight =150

    open "Many draws" for graphics_nsb as #wg

    #wg "trapclose quit"
    #wg "down ; size 25"

    for i =1 to 1E6
        #wg "place 25 49 ; fill 20 20 20 ; color darkblue ; font Courier_New 14 bold"
        #wg "\ "; rndTxt$( 10); " "; right$( "--------" +str$( i), 8); " "
        #wg "color "; int( 2.56 *( i mod 100)); " 40 "; int( 2.56 *( 100 -( i mod 100)))
        #wg "set "; 40 +2 *( i mod 100); " 80"
        #wg "getbmp scr 0 0 300 150"
        #wg "cls"
        #wg "drawbmp scr 0 0"
        #wg "flush"

        'calldll #kernel32, "Sleep", 30 as long, re as void
        scan
    next i

    wait

    sub quit h$
        close #wg
        end
    end sub

    function rndTxt$( n)
        for i =1 to n
            rndTxt$ =rndTxt$ +chr$( 65 +int( 26 *rnd( 1)))
        next i
    end function
 

rem-out the delay for best speed- but will obviously flash at hundreds of loops/second...
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: CLS and FLUSH confusion
« Reply #6 on: Aug 19th, 2017, 10:31am »

Thanks for everyone's comments.

I ran a few hundred thousand simulations with and without the (several) flush statements and was surprised that there wasn't an appreciable difference in memory usage. However, usage definitely increases proportionately with the number of simulations. Rough numbers look something like this:

Sims Mem
Start 11 M
100K 14 M
200K 17 M
300K 22 M
600K 32 M

Memory usage increases with the number of simulations and I don't have a clue if that's to be expected or why it's happening. The program contains several windows and displays, but none were opened during these measurements.

Just for giggles, I removed the CLS statements and predictably the program crashes MUCH earlier, so evidently CLS is doing what it should. A million sims ran this morning without crashing with the flush in place, so maybe blaming flush was a false alarm.

Flushing on the last segment would be great if the user paused the simulations before minimizing the window, but that's not going to happen. Is there any way of detecting when the window is minimized so a flush can be initiated? Likewise, a refresh could be initiated if a restored window could be detected.

Overall, it's looking more and more like the number of sims should be limited to a "safe" number even if that's far from the ideal solution.

User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: CLS and FLUSH confusion
« Reply #7 on: Aug 19th, 2017, 2:34pm »

It may not be flush or graphics that is using memory. What does one sim look like, in outline terms. What gets opened, what gets loaded, what gets drawn. Do you unload anything?

In terms of minimizing you shouldn't need to be aware as if you have flush/cls right you will only ever have the last segment in memory, automatically.
« Last Edit: Aug 19th, 2017, 2:36pm by Rod » User IP Logged

Brandon Parker
Moderator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1115
xx Re: CLS and FLUSH confusion
« Reply #8 on: Aug 19th, 2017, 10:53pm »

Are you calling any DLL functions during a simulation? This is another area where people can get caught off guard. Depending on how DLL's are written they may/ may not "clean-up" memory that they use or do some other shenanigans that people are expecting.

How much code is involved in one run of a simulation? Can you post it here, link to it, or at least give a brief description?


{:0)

Brandon Parker
User IP Logged

Windows 7 Home Premium 64-bit Intel(R) Quad Core(TM) i5 CPU M 430 @ 2.27GHz 4GB DDR3 RAM
Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: CLS and FLUSH confusion
« Reply #9 on: Aug 20th, 2017, 08:57am »

Thanks again for everyone's comments.

Yes, there are quite a few DLL's (#kernel32, #user32, #qc) that are used for displaying playing cards and repositioning windows. However, I don't believe any DLL's are being called while running the simulations.

For the silly question of the day: does the fact that memory usage steadily increases necessarily indicate a problem? Are there any restrictions writing to graphicboxes as long as the CLS instruction accompanies all writes? What should I look for as a possible cause of problems? Memory usage seldom goes above 40M, which is relatively small compared to many programs on the machine. Still, the program starts running below 12M. At this point, I'm not sure that the occasional crash when running hundreds of thousands of simulations is even related to memory! Troubleshooting is difficult when the problem is so intermittent and takes so long before it appears.

The program is over 17K lines of code with many subroutines being called repeatedly when running the simulations, thus making it impractical to post code.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: CLS and FLUSH confusion
« Reply #10 on: Aug 20th, 2017, 12:20pm »

OK, just a few thoughts then. Program flow can use memory, if you call lots of subs and leave variables behind, that can use memory. Use and reuse an array() which is global. That way you don't leave a trail of variables that will never be used again. If you are building strings don't copy them or pass them, use byref or global or an array$().

Always be sure you are exiting loops correct. The loop must be finished or you must exit early with exit for exit while exit do etc. If not you build a stack and that will consume memory.

Run the program without the dll calls if possible just to test.
You can use find/replace to stick a ' in front quickly and just as easily remove it.

if you are recursively calling a sub that can use memory too.

Without even an outline of what a sim looks like it will be hard to help more. Be sure you are using v4.5.1 as that has more memory available, older versions were restricted to 70Mb But you probably knew all of that stuff!
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: CLS and FLUSH confusion
« Reply #11 on: Aug 20th, 2017, 12:30pm »

Thanks Rob. As you mentioned, I'm beginning to suspect that I've been lulled into state of false security since the program runs most of the time and that I am probably exiting a couple loops incorrectly. I may be able to post a code sample later; meanwhile, is there any way to see what "remnants" maybe left behind? And at the risk of exposing my ignorance once again, what does "recursively calling a sub" mean?

Yes, I'm using 4.5.1, which is big improvement, if only for displaying correctly when first opened.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5526
xx Re: CLS and FLUSH confusion
« Reply #12 on: Aug 20th, 2017, 4:13pm »

If you search out the Factorial demo you will see that the sub calls itself. Each time that happens it's current state is saved to the stack and a new instance with new variables starts. So there is always a limit to recursion. But you may not be doing that.
User IP Logged

Jim Hiley
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 202
xx Re: CLS and FLUSH confusion
« Reply #13 on: Aug 20th, 2017, 7:15pm »

I think that memory usage will increase steadily until LB does it's 'garbage collection'

I have an application that has a similar issue.

I don't know of any way to force LB to do it's garbage collection.
You could try pausing the program periodically during the run and see if that allows the memory usage to drop.

Jim
User IP Logged

Turtleman
Full Member
ImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 186
xx Re: CLS and FLUSH confusion
« Reply #14 on: Aug 21st, 2017, 06:11am »

I haven't been able to find "Factorial Demo" and still have a few things to examine. Meanwhile, I'll probably wimp-out and limit the number of simulations to 100,000. That's more than sufficient, though far from the ideal solution. I'll report back if I find what's occasionally causing problems with a much larger number. Thanks all!
User IP Logged

Pages: 1 2 3  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls