Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: A collection of garbage

From: carl:rebol at: 8-Jul-2003 20:06

The logic is fairly complicated, but it's also quite fast. For example, you might be using 50MB of memory, but not even notice when the GC happens. There are many REBOL programs that run for months (years?) at a time without restarting, so we know the GC is stable. (There are a few exceptions, in operations like TRY on OPEN failures, etc.) If you're curious about your code's memory, just put your code into a loop and print memory stats every so often. (Use a newer version of REBOL for better accuracy on stats.) loop 10000 [ your code print ["Mem:" stats] ] You'll see the mem usage top out after a while (unless you are doing things like continuously appending to series, such as when keeping a log). The two basic memory methods in REBOL are: * Small allocations (values, blocks, etc.) are managed by REBOL for speed reasons. They are efficiently recycled. * Large allocations (files, images, sounds, big blocks, etc.) are allocated thru the native OS, because we figure it knows better the characteristics of its VM paging model and can optimize the allocation. So, here's what programmers need to remember: * If you're just using normal REBOL values and small series, just sit back and relax. * If you allocate large memory objects (strings, images, blocks, etc.), you may want to un-reference them when you are done with them. For example: file: read %large-file.txt do some-stuff-on-file file: none ; allow file buffer to GC eventually This is not required, but it will help reduce memory usage. IMPORTANT: in the example above, if the file variable is a local variable to a function, DO NOT ASSUME that it will be un-referenced automatically. By default, the first frame (first level of call) of local variables for a function remain referenced. For example, if you have a function: foo: has [file] [ file: read %large-file.txt print length? file ] The file memory will not be released until the local variable is set to a different value (by a second call to foo, which will then hold the file memory until the next call, etc.) To change that, unreference the file series when done: foo: has [file] [ file: read %large-file.txt print length? file file: none ] -Carl