Timing

Hi (probably a question for Rob),

The app in question is a cpu simulator which allows the user to write programs in the simulated computer and then either a) fetch and run one instruction (single step) or run the program until a stop instruction is reached.

After advice from Rob I solved a very early problem in timing when I couldn’t get the while do loop function correctly for the obvious (now) reason that the loops whizzed through and fired off all the iterations and didn’t wait for the screen drawing, validation code etc.

The loop is now: (not all in proper lua code to save space here)

function run_program()

notfinished = true

… various bits of setup code

runloop = timer.performWithDelay(1000,singlestep,0)

various end code bits

end

function singlestep ()

   if notfinished then

       fetch_instruction()

       timer.perfromWithDelay(500,execute_instruction)

  else

     tidyup code here

  end

end

end

This worked great. Potentially the program could be up to 255 instructions but is usually shorter, also as some of the instructions cause loops the program could be infinite (there is an interrupt button that sets notfinished = false).

What I have discovered recently was that when the program gets long (say 50 steps) the whole app slows down and effectively freezes). With even a simple 2 instruction program that adds 1 to the accumulator (in the app) and then executes a goto instruction that takes it back to the start (ie adding 1 to the acc continually) the app works perfectly for about 40 iterations then begins to freeze.

My thoughts are that it is running out of some resource (timers?) or filling memory with uncollected garbage that eventually overflows? and then eventually impacts on the program.

Very few users would ever see the bug, but it is not elegant or even honest to leave it in.

Anyone see the flaw (and a solution)???

regards

Martin

  • Obviously a very difficult problem! Not even one comment in almost a month

Well I suspect one reason you may  not have gotten a response is you flagged me as the one to answer and unfortunately, its too easy for a single post to slip through and I may miss it.  We have a great community and asking a specific person to answer you, really limits the possibilities.

I would recommend printing out your Lua memory with collectgarbage(“count”) and getting the current texture memory (I don’t have that code in the top of my head).   The collectgarbage(“count”) prints memory in “Kbytes”.  The other memory prints in bytes… 

Rob

Fair point. I wasn’t asking you to answer, just “expecting” you would know about this given your previous tutorials and masterclasses etc.

I was sure it is a memory issue and also that I have left something constantly reducing memory. I have done as you suggested and the amount used by Lua goes up by 200 (k) each loop.

I suspect I know where the issue is.

Thanks Rob,

by moving the collectgarbage around I was able to narrow down then identify the offending piece of code. I was continuously creating a new screen object which over time created the problem.

Martin

I assumed (incorrectly) that recreating the same object with the same name would “overwrite” the old data in memory. it didn’t and created a memory leak that got bigger and bigger.

  • Obviously a very difficult problem! Not even one comment in almost a month

Well I suspect one reason you may  not have gotten a response is you flagged me as the one to answer and unfortunately, its too easy for a single post to slip through and I may miss it.  We have a great community and asking a specific person to answer you, really limits the possibilities.

I would recommend printing out your Lua memory with collectgarbage(“count”) and getting the current texture memory (I don’t have that code in the top of my head).   The collectgarbage(“count”) prints memory in “Kbytes”.  The other memory prints in bytes… 

Rob

Fair point. I wasn’t asking you to answer, just “expecting” you would know about this given your previous tutorials and masterclasses etc.

I was sure it is a memory issue and also that I have left something constantly reducing memory. I have done as you suggested and the amount used by Lua goes up by 200 (k) each loop.

I suspect I know where the issue is.

Thanks Rob,

by moving the collectgarbage around I was able to narrow down then identify the offending piece of code. I was continuously creating a new screen object which over time created the problem.

Martin

I assumed (incorrectly) that recreating the same object with the same name would “overwrite” the old data in memory. it didn’t and created a memory leak that got bigger and bigger.