Memory in Meem
In short: memory in Meem is manual, close to Zig’s model. There is no reference counting and no garbage collector (reference counting was removed in 0.7.0). Memory is allocated from an allocator; you give it back with حرر (free), or all at once with an arena, and خاتمه (defer) postpones giving it back until the block exits. Whatever you do not give back returns to the system when the program exits.
The tools, in order
Section titled “The tools, in order”- The allocator. Every string you build and every list and dictionary is allocated from an allocator. Builtins that build something take it as their last argument; if you leave it out, it comes from
الافتراضي(the default). The built-in allocators includeالنظام(the system),الافتراضي,ساحة(arena),كاشف(detector) andمخزن_ثابت(fixed buffer).بلا_افتراضي(no default) at the top of a module forbids allocating from the default allocator in it. حرر(س)gives back what was allocated forسimmediately, and makesسلاشيء(nothing). It does not follow what is inside: freeing a list of lists leaves the inner lists.خاتمه جملةdefers the statement until the block exits, on every exit: the last line, an earlyارجع(return), or a failingجرّب(try). Deferred statements run in the reverse order of writing:
داله عالج(ن عدد64) نص: اطبع("فتح المورد ", ن) خاتمه اطبع("إغلاق المورد ", ن) خاتمه اطبع("تنظيف أول") اذا ن < 0: ارجع "سالب" اطبع("عمل على ", ن) ارجع "تم"
اطبع(عالج(5))اطبع("---")اطبع(عالج(-1))- An arena collects what is allocated from it, and
اعد(reset) gives it all back at once. It suits a phase in which you build a lot and then finish with all of it:
مجموع = 0لكل دورة في مدى(0, 3): ساحه = ساحة(الافتراضي) خاتمه اعد(ساحه) كلمات = تقسيم(دمج_نصوص("ا ب ج ", دورة, ساحه), " ", ساحه) مجموع += طول(كلمات)اطبع("كلمات في الدورات الثلاث: ", مجموع)- The detector is an allocator that watches what is allocated and freed. It is on by default in a direct run (
meem برنامج.م) since 0.10.0, and--debug-memadds a report at the end of the program listing every line that allocated and did not free:
لكل ع في مدى(0, 3): ن = "سطر " + نص(ع) اطبع(ن)The two modes: development and release
Section titled “The two modes: development and release”Direct run meem برنامج.م |
Built file meem build |
|
|---|---|---|
| Default | With the detector, no report at the end | Without the detector |
| The opposite option | --release: no detector |
--debug or --debug-mem: with the detector |
| Report of what was not freed | with --debug-mem |
with --debug-mem |
So development with a direct run is protected, and the built file is fast, like Zig’s Debug and ReleaseFast modes. The detector has a cost: on the benchmark programs in the compiler repository it was 0% to 32% slower, and memory freed in its mode is not returned to the system (it is quarantined so that use after free can be caught), so a long-running program that frees a lot is run with --release.
What the compiler guarantees in every mode
Section titled “What the compiler guarantees in every mode”Outside خطر (unsafe) and بلا_فحص (unchecked), with or without the detector:
Arithmetic and indexing. Overflowing the type’s width (عدد64 and the others), an index outside a list or string, division by zero, and unwrapping لاشيء are all orderly runtime errors with code 3, an ID and a line, not undefined behaviour:
ق = [10, 20, 30]اطبع(ق[2])اطبع(ق[3])Exit code: 3
In the error catalog: E0321
كبير = 9223372036854775807اطبع(كبير)اطبع(كبير + 1)Exit code: 3
In the error catalog: E0341
A list or dictionary used through the same name after حرر. حرر(ق) makes ق لاشيء, and using it afterwards is error E0428, in a direct run and in a built file alike (in 0.9.1 it was a crash with SIGSEGV):
ق = [1, 2, 3]حرر(ق)اطبع(طول(ق))Exit code: 3
In the error catalog: E0428
Also built with meem build when the site was built: a static file of 37.2 kB, run on its own, and its output matched the output above.
No allocation you did not write. A program allocates only what you see in the code: a builtin that takes an allocator (written, or left to the default), + on strings, or a literal [...] or {...}. Operations on a «view» (the result of جزء, قص_الفراغات, مسارات.الامتداد…) do not copy: طول, comparison, indexing, لكل, and a dictionary key. Here is a loop of a thousand rounds with --debug-mem, and there is no report at its end because nothing was left:
جملة = " ميم لغة عربية "مجموع = 0لكل ع في مدى(0, 1000): ك = قص_الفراغات(جملة) مجموع += طول(ك) اذا ك == "ميم لغة عربية": مجموع += 1اطبع(مجموع)The only exception: growing a string with ن += ذيل (see «what it does not guarantee» below).
What the compiler rejects or warns about before running (since 0.13.0)
Section titled “What the compiler rejects or warns about before running (since 0.13.0)”The promise, in the words of the memory reference: «ميم تحرر ما تبنيه في وسط التعبير بنفسها، وتحذّرك من البرنامج الذي يضيّع حجزاً محلياً لم يُحرَّر ولم يخرج من دالته (وترفضه منذ 0.14.0)، ومما تعجز عن إثبات تحريره؛ وما بقي فعليك، والكاشف يدلّك عليه.» (Meem frees what you build in the middle of an expression by itself, warns you about a program that loses a local allocation that was neither freed nor passed out of its function, and rejects it since 0.14.0, and about what it cannot prove is freed; the rest is up to you, and the detector points you to it.)
| Message | When | Mode |
|---|---|---|
| E0472 | An allocation assigned to a local in a function, which does not leave the function and is not named in any حرر: it is lost on every call |
Compile error (since 0.14.0) |
| E0471 | No proof it is freed: it is freed on some paths only, or before an early exit, or reassigned in a loop without freeing the previous one | A warning shown only with --warn-unproven (since 0.17.0) |
| E0473 | حرر on a list from تقسيم gives back its head, and its parts stay |
A warning shown only with --warn-unproven (since 0.17.0) |
--strict-memory (since 0.17.0) makes «no proof of freeing» (E0471 and E0473) a compile error: optional for your program, and the default for the standard library only. The fix is usually one line after the allocation: خاتمه حرر(س), or ارجع س if it belongs to the caller. The promise has limits: whatever leaves the function (returned, stored, passed to something that may keep it, or to خارجي) is not judged by the language, there is no analysis of pointers, of خطر or of C, and the analysis stays silent when in doubt.
And --debug-mem=fail (since 0.13.0) turns whatever is left at exit into a failure with code 4, for tests.
What only the detector catches
Section titled “What only the detector catches”When the mistake passes through another name that the compiler cannot know about at compile time, the detector catches it at run time, with a message giving the line of creation and the line of freeing. These examples were all run in the default direct run, with no options:
A list held by two names, freed through one and used through the other (E0429):
ا = [7, 8, 9]ب = احرر(ا)ج = [1, 2, 3, 4, 5, 6]اطبع(ب[0], " ", طول(ب))حرر(ج)Exit code: 3
In the error catalog: E0429
A list from an arena used after اعد on its arena (E0429 as well):
ساحه = ساحة(الافتراضي)ق = قائمة_من([1, 2, 3], ساحه)اعد(ساحه)اطبع(ق[0])Exit code: 3
In the error catalog: E0429
A view onto a string whose original was freed (E0430):
مسار = "ملفات/تقرير.md"ن = مسار + ""امتداد = جزء(ن, 10)حرر(ن)اطبع(امتداد)Exit code: 3
In the error catalog: E0430
And a string freed twice through two names (E0431). A double free of a container is E0350, and of a slot or block E0421:
ن = "مرحبا " + نص(1)م = نحرر(ن)حرر(م)Exit code: 3
In the error catalog: E0431
In detector mode, freed memory is filled with the byte 0xDF, so a read the detector did not check sees obvious garbage, not the old data intact.
What it does not guarantee
Section titled “What it does not guarantee”- A built file without the detector. The four previous examples, in a file built with
meem buildwithout--debug, are undefined behaviour: use after free through another name may print old output or garbage, and a double free through another name crashes the program. That is why we do not publish their output there. Try your program with a direct run, or build it with--debug, before building it for release. - What does not run is not checked. The detector works at run time, and the compiler’s analysis before running (above) does not track aliases or pointers. A branch your tests never reach is never checked.
حررdoes not follow what is inside. Freeing a list of lists, a struct holding lists, or a list of strings fromتقسيمdoes not free what they hold. For nested structures, use an arena, or a deep free you write yourself.- Growing with
+=leaves the old buffer behind. A bounded leak: about the base-2 logarithm of the length in buffers, totalling about twice the final length (allocation and performance of builtins). - Inside
خطر: raw pointers,بلا_فحص,حوّل(cast) and C memory are the writer’s responsibility, and the detector does not see C memory. - Threads: the language has no threads, and the allocators are not atomic.
- Runtime errors do not run
خاتمه. A runtime error (code 3) ends the program immediately, likepanicin Zig: the system reclaims the memory, but a temporary file that aخاتمهwould have deleted stays on disk. An expected error is returned withارجع خطأ.اسم, and then the deferred statements do run.
See also
Section titled “See also”Lesson 20: allocators and arenas · Memory reference · What is خطر? · Meem compared with C and Zig