gc.rst 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. :mod:`gc` -- control the garbage collector
  2. ==========================================
  3. .. module:: gc
  4. :synopsis: control the garbage collector
  5. |see_cpython_module| :mod:`python:gc`.
  6. Functions
  7. ---------
  8. .. function:: enable()
  9. Enable automatic garbage collection.
  10. .. function:: disable()
  11. Disable automatic garbage collection. Heap memory can still be allocated,
  12. and garbage collection can still be initiated manually using :meth:`gc.collect`.
  13. .. function:: collect()
  14. Run a garbage collection.
  15. .. function:: mem_alloc()
  16. Return the number of bytes of heap RAM that are allocated.
  17. .. admonition:: Difference to CPython
  18. :class: attention
  19. This function is MicroPython extension.
  20. .. function:: mem_free()
  21. Return the number of bytes of available heap RAM, or -1 if this amount
  22. is not known.
  23. .. admonition:: Difference to CPython
  24. :class: attention
  25. This function is MicroPython extension.
  26. .. function:: threshold([amount])
  27. Set or query the additional GC allocation threshold. Normally, a collection
  28. is triggered only when a new allocation cannot be satisfied, i.e. on an
  29. out-of-memory (OOM) condition. If this function is called, in addition to
  30. OOM, a collection will be triggered each time after *amount* bytes have been
  31. allocated (in total, since the previous time such an amount of bytes
  32. have been allocated). *amount* is usually specified as less than the
  33. full heap size, with the intention to trigger a collection earlier than when the
  34. heap becomes exhausted, and in the hope that an early collection will prevent
  35. excessive memory fragmentation. This is a heuristic measure, the effect
  36. of which will vary from application to application, as well as
  37. the optimal value of the *amount* parameter.
  38. Calling the function without argument will return the current value of
  39. the threshold. A value of -1 means a disabled allocation threshold.
  40. .. admonition:: Difference to CPython
  41. :class: attention
  42. This function is a MicroPython extension. CPython has a similar
  43. function - ``set_threshold()``, but due to different GC
  44. implementations, its signature and semantics are different.