Garbage Collection issue

@dave1707 - I’ve been playing around with a project recently and it started to crash, reported to @sim @John via the crash report, but I suddenly realised that it was a little bad coding from me. Not in my project as such it was down to garbage monitoring - I used print(collect.garbage()) - if you do so without using output.clear() it eventually slows down touch response etc and eventually leads to a crash.

I think what we need is a routine to collect garbage and print it out with a measure of the change in garbage size so that you can see the level and if it is increasing. What I’ve got in mind is fairly simple - have you already approached this ? got a routine sorted ?

Edit: tried this:

Put


garbageStart = collectgarbage()

at the end of setup(). Then called


function garbageMonitor()
    --
    local crap, diff
    crap = collectgarbage()
    diff = crap - garbageStart
    output.clear()
    print("Latest "..crap)
    print(100*diff/garbageStart)
end

at the end of draw(), it fired out an answer of 0 and nan.

What on Earth is nan - definitely not my granny !!!

Edit 2: just thought - is it Not A Number .

@Bri_G Are you after something like this. NAN is not a number. You guessed it.


function setup()
    fill(255)
end

function draw()
    background(0)
    text("KB Memory used  "..collectgarbage("count")//1,WIDTH/2,HEIGHT-100)
    print("uu")
end

@dave1707 - that’s intriguing. The obvious text() instead of print, which is great during development - I should have thought of that.

The print(“uu”) had me a little puzzled, the problem with the print statement is it prints a line and carriage return so the scrollable output file printed can eventually cause an error or crash. Could Codea be built so that the output file has a finite length after which it fires up a message and stops printing?

Does the print(“uu”) involve no CR ? Or does it overwrite ? Can we convert output to strings and print that ?

Or is this some king of control character which doesn’t add a newline ?

All I got for that was the incrementing count if the draw() loop printed up.

@Bri_G The whole purpose if the print(“up”) was to start chewing up memory so the garbagecollect would show memory being used.

Here’s an interesting run. Didn’t think this would happen.


function setup()
    fill(255)
    a="a"
end

function draw()
    background(0)
    text("KB Memory used  "..collectgarbage("count")//1,WIDTH/2,HEIGHT-100)
    a=a..a
end

@dave1707 - interesting if you make the last a as “a” it counts up much slower.

Then again your doubling the variable an each time you run this rather than appending a string !!! Subtleties !!!