Skip to content
العربية

Manual memory

To know where memory comes from in Meem, and when you give it back yourself.

  • Every string you build, and every list and dictionary, takes a place in memory from an allocator, which is where memory is taken from and given back to. If you name no allocator, the allocation comes from the default allocator.
  • Since 0.7, Meem has neither reference counting nor a garbage collector. Whatever is not freed returns to the system when the program exits.
  • So a short program needs nothing. A long program gives back what it is done with:
    • حرر(س) (free) gives back the string, list or dictionary immediately, and makes س لاشيء.
    • خاتمه جملة (defer) postpones the statement until the block exits, so if you write خاتمه حرر(ن) in a loop, the space of ن is given back at the end of each round.
Example
لكل ع في مدى(0, 3):
ن = "سطر " + نص(ع)
خاتمه حرر(ن)
اطبع(ن)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
سطر 0سطر 1سطر 2

Freeing the same value twice is a runtime error:

Causes the error
م = جديد(1)
حرر(م)
حرر(م)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
[خطأ تشغيل E0349] سطر 3:1 - تحرير مؤشر قيمته لاشيء (هل حُرّر من قبل؟)  حرر(م)

Exit code: 3

In the error catalog: E0349

Fixes it
م = جديد(1)
حرر(م)
اطبع(م == لاشيء)
Output recorded in the reference handbookmeem برنامج.م · 0.17.1
صواب
  • An arena (ساحة(الاصل)) is an allocator that collects what is allocated from it, and اعد(ساحه) (reset) gives it all back at once.
  • The detector (كاشف(الاصل)) is an allocator that reports what was not freed from it.
  • In builtins and functions, a last argument of type مخصص (allocator) is optional, and what the function builds is allocated from it.
  • The detector is on by default in a direct run (since 0.10.0); --debug-mem adds a report of what was not freed, with the allocation line and the call line, and --debug-mem=fail turns whatever is left into a failure with code 4 (since 0.13.0).
  • Before running (since 0.13.0): the compiler frees temporaries by itself, a local allocation in a function that is neither freed nor passed out of it is compile error E0472 (since 0.14.0), and «لا دليل على تحرير» (no proof of freeing, E0471) is shown with --warn-unproven and becomes an error with --strict-memory (since 0.17.0).

The full table, the examples and the limits are in the memory reference: allocators and خاتمه.

Release 0.6 counted references, and release 0.7 does not. Details in migrating from 0.6 to 0.7, and the old model is described in reference counting (historical).

The beginner’s memory lesson · The low-level layer · Glossary