Skip to content
العربية

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.

Your programDefault allocatorBlock by block: حرر(س)ArenaAll at once: اعد(ساحه)What you do not give back stays until exit
Every allocation comes from an allocator: from the default one you give back block by block, and an arena gives back everything in it at once.
  1. 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.
  2. حرر(س) 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.
  3. خاتمه جملة 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:
خاتمه on every exit, in reverse order
داله عالج(ن عدد64) نص:
اطبع("فتح المورد ", ن)
خاتمه اطبع("إغلاق المورد ", ن)
خاتمه اطبع("تنظيف أول")
اذا ن < 0:
ارجع "سالب"
اطبع("عمل على ", ن)
ارجع "تم"
اطبع(عالج(5))
اطبع("---")
اطبع(عالج(-1))
Output recorded from the compilermeem ذاكرة_خاتمه.م · meem 0.17.3+434e13f
فتح المورد 5عمل على 5تنظيف أولإغلاق المورد 5تم---فتح المورد -1تنظيف أولإغلاق المورد -1سالب
  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:
An arena for each round
مجموع = 0
لكل دورة في مدى(0, 3):
ساحه = ساحة(الافتراضي)
خاتمه اعد(ساحه)
كلمات = تقسيم(دمج_نصوص("ا ب ج ", دورة, ساحه), " ", ساحه)
مجموع += طول(كلمات)
اطبع("كلمات في الدورات الثلاث: ", مجموع)
Output recorded from the compilermeem 16_ساحة.م · meem 0.17.3+434e13f
كلمات في الدورات الثلاث: 12
  1. 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-mem adds a report at the end of the program listing every line that allocated and did not free:
A leak that --debug-mem reveals by line
لكل ع في مدى(0, 3):
ن = "سطر " + نص(ع)
اطبع(ن)
Output recorded from the compilermeem --debug-mem 16_تسرب.م · meem 0.17.3+434e13f
سطر 0سطر 1سطر 2
[ذاكرة] معلومة: كتل لم تُحرَّر من الافتراضي: 3 (75 بايت)، تعود للنظام عند الخروج  سطر 2: 3 كتل، 75 بايت  تلميح: الحجز المتكرر في سطر واحد (حلقة في الغالب) يُعاد بـ خاتمه حرر(...) بعد إنشائه، أو بساحة للمرحلة
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:

Index out of bounds
ق = [10, 20, 30]
اطبع(ق[2])
اطبع(ق[3])
Output recorded from the compilermeem ذاكرة_فهرس.م · meem 0.17.3+434e13f
30
[خطأ تشغيل E0321] سطر 3:7 - الفهرس 3 خارج الحدود (الحجم 3)  اطبع(ق[3])

Exit code: 3

In the error catalog: E0321

Overflowing عدد64
كبير = 9223372036854775807
اطبع(كبير)
اطبع(كبير + 1)
Output recorded from the compilermeem ذاكرة_تجاوز.م · meem 0.17.3+434e13f
9223372036854775807
[خطأ تشغيل E0341] سطر 3:11 - تجاوز النوع 'عدد64' في العملية '+'  اطبع(كبير + 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):

Using a list after freeing it, through the same name
ق = [1, 2, 3]
حرر(ق)
اطبع(طول(ق))
Output recorded from the compilermeem ذاكرة_نفس_الاسم.م · meem 0.17.3+434e13f
[خطأ تشغيل E0428] سطر 3:10 - الحاوية 'ق' استُعملت بعد تحريرها (قيمتها لاشيء)  اطبع(طول(ق))

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:

Operations on a view do not allocate
جملة = " ميم لغة عربية "
مجموع = 0
لكل ع في مدى(0, 1000):
ك = قص_الفراغات(جملة)
مجموع += طول(ك)
اذا ك == "ميم لغة عربية":
مجموع += 1
اطبع(مجموع)
Output recorded from the compilermeem --debug-mem ذاكرة_لا_نسخ.م · meem 0.17.3+434e13f
14000

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.

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):

Using a list after freeing it, through another name
ا = [7, 8, 9]
ب = ا
حرر(ا)
ج = [1, 2, 3, 4, 5, 6]
اطبع(ب[0], " ", طول(ب))
حرر(ج)
Output recorded from the compilermeem ذاكرة_بعد_التحرير.م · meem 0.17.3+434e13f
[خطأ تشغيل E0429] سطر 5:7 - استعمال القائمة أو القاموس بعد التحرير (أُنشئت في السطر 1 وحُرّرت في السطر 3)  اطبع(ب[0], " ", طول(ب))

Exit code: 3

In the error catalog: E0429

A list from an arena used after اعد on its arena (E0429 as well):

An arena after اعد
ساحه = ساحة(الافتراضي)
ق = قائمة_من([1, 2, 3], ساحه)
اعد(ساحه)
اطبع(ق[0])
Output recorded from the compilermeem ذاكرة_ساحة_بعد_اعد.م · meem 0.17.3+434e13f
[خطأ تشغيل E0429] سطر 4:7 - استعمال القائمة أو القاموس بعد التحرير (أُنشئت في السطر 2 وحُرّرت في السطر 3)  اطبع(ق[0])

Exit code: 3

In the error catalog: E0429

A view onto a string whose original was freed (E0430):

A dangling view
مسار = "ملفات/تقرير.md"
ن = مسار + ""
امتداد = جزء(ن, 10)
حرر(ن)
اطبع(امتداد)
Output recorded from the compilermeem ذاكرة_عرض_معلق.م · meem 0.17.3+434e13f
[خطأ تشغيل E0430] سطر 5:1 - استعمال النص بعد التحرير (أُنشئت في السطر 2 وحُرّرت في السطر 4)  اطبع(امتداد)

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:

A double free through another name
ن = "مرحبا " + نص(1)
م = ن
حرر(ن)
حرر(م)
Output recorded from the compilermeem ذاكرة_تحرير_مزدوج.م · meem 0.17.3+434e13f
[خطأ تشغيل E0431] سطر 4: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.

  • A built file without the detector. The four previous examples, in a file built with meem build without --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, like panic in 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.

Lesson 20: allocators and arenas · Memory reference · What is خطر? · Meem compared with C and Zig