@@ -119,10 +119,10 @@ symbols that have already been defined, e.g. ``1 << BIT``.
119119
120120Where there is a substantial volume of constant data and the platform supports
121121execution from Flash, RAM may be saved as follows. The data should be located in
122- Python modules and frozen as bytecode. The data must be defined as `` bytes ` `
123- objects. The compiler 'knows' that `` bytes ` ` objects are immutable and ensures
122+ Python modules and frozen as bytecode. The data must be defined as `bytes `
123+ objects. The compiler 'knows' that `bytes ` objects are immutable and ensures
124124that the objects remain in flash memory rather than being copied to RAM. The
125- `` ustruct `` module can assist in converting between `` bytes ` ` types and other
125+ `ustruct ` module can assist in converting between `bytes ` types and other
126126Python built-in types.
127127
128128When considering the implications of frozen bytecode, note that in Python
@@ -185,7 +185,7 @@ a file it will save RAM if this is done in a piecemeal fashion. Rather than
185185creating a large string object, create a substring and feed it to the stream
186186before dealing with the next.
187187
188- The best way to create dynamic strings is by means of the string `` format ` `
188+ The best way to create dynamic strings is by means of the string `format `
189189method:
190190
191191.. code ::
@@ -226,26 +226,26 @@ function ``foo()``:
226226 foo(b'\1\2\xff')
227227
228228 In the first call a tuple of integers is created in RAM. The second efficiently
229- creates a `` bytes ` ` object consuming the minimum amount of RAM. If the module
230- were frozen as bytecode, the `` bytes ` ` object would reside in flash.
229+ creates a `bytes ` object consuming the minimum amount of RAM. If the module
230+ were frozen as bytecode, the `bytes ` object would reside in flash.
231231
232232**Strings Versus Bytes **
233233
234234Python3 introduced Unicode support. This introduced a distinction between a
235235string and an array of bytes. MicroPython ensures that Unicode strings take no
236236additional space so long as all characters in the string are ASCII (i.e. have
237- a value < 126). If values in the full 8-bit range are required `` bytes ` ` and
238- `` bytearray ` ` objects can be used to ensure that no additional space will be
239- required. Note that most string methods (e.g. `` strip() `` ) apply also to `` bytes ` `
237+ a value < 126). If values in the full 8-bit range are required `bytes ` and
238+ `bytearray ` objects can be used to ensure that no additional space will be
239+ required. Note that most string methods (e.g. :meth: ` str. strip() `) apply also to `bytes `
240240instances so the process of eliminating Unicode can be painless.
241241
242242.. code ::
243243
244- s = 'the quick brown fox' # A string instance
245- b = b'the quick brown fox' # a bytes instance
244+ s = 'the quick brown fox' # A string instance
245+ b = b'the quick brown fox' # A bytes instance
246246
247- Where it is necessary to convert between strings and bytes the string `` encode ` `
248- and the bytes `` decode ` ` methods can be used. Note that both strings and bytes
247+ Where it is necessary to convert between strings and bytes the :meth: ` str. encode `
248+ and the :meth: ` bytes. decode ` methods can be used. Note that both strings and bytes
249249are immutable. Any operation which takes as input such an object and produces
250250another implies at least one RAM allocation to produce the result. In the
251251second line below a new bytes object is allocated. This would also occur if ``foo ``
@@ -258,10 +258,10 @@ were a string.
258258
259259 **Runtime compiler execution **
260260
261- The Python keywords `` eval `` and `` exec ` ` invoke the compiler at runtime, which
262- requires significant amounts of RAM. Note that the `` pickle `` library employs
263- `` exec `` . It may be more RAM efficient to use the `` json `` library for object
264- serialisation.
261+ The Python funcitons ` eval ` and `exec ` invoke the compiler at runtime, which
262+ requires significant amounts of RAM. Note that the `pickle ` library from
263+ `micropython-lib ` employs ` exec `. It may be more RAM efficient to use the
264+ ` ujson ` library for object serialisation.
265265
266266**Storing strings in flash **
267267
@@ -300,7 +300,7 @@ from a fixed size pool known as the heap. When the object goes out of scope (in
300300other words becomes inaccessible to code) the redundant object is known as
301301"garbage". A process known as "garbage collection" (GC) reclaims that memory,
302302returning it to the free heap. This process runs automatically, however it can
303- be invoked directly by issuing `` gc.collect() ` `.
303+ be invoked directly by issuing `gc.collect() `.
304304
305305The discourse on this is somewhat involved. For a 'quick fix' issue the
306306following periodically:
@@ -332,7 +332,7 @@ Reporting
332332~~~~~~~~~
333333
334334A number of library functions are available to report on memory allocation and
335- to control GC. These are to be found in the `` gc `` and `` micropython ` ` modules.
335+ to control GC. These are to be found in the `gc ` and `micropython ` modules.
336336The following example may be pasted at the REPL (``ctrl e `` to enter paste mode,
337337``ctrl d `` to run it).
338338
@@ -357,17 +357,17 @@ The following example may be pasted at the REPL (``ctrl e`` to enter paste mode,
357357
358358 Methods employed above:
359359
360- * `` gc.collect() ` ` Force a garbage collection. See footnote.
361- * `` micropython.mem_info() ` ` Print a summary of RAM utilisation.
362- * `` gc.mem_free() ` ` Return the free heap size in bytes.
363- * `` gc.mem_alloc() ` ` Return the number of bytes currently allocated.
360+ * `gc.collect() ` Force a garbage collection. See footnote.
361+ * `micropython.mem_info() ` Print a summary of RAM utilisation.
362+ * `gc.mem_free() ` Return the free heap size in bytes.
363+ * `gc.mem_alloc() ` Return the number of bytes currently allocated.
364364* ``micropython.mem_info(1) `` Print a table of heap utilisation (detailed below).
365365
366366The numbers produced are dependent on the platform, but it can be seen that
367367declaring the function uses a small amount of RAM in the form of bytecode
368368emitted by the compiler (the RAM used by the compiler has been reclaimed).
369369Running the function uses over 10KiB, but on return ``a `` is garbage because it
370- is out of scope and cannot be referenced. The final `` gc.collect() ` ` recovers
370+ is out of scope and cannot be referenced. The final `gc.collect() ` recovers
371371that memory.
372372
373373The final output produced by ``micropython.mem_info(1) `` will vary in detail but
@@ -394,7 +394,7 @@ line of the heap dump represents 0x400 bytes or 1KiB of RAM.
394394Control of Garbage Collection
395395~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
396396
397- A GC can be demanded at any time by issuing `` gc.collect() ` `. It is advantageous
397+ A GC can be demanded at any time by issuing `gc.collect() `. It is advantageous
398398to do this at intervals, firstly to pre-empt fragmentation and secondly for
399399performance. A GC can take several milliseconds but is quicker when there is
400400little work to do (about 1ms on the Pyboard). An explicit call can minimise that
@@ -417,7 +417,7 @@ occupied.
417417In general modules should instantiate data objects at runtime using constructors
418418or other initialisation functions. The reason is that if this occurs on
419419initialisation the compiler may be starved of RAM when subsequent modules are
420- imported. If modules do instantiate data on import then `` gc.collect() ` ` issued
420+ imported. If modules do instantiate data on import then `gc.collect() ` issued
421421after the import will ameliorate the problem.
422422
423423String Operations
@@ -444,13 +444,13 @@ RAM usage and speed.
444444
445445Where variables are required whose size is neither a byte nor a machine word
446446there are standard libraries which can assist in storing these efficiently and
447- in performing conversions. See the `` array `` , `` ustruct `` and `` uctypes ` `
447+ in performing conversions. See the `array `, `ustruct ` and `uctypes `
448448modules.
449449
450450Footnote: gc.collect() return value
451451~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
452452
453- On Unix and Windows platforms the `` gc.collect() ` ` method returns an integer
453+ On Unix and Windows platforms the `gc.collect() ` method returns an integer
454454which signifies the number of distinct memory regions that were reclaimed in the
455455collection (more precisely, the number of heads that were turned into frees). For
456456efficiency reasons bare metal ports do not return this value.
0 commit comments