LB Booster
Programming >> Liberty BASIC language >> question on timer -- memory usage
http://lbb.conforums.com/index.cgi?board=lblang&action=display&num=1440731132

question on timer -- memory usage
Post by hammerjit on Aug 28th, 2015, 03:05am

I notice when I use 'timer' in my code, the memory usage keeps increasing when the code is executed.

How to prevent the memory increase?
Code:
timer 100, [main.inputLoop]
[main.inputLoop]
code....
wait

[quit]
timer 0
close #main
end

 

Re: question on timer -- memory usage
Post by Richard Russell on Aug 28th, 2015, 12:02pm

on Aug 28th, 2015, 03:05am, hammerjit wrote:
I notice when I use 'timer' in my code, the memory usage keeps increasing when the code is executed.

I tried running your program - exactly as listed but with code... commented out of course - and I see no increase in memory usage. In the debugger the Heap usage reads 1157 and the Stack usage reads 4, constantly. I've also checked in Task Manager and I'm not seeing any increase in memory usage there either.

Are you sure you are seeing the increase even with no user code between [main.inputLoop] and wait?

Richard.
Re: question on timer -- memory usage
Post by hammerjit on Sep 2nd, 2015, 06:58am

ok...sorry for not including the actual code....take for eg this code and
1) you will notice memory increase in task manager
2) leave the program for few mins unattended, do you see window appearing on the top left side of screen?

Code:

    nomainwin

    WindowWidth = 880
    WindowHeight = 650

    groupbox #main.groupbox1, "Input", 6, 26, 840, 215
    groupbox #main.groupbox2, "Results", 6, 246, 840, 265
    statictext #main.statictext1, "Input 1", 30, 51, 96, 20
    statictext #main.statictext2, "Input 2", 30, 81, 120, 20
    statictext #main.statictext3, "Input 3", 30, 111, 144, 20
    statictext #main.statictext4, "Input 4", 30, 141, 96, 20
    statictext #main.statictext5, "Input 5", 30, 171, 184, 20
    statictext #main.statictext6, "Input 6", 30, 201, 184, 20
    textbox #main.textbox1, 246, 46, 100, 25
    textbox #main.textbox2, 246, 76, 100, 25
    textbox #main.textbox3, 246, 106, 100, 25
    textbox #main.textbox4, 246, 136, 100, 25
    textbox #main.textbox5, 246, 166, 100, 25
    textbox #main.textbox6, 246, 196, 100, 25
    statictext #main.statictext20, "Result 1", 94, 276, 112, 20
    statictext #main.statictext21, "Result 2", 94, 301, 192, 20
    statictext #main.statictext22, "Result 3", 94, 326, 176, 20
    statictext #main.statictext23, "Result 4", 94, 351, 200, 20
    statictext #main.statictext24, "Result 5", 94, 376, 208, 20
    statictext #main.statictext25, "Result 6", 94, 401, 232, 20
    statictext #main.statictext26, "Result 7", 94, 426, 136, 20
    statictext #main.statictext27, "Result 8", 94, 451, 120, 20
    statictext #main.statictext28, "Result 9", 94, 476, 120, 20
    statictext #main.statictext40, "", 406, 276, 144, 20
    statictext #main.statictext41, "", 406, 301, 144, 20
    statictext #main.statictext42, "", 406, 326, 144, 20
    statictext #main.statictext43, "", 406, 351, 144, 20
    statictext #main.statictext44, "", 406, 376, 144, 20
    statictext #main.statictext45, "", 406, 401, 144, 20
    statictext #main.statictext46, "", 406, 426, 144, 20
    statictext #main.statictext47, "", 406, 451, 144, 20
    statictext #main.statictext48, "", 406, 476, 144, 20
    open "test memory usage" for window_nf as #main
    print #main, "trapclose [quit]"
    print #main, "font ms_sans_serif 0 16"


timer 100, [main.inputLoop]
[main.inputLoop]   'wait here for input event
    PRINT #main.textbox1, "!contents? Input1"
    PRINT #main.textbox2, "!contents? Input2"
    PRINT #main.textbox3, "!contents? Input3"

    PRINT #main.textbox4, "!contents? Input4"
    PRINT #main.textbox5, "!contents? Input5"
    PRINT #main.textbox6, "!contents? Input6"


if Input5 <> 0 then

let value1=(Input2*Input6)/100
let value2=(Input2*Input3)/100
let value3=Input4
let value4=Input5-value2-value1-value3
let value5=(value4/Input2)*100
let value6=(value4/(100-Input2))*100
let value7=(Input5-Input4)/(Input6+Input3)*100
let value8=(Input1-Input5)
let value9=(100/value7)*100

end if

    print #main.statictext40, "!font lucida_console 0 14 bold"
    print #main.statictext41, "!font lucida_console 0 14 bold"
    print #main.statictext42, "!font lucida_console 0 14 bold"
    print #main.statictext43, "!font lucida_console 0 14 bold"
    print #main.statictext44, "!font lucida_console 0 14 bold"
    print #main.statictext45, "!font lucida_console 0 14 bold"
    print #main.statictext46, "!font lucida_console 0 14 bold"
    print #main.statictext47, "!font lucida_console 0 14 bold"
    print #main.statictext48, "!font lucida_console 0 14 bold red"

    mask$="#,###,###.##"


    print #main.statictext40, using(mask$,value1)
    print #main.statictext41, using(mask$,value2)
    print #main.statictext42, using(mask$,value3)
    print #main.statictext43, using(mask$,value4)
    print #main.statictext44, using(mask$,value5)
    print #main.statictext45, using(mask$,value6)
    print #main.statictext46, using(mask$,value7)
    print #main.statictext47, using(mask$,value8)
    print #main.statictext48, using(mask$,value9)

    wait


[quit]
    Close #main
    END
 

Re: question on timer -- memory usage
Post by Richard Russell on Sep 2nd, 2015, 08:36am

on Sep 2nd, 2015, 06:58am, hammerjit wrote:
1) you will notice memory increase in task manager

OK, the reason is the !font commands which you are repeating on every timer interrupt; normally you should set the font just once. I will see if I can modify LBB to avoid this resource leak, but the practical workaround is to move the font commands out of the loop, like this:

Code:
    nomainwin

    WindowWidth = 880
    WindowHeight = 650

    groupbox #main.groupbox1, "Input", 6, 26, 840, 215
    groupbox #main.groupbox2, "Results", 6, 246, 840, 265
    statictext #main.statictext1, "Input 1", 30, 51, 96, 20
    statictext #main.statictext2, "Input 2", 30, 81, 120, 20
    statictext #main.statictext3, "Input 3", 30, 111, 144, 20
    statictext #main.statictext4, "Input 4", 30, 141, 96, 20
    statictext #main.statictext5, "Input 5", 30, 171, 184, 20
    statictext #main.statictext6, "Input 6", 30, 201, 184, 20
    textbox #main.textbox1, 246, 46, 100, 25
    textbox #main.textbox2, 246, 76, 100, 25
    textbox #main.textbox3, 246, 106, 100, 25
    textbox #main.textbox4, 246, 136, 100, 25
    textbox #main.textbox5, 246, 166, 100, 25
    textbox #main.textbox6, 246, 196, 100, 25
    statictext #main.statictext20, "Result 1", 94, 276, 112, 20
    statictext #main.statictext21, "Result 2", 94, 301, 192, 20
    statictext #main.statictext22, "Result 3", 94, 326, 176, 20
    statictext #main.statictext23, "Result 4", 94, 351, 200, 20
    statictext #main.statictext24, "Result 5", 94, 376, 208, 20
    statictext #main.statictext25, "Result 6", 94, 401, 232, 20
    statictext #main.statictext26, "Result 7", 94, 426, 136, 20
    statictext #main.statictext27, "Result 8", 94, 451, 120, 20
    statictext #main.statictext28, "Result 9", 94, 476, 120, 20
    statictext #main.statictext40, "", 406, 276, 144, 20
    statictext #main.statictext41, "", 406, 301, 144, 20
    statictext #main.statictext42, "", 406, 326, 144, 20
    statictext #main.statictext43, "", 406, 351, 144, 20
    statictext #main.statictext44, "", 406, 376, 144, 20
    statictext #main.statictext45, "", 406, 401, 144, 20
    statictext #main.statictext46, "", 406, 426, 144, 20
    statictext #main.statictext47, "", 406, 451, 144, 20
    statictext #main.statictext48, "", 406, 476, 144, 20
    open "test memory usage" for window_nf as #main
    print #main, "trapclose [quit]"
    print #main, "font ms_sans_serif 0 16"

    print #main.statictext40, "!font lucida_console 0 14 bold"
    print #main.statictext41, "!font lucida_console 0 14 bold"
    print #main.statictext42, "!font lucida_console 0 14 bold"
    print #main.statictext43, "!font lucida_console 0 14 bold"
    print #main.statictext44, "!font lucida_console 0 14 bold"
    print #main.statictext45, "!font lucida_console 0 14 bold"
    print #main.statictext46, "!font lucida_console 0 14 bold"
    print #main.statictext47, "!font lucida_console 0 14 bold"
    print #main.statictext48, "!font lucida_console 0 14 bold"

timer 100, [main.inputLoop]
[main.inputLoop]   'wait here for input event
    PRINT #main.textbox1, "!contents? Input1"
    PRINT #main.textbox2, "!contents? Input2"
    PRINT #main.textbox3, "!contents? Input3"

    PRINT #main.textbox4, "!contents? Input4"
    PRINT #main.textbox5, "!contents? Input5"
    PRINT #main.textbox6, "!contents? Input6"

if Input5 <> 0 then

let value1=(Input2*Input6)/100
let value2=(Input2*Input3)/100
let value3=Input4
let value4=Input5-value2-value1-value3
let value5=(value4/Input2)*100
let value6=(value4/(100-Input2))*100
let value7=(Input5-Input4)/(Input6+Input3)*100
let value8=(Input1-Input5)
let value9=(100/value7)*100

end if

    mask$="#,###,###.##"

    print #main.statictext40, using(mask$,value1)
    print #main.statictext41, using(mask$,value2)
    print #main.statictext42, using(mask$,value3)
    print #main.statictext43, using(mask$,value4)
    print #main.statictext44, using(mask$,value5)
    print #main.statictext45, using(mask$,value6)
    print #main.statictext46, using(mask$,value7)
    print #main.statictext47, using(mask$,value8)
    print #main.statictext48, using(mask$,value9)

    wait

[quit]
    Close #main
    END 

Richard.
Re: question on timer -- memory usage
Post by hammerjit on Sep 2nd, 2015, 08:56am

Thanks Richard...that was great help.