This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
クデル's profile picture

Checking how much memory is used

Started by クデル, 07 July 2016 - 07:08 AM
クデル #1
Posted 07 July 2016 - 09:08 AM
I am writing a simple interpreter, and for the sake I of it I am implementing some memory limiting. Is there another way to tell how much memory is being used, besides dumping it to a file? surely that method is also inaccurate too considering table structures etc?
The Crazy Phoenix #2
Posted 07 July 2016 - 09:53 AM
http://www.computerc...Fs.getFreeSpace

http://www.computerc...wiki/Fs.getSize

Check the wiki next time. If you're looking for another type of memory, you can't.
Edited on 07 July 2016 - 07:54 AM
LBPHacker #3
Posted 07 July 2016 - 10:55 AM
It really is the table (and other) structures, the size of which you cannot control or even query, that make it next to impossible, unless you have a VM with its own virtual memory (probably a huge string). Also, how would you dump it into a file?

Check the wiki next time. If you're looking for another type of memory, you can't.
The OP might have checked the wiki and found nothing while searching for memory usage, which has nothing to do with the filesystem. Finding nothing on a wiki is among the things that make people ask questions.
Edited on 07 July 2016 - 09:02 AM
Bomb Bloke #4
Posted 07 July 2016 - 12:33 PM
Assuming your interpreter works by converting incoming source to Lua and then loadfile'ing it, I can't see any way to do this short of adding tracking code into the converted string before the loadfile call.

Probably the easiest way to do that is to make your language use static types; rig things such that coders don't get to assign to a variable / key until they declare it, each declaration carries a "cost", and once the function hits it's "cost limit", that's that! This way, the users of your language end up doing most of the work for you.
Edited on 07 July 2016 - 10:34 AM
The Crazy Phoenix #5
Posted 07 July 2016 - 01:06 PM
Out of curiosity, how would you force someone to run your declaration function?
Bomb Bloke #6
Posted 07 July 2016 - 01:39 PM
Whenever the interpreter (the bit that builds the Lua code that'll eventually be loadstring'd) reads code attempting an assignment, check to see if declaration code has previously been encountered for the destination.

If not, error.
クデル #7
Posted 07 July 2016 - 01:55 PM
Thank you BB & LBP, despite converting to Lua I think I'm just going to stick with adding tracking code, my only concern is if this would be intensive with "large" programs? I doubt it would be, but I just want to make sure. :)/>
Bomb Bloke #8
Posted 08 July 2016 - 12:50 AM
You'll tend to spend more resources tracking resource usage than the original resources would've used in the first place, I'd assume. Whether that's a "problem" is really up to you.