Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Eunomiac

How to Purge All Memory w/o Reboot?

Question

When I'm testing a program, my standard "workflow" is to Ctrl-Alt-C out of the program and simply re-run it, whenever I want to test new code.

 

I know to use package.loaded[library] = nil to unload libraries, but I'm running into two other issues after I started playing around with event.timer():

  • How do I cancel or purge all timers in response to a Ctrl-Alt-C interrupt?  Is there something similar to package.loaded[lib] = nil; library = require(lib) that I can add to the beginning of my programs to wipe out timers from previous runs?
  • Some elements (I'm not sure which) of my programs are persisting in memory after Ctrl-Alt-C'ing out, resulting in the occasional out-of-memory error if I don't reboot before restarting the program.  I suppose a broader question that would encompass the first bullet might be "is there a command I can call to "purge" everything from the interrupted run of my script, ensuring each time I call my main program it begins in a completely clear environment?"

Thanks!

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

Looking at the source code for event.lua it looks like you should be able to clear all timers like this:

local event = require("event")
event.timers = {}

Be aware that this might break things relying on timers - I don't believe the default OpenOS shell has any background timers running, though the same might not be true for other scripts.

Link to post
Share on other sites
  • 0
  • Solution

The timer list is a local variable, it's not in the table returned by require("event"), so that won't work. In general this isn't trivial, because a number of libraries depend on the event library to work (such as for keeping a primary component to name one). The best bet would probably wrapping your actual program code in a pcall and clear up any timers / event listeners after that returns. I.e.
 

function main(...)
  -- Your actual code.
end
 
pcall(main, ...)
-- Clean up timers, listeners, ...
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.