-
Notifications
You must be signed in to change notification settings - Fork 117
Automatic memory management
MY-BASIC uses both Reference Counting and Garbage Collection to manage memory automatically. It's necessary to introduce these techniques to MY-BASIC since many other advanced features are based on automatic memory management.
Some data types such as integer, real, etc. are value types, that it copies the value directly when assigning a value to a variable, and it disposes the old value.
Besides, MY-BASIC supports referenced data types as well, such as: collections, class instance, lambda and referenced usertype, etc. A referenced value maintains a reference count, the initialized count value is 1 when it's created, the reference count increases by 1 when it's referenced by another object, and decreases by 1 when unreferenced by an object; objects referencing the same value share the raw value instead of copying it, MY-BASIC disposes the value when the reference count reaches 1 (none referencing).
Reference counting solved most automatic disposing cases, on the other hand it introduces an new issue of cycled referencing; for instance two values reference each other but are not referenced by other values, the reference counts of them never reach 1 which will cause memory leak. So as a supplement I added garbage collection to MY-BASIC. MY-BASIC does a mark-sweep GC algorithm when unreferencing operation occurred for some times; it iterates all referenced values from the root scope to detect unreachable values for later disposing. It solves leakage properly.
For testing:
k = list() ' k references a list
l = list() ' l references a list
push(l, k) ' l references k
push(k, l) ' k references l, got a cycle
k = nil ' Unreference the list value of k
l = nil ' Unreference the list value of l
It won't generate leakage in MY-BASIC.
It's possible to configure GC in MY-BASIC, for more information read the Customizing macros page.
- Principles
- Coding
- Data types
- Standalone shell
- Integration
- Customization
- More scripting API
- FAQ