Python gc.mem_free() Examples

The following are 30 code examples of gc.mem_free(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gc , or try the search function .
Example #1
Source File: range_ex.py    From micropython-mqtt with MIT License 6 votes vote down vote up
def main(client):
    try:
        await client.connect()
    except OSError:
        print('Connection failed.')
        return
    n = 0
    s = '{} repubs: {} outages: {} rssi: {}dB free: {}bytes'
    while True:
        await asyncio.sleep(5)
        gc.collect()
        m = gc.mem_free()
        print('publish', n)
        # If WiFi is down the following will pause for the duration.
        await client.publish(TOPIC, s.format(n, client.REPUB_COUNT, outages, rssi, m), qos = 1)
        n += 1

# Define configuration 
Example #2
Source File: device.py    From terkin-datalogger with GNU Affero General Public License v3.0 6 votes vote down vote up
def run_gc(self):
        """
        Curate the garbage collector.
        https://docs.pycom.io/firmwareapi/micropython/gc.html

        For a "quick fix", issue the following periodically.
        https://community.hiveeyes.org/t/timing-things-on-micropython-for-esp32/2329/9

        """
        import gc
        log.info('Start curating the garbage collector')
        gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
        log.info('Collecting garbage')
        gc.collect()
        #log.info('Curating the garbage collector finished')
        log.info('Curating the garbage collector finished. Free memory: %s', gc.mem_free()) 
Example #3
Source File: main.py    From pysmartnode with MIT License 6 votes vote down vote up
def _receiveConfig():
    await asyncio.sleep(2)
    _log.debug("RAM before import receiveConfig:", gc.mem_free(), local_only=True)
    import pysmartnode.components.machine.remoteConfig
    gc.collect()
    _log.debug("RAM after import receiveConfig:", gc.mem_free(), local_only=True)
    conf = pysmartnode.components.machine.remoteConfig.RemoteConfig()
    gc.collect()
    _log.debug("RAM after creating receiveConfig:", gc.mem_free(), local_only=True)
    while not conf.done():
        await asyncio.sleep(1)
    gc.collect()
    _log.debug("RAM before deleting receiveConfig:", gc.mem_free(), local_only=True)
    await conf.removeComponent(conf)  # removes component from Component chain
    del conf
    del pysmartnode.components.machine.remoteConfig
    del sys.modules["pysmartnode.components.machine.remoteConfig"]
    gc.collect()
    _log.debug("RAM after deleting receiveConfig:", gc.mem_free(), local_only=True) 
Example #4
Source File: main.py    From micropython-ov2640 with MIT License 6 votes vote down vote up
def main():
    try:
        print("initializing camera")
        #cam = ov2640.ov2640(resolution=ov2640.OV2640_320x240_JPEG)
        cam = ov2640.ov2640(resolution=ov2640.OV2640_1024x768_JPEG)
        print(gc.mem_free())
    
        clen = cam.capture_to_file(FNAME, True)
        print("captured image is %d bytes" % clen)
        print("image is saved to %s" % FNAME)
    
        time.sleep(10)
        sys.exit(0)
    
    except KeyboardInterrupt:
        print("exiting...")
        sys.exit(0) 
Example #5
Source File: cmd.py    From esp8266 with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def mem(level=None):
    import gc
    mem_alloc = gc.mem_alloc()
    mem_free = gc.mem_free()
    capacity = mem_alloc + mem_free
    print("    capacity\tfree\tusage")
    print("    {}\t{}\t{}%".format(capacity, mem_free, int(
        ((capacity - mem_free) / capacity) * 100.0)))
    if level:
        import micropython
        micropython.mem_info(level) 
Example #6
Source File: Board.py    From illuminOS with MIT License 6 votes vote down vote up
def get_onboard_button_events(self, btn, bcc_key, on_single_click, on_double_click):
        import gc
        from machine import Timer

        if btn.value() == 0:
            self.button_click_counter[bcc_key] += 1
            if self.button_click_counter[bcc_key] == 1:
                log.info("single-click registered (mem free: " + str(gc.mem_free()) + ")")
                sc = getattr(tk, on_single_click)
                sc()

            elif self.button_click_counter[bcc_key] == 2:
                log.info("double click registered (mem free: " + str(gc.mem_free()) + ")")
                sc = getattr(tk, on_double_click)
                sc()
            else:
                pass

            gtim = Timer(1)
            gtim.init(period=300, mode=Timer.ONE_SHOT, callback=lambda t:self.reset_onboard_button_event_counter(bcc_key))

    # @timed_function 
Example #7
Source File: i2c_esp.py    From micropython-async with MIT License 5 votes vote down vote up
def sender():
    swriter = asyncio.StreamWriter(chan, {})
    txdata = [0, 0]
    while True:
        txdata[0] = gc.mem_free()
        await swriter.awrite(''.join((ujson.dumps(txdata), '\n')))
        txdata[1] += 1
        await asyncio.sleep_ms(1500)
        gc.collect() 
Example #8
Source File: mqtt_iot.py    From pysmartnode with MIT License 5 votes vote down vote up
def _receiveConfig(self):
        self.__receive_config = None
        while True:
            gc.collect()
            _log.debug("RAM before receiveConfig import: {!s}".format(gc.mem_free()), local_only=True)
            import pysmartnode.networking.mqtt_receive_config
            gc.collect()
            _log.debug("RAM after receiveConfig import: {!s}".format(gc.mem_free()), local_only=True)
            result = await pysmartnode.networking.mqtt_receive_config.requestConfig(config, self, _log)
            if result is False:
                _log.info("Using local components.json/py", local_only=True)
                gc.collect()
                _log.debug("RAM before receiveConfig deletion: {!s}".format(gc.mem_free()), local_only=True)
                del pysmartnode.networking.mqtt_receive_config
                del sys.modules["pysmartnode.networking.mqtt_receive_config"]
                gc.collect()
                _log.debug("RAM after receiveConfig deletion: {!s}".format(gc.mem_free()), local_only=True)
                local_works = await config._loadComponentsFile()
                if local_works is True:
                    return True
            else:
                gc.collect()
                _log.debug("RAM before receiveConfig deletion: {!s}".format(gc.mem_free()), local_only=True)
                del pysmartnode.networking.mqtt_receive_config
                del sys.modules["pysmartnode.networking.mqtt_receive_config"]
                gc.collect()
                _log.debug("RAM after receiveConfig deletion: {!s}".format(gc.mem_free()), local_only=True)
                result = ujson.loads(result)
                loop = asyncio.get_event_loop()
                if platform == "esp8266":
                    # on esp8266 components are split in small files and loaded after each other
                    # to keep RAM requirements low, only if filesystem is enabled
                    if sys_vars.hasFilesystem():
                        loop.create_task(config._loadComponentsFile())
                    else:
                        loop.create_task(config._registerComponentsAsync(result))
                else:
                    # on esp32 components are registered directly but async to let logs run between registrations
                    loop.create_task(config._registerComponentsAsync(result))
                return True
            await asyncio.sleep(60)  # if connection not stable or broker unreachable, try again in 60s 
Example #9
Source File: main.py    From pysmartnode with MIT License 5 votes vote down vote up
def main():
    print("free ram {!r}".format(gc.mem_free()))
    gc.collect()
    loop.create_task(_resetReason())
    config.getMQTT().registerWifiCallback(start_services)

    print("Starting uasyncio loop")
    try:
        loop.run_forever()
    except Exception as e:
        try:
            config.getMQTT().close()
        except:
            pass
        if config.DEBUG_STOP_AFTER_EXCEPTION:
            # want to see the exception trace in debug mode
            if config.USE_SOFTWARE_WATCHDOG:
                wdt.deinit()  # so it doesn't reset the board
            raise e
        # just log the exception and reset the microcontroller
        if sys.platform == "esp8266":
            try:
                rtc.memory("{!s}".format(e).encode())
            except Exception as e:
                print(e)
            print("{!s}".format(e).encode())
        else:
            with open("reset_reason.txt", "w") as f:
                f.write(e)
        machine.reset() 
Example #10
Source File: registerComponents.py    From pysmartnode with MIT License 5 votes vote down vote up
def __printRAM(start, info=""):
        print(info, "Mem free", gc.mem_free(), "diff:", gc.mem_free() - start) 
Example #11
Source File: config.py    From pysmartnode with MIT License 5 votes vote down vote up
def __printRAM(start, info=""):
        print(info, "Mem free", gc.mem_free(), "diff:", gc.mem_free() - start) 
Example #12
Source File: subscription.py    From pysmartnode with MIT License 5 votes vote down vote up
def printMemory(info=""):
    global memory
    memory_new = gc.mem_free()
    print("[RAM] [{!s}] {!s}".format(info, memory_new - memory))
    memory = memory_new 
Example #13
Source File: tree.py    From pysmartnode with MIT License 5 votes vote down vote up
def printMemory(info=""):
    global memory
    memory_new = gc.mem_free()
    print("[RAM] [{!s}] {!s}".format(info, memory_new - memory))
    memory = memory_new 
Example #14
Source File: subscribe_file.py    From pysmartnode with MIT License 5 votes vote down vote up
def printMemory(info=""):
    global memory
    memory_new = gc.mem_free()
    print("[RAM] [{!s}] {!s}".format(info, memory_new - memory))
    memory = memory_new 
Example #15
Source File: sx127x.py    From uPyLoRaWAN with Apache License 2.0 5 votes vote down vote up
def collect_garbage(self):
        gc.collect()
        if __DEBUG__:
            print('[Memory - free: {}   allocated: {}]'.format(gc.mem_free(), gc.mem_alloc())) 
Example #16
Source File: eddystone.py    From uble with MIT License 5 votes vote down vote up
def run(self, *args, **kwargs):
        """ run """
        def callback():
            """ callback """
            log.debug("memory free %d", gc.mem_free())

        super(Eddystone, self).run(callback=callback, callback_time=1000) 
Example #17
Source File: toolkit.py    From illuminOS with MIT License 5 votes vote down vote up
def timed_function(f, *args, **kwargs):
    import time
    myname = str(f).split(' ')[1]
    def new_func(*args, **kwargs):
        t = time.ticks_us()
        result = f(*args, **kwargs)
        delta = time.ticks_diff(t, time.ticks_us())
        log.debug('GC: {} Function {} Time = {:6.3f}ms'.format(str(gc.mem_free()), myname, delta/1000))
        return result

    return new_func

# @timed_function 
Example #18
Source File: Board.py    From illuminOS with MIT License 5 votes vote down vote up
def mem_cleanup(self):
        import gc
        log.debug("Invoking garbage collection ...")
        gc.collect()
        mem = gc.mem_free()
        if 6001 <= mem <= 10000:
            log.warn("Memory is low: " + str(mem))
        elif 4001 <= mem <= 6000:
            log.warn("Memory is very low: " + str(mem))
        elif mem < 4000:
            log.critical("Memory is extremely low: {}".format(str(mem)))
        else:
            log.debug("Memory is currently: " + str(mem)) 
Example #19
Source File: device.py    From microhomie with MIT License 5 votes vote down vote up
def publish_stats(self):
        from utime import time

        start_time = time()
        delay = self.stats_interval * MAIN_DELAY
        publish = self.publish
        while True:
            uptime = time() - start_time
            await publish("$stats/uptime", str(uptime))
            await publish("$stats/freeheap", str(mem_free()))
            await sleep_ms(delay) 
Example #20
Source File: mqtt_as.py    From microhomie with MIT License 5 votes vote down vote up
def _memory(self):
        count = 0
        while self.isconnected():  # Ensure just one instance.
            await asyncio.sleep(1)  # Quick response to outage.
            count += 1
            count %= 20
            if not count:
                gc.collect()
                print('RAM free {} alloc {}'.format(gc.mem_free(), gc.mem_alloc())) 
Example #21
Source File: system.py    From terkin-datalogger with GNU Affero General Public License v3.0 5 votes vote down vote up
def read(self):
        """ """
        import gc
        value = gc.mem_free()
        return {'system.memfree': value} 
Example #22
Source File: micropython.py    From terkin-datalogger with GNU Affero General Public License v3.0 5 votes vote down vote up
def monkeypatch_stdlib():

    import builtins
    builtins.const = int

    sys.modules['micropython'] = Mock()
    sys.modules['micropython'].const = int

    import struct
    sys.modules['ustruct'] = struct

    import binascii
    sys.modules['ubinascii'] = binascii

    import time
    def ticks_ms():
        import time
        return time.time() * 1000
    def ticks_diff(ticks1, ticks2):
        return abs(ticks1 - ticks2)
    time.ticks_ms = ticks_ms
    time.ticks_diff = ticks_diff
    sys.modules['utime'] = time

    import io
    sys.modules['uio'] = io

    import os
    sys.modules['uos'] = os

    import gc
    gc.threshold = Mock()
    gc.mem_free = Mock(return_value=1000000)
    gc.mem_alloc = Mock(return_value=2000000)

    # Optional convenience to improve speed.
    gc.collect = Mock() 
Example #23
Source File: kmk_keyboard.py    From kmk_firmware with GNU General Public License v3.0 5 votes vote down vote up
def _print_debug_cycle(self, init=False):
        pre_alloc = gc.mem_alloc()
        pre_free = gc.mem_free()

        if self.debug_enabled:
            if init:
                print('KMKInit(release={})'.format(KMK_RELEASE))

            print(self)
            print(self._state)
            print(
                'GCStats(pre_alloc={} pre_free={} alloc={} free={})'.format(
                    pre_alloc, pre_free, gc.mem_alloc(), gc.mem_free()
                )
            ) 
Example #24
Source File: sx127x.py    From SX127x_driver_for_MicroPython_on_ESP8266 with GNU General Public License v3.0 5 votes vote down vote up
def collect_garbage(self):
        gc.collect()
        if config_lora.IS_MICROPYTHON:
            print('[Memory - free: {}   allocated: {}]'.format(gc.mem_free(), gc.mem_alloc())) 
Example #25
Source File: mqtt_as.py    From micropython-mqtt with MIT License 5 votes vote down vote up
def _memory(self):
        count = 0
        while self.isconnected():  # Ensure just one instance.
            await asyncio.sleep(1)  # Quick response to outage.
            count += 1
            count %= 20
            if not count:
                gc.collect()
                print('RAM free {} alloc {}'.format(gc.mem_free(), gc.mem_alloc())) 
Example #26
Source File: mqtt.py    From micropython-mqtt with MIT License 5 votes vote down vote up
def from_pyboard(self):
        client = self.client
        while True:
            istr = await self.await_obj(20)  # wait for string (poll interval 20ms)
            s = istr.split(SEP)
            command = s[0]
            if command == PUBLISH:
                await client.publish(s[1], s[2], bool(s[3]), int(s[4]))
                # If qos == 1 only returns once PUBACK received.
                self.send(argformat(STATUS, PUBOK))
            elif command == SUBSCRIBE:
                await client.subscribe(s[1], int(s[2]))
                client.subscriptions[s[1]] = int(s[2])  # re-subscribe after outage
            elif command == MEM:
                gc.collect()
                gc.threshold(gc.mem_free() // 4 + gc.mem_alloc())
                self.send(argformat(MEM, gc.mem_free(), gc.mem_alloc()))
            elif command == TIME:
                t = await client.get_time()
                self.send(argformat(TIME, t))
            else:
                self.send(argformat(STATUS, UNKNOWN, 'Unknown command:', istr))

# Runs when channel has synchronised. No return: Pyboard resets ESP on fail.
# Get parameters from Pyboard. Process them. Connect. Instantiate client. Start
# from_pyboard() task. Wait forever, updating connected status. 
Example #27
Source File: esp8266.py    From tinyweb with MIT License 5 votes vote down vote up
def get(self, data):
        mem = {'mem_alloc': gc.mem_alloc(),
               'mem_free': gc.mem_free(),
               'mem_total': gc.mem_alloc() + gc.mem_free()}
        sta_if = network.WLAN(network.STA_IF)
        ifconfig = sta_if.ifconfig()
        net = {'ip': ifconfig[0],
               'netmask': ifconfig[1],
               'gateway': ifconfig[2],
               'dns': ifconfig[3]
               }
        return {'memory': mem, 'network': net}


# RESTAPI: GPIO status 
Example #28
Source File: esp_link.py    From micropython-iot with MIT License 5 votes vote down vote up
def report(self, time):
        data = [0, 0, 0]
        count = 0
        while True:
            await asyncio.sleep(time)
            data[0] = self.cl.connects  # For diagnostics
            data[1] = count
            count += 1
            gc.collect()
            data[2] = gc.mem_free()
            line = 'r{}\n'.format(ujson.dumps(data))
            self.swriter.write(line)
            await self.swriter.drain() 
Example #29
Source File: c_app.py    From micropython-iot with MIT License 5 votes vote down vote up
def writer(self):
        self.verbose and print('Started writer')
        data = [0, 0, 0]
        count = 0
        while True:
            data[0] = self.cl.connects
            data[1] = count
            count += 1
            gc.collect()
            data[2] = gc.mem_free()
            print('Sent', data, 'to server app\n')
            # .write() behaves as per .readline()
            await self.cl.write(ujson.dumps(data))
            await asyncio.sleep(5) 
Example #30
Source File: c_qos_fast.py    From micropython-iot with MIT License 5 votes vote down vote up
def writer(self):
        self.verbose and print('Started writer')
        while True:
            for _ in range(4):
                gc.collect()
                data = [self.tx_msg_id, self.cl.connects, gc.mem_free(),
                        self.cm.dupe, self.cm.miss]
                self.tx_msg_id += 1
                await self.cl  # Only launch write if link is up
                print('Sent', data, 'to server app\n')
                dstr = ujson.dumps(data)
                asyncio.create_task(self.cl.write(dstr, wait=False))
            await asyncio.sleep(5)