def doit(id, ct, level): print(id, ct, level, micropython.stack_use()) if level > 10: return else: doit(id, ct, level + 1) time.sleep(0.5)
def log_mem_usage(_ticks_now): #pylint: disable=global-statement global _mem_used global _stack_used global _fs_used global _counter mem_used = gc.mem_alloc() mem_free = gc.mem_free() stack_used = micropython.stack_use() fs_data = uos.statvfs('/') fs_size = fs_data[1] * fs_data[2] # f_frsize * f_blocks fs_avail = fs_data[0] * fs_data[4] # f_bsize * f_bavail fs_used = fs_size - fs_avail if mem_used == _mem_used and stack_used == _stack_used and fs_used == _fs_used: _counter += 1 if _counter >= 60: _counter = 0 else: _counter = 0 if _counter == 0: _mem_used = mem_used _stack_used = stack_used _fs_used = fs_used _logger.debug("{},{},{},{},{}", mem_used, mem_free, stack_used, fs_used, fs_avail)
def mem(verbose=False): if verbose: print("{:18} {:9}".format("GC total ", gc.mem_alloc() + gc.mem_free())) micropython.mem_info() print("{:18} {:9}".format("mp stack use", micropython.stack_use())) print("{:18} {:9}".format("GC free", gc.mem_free())) print("{:18} {:9}".format("heap internal free", pycom.get_free_heap()[0])) print("{:18} {:9}".format("heap external free", pycom.get_free_heap()[1]))
def log_trace(x=None) -> None: log.debug( __name__, "Log trace %s, ... F: %s A: %s, S: %s", x, gc.mem_free(), gc.mem_alloc(), micropython.stack_use(), )
def mem_info(): # print(micropython.mem_info()) mem_heap_alloc = gc.mem_alloc() mem_heap_free = gc.mem_free() mem_heap_total = mem_heap_alloc + mem_heap_free mem_heap_alloc_ratio = mem_heap_alloc / mem_heap_total * 100 mem_stack_alloc = micropython.stack_use() # print('heap alloc = {:,}/{:,},{:.1f}% in use, stack in use={}'.format(mem_heap_alloc,mem_heap_total,mem_heap_alloc_ratio,mem_stack_alloc)) return (mem_heap_alloc_ratio, mem_stack_alloc)
def stack_status(): try: import micropython used_stack = micropython.stack_use() free_stack = 8192 - used_stack data = {'used': used_stack, 'free': free_stack, 'total': 8192} return data except Exception as e: print(e) finally: gc.collect()
def memorySnapshot(location=None): print("\n------memorySnapshot-----") if location: print("Location: {}\n".format(location)) # pylint: disable=E1101 print("Free memory: {} bytes".format(gc.mem_free())) # pylint: disable=E1101 print("Allocated memory: {} bytes".format(gc.mem_alloc())) # pylint: disable=E1101 print("Stack Use: {}".format(micropython.stack_use())) # pylint: disable=E1101 print("Memory Info:") # pylint: disable=E1101 print("-----------------------------") micropython.mem_info(1) print("-----------------------------") print("\n")
def demo_memory_usage(): ''' Demonstrates using the 'gc' module to print usage statistics and to invoke the garbage collector. Also demonstrates how the 'micropython' module can print some memory usage information. https://www.digi.com/resources/documentation/digidocs/90002219/#reference/r_module_gc.htm? ''' import gc def stats(): print(" gc.mem_free(): %s" % gc.mem_free()) print(" gc.mem_alloc(): %s" % gc.mem_alloc()) print(" total: %s" % (gc.mem_free() + gc.mem_alloc())) print("Before garbage collection:") stats() gc.collect() print("After garbage collection:") stats() print("Additional statistics:") import micropython micropython.mem_info() micropython.stack_use()
def get_system_info(): print("\r\n\r\n") print( "=======================================================================" ) print("SYSTEM INFO") print( "=======================================================================" ) print('datetime :{}'.format(get_string(utime.localtime()))) print('platform :{}'.format(sys.platform)) print('version :{}'.format(sys.version)) print('system :{}-{}'.format(sys.implementation[0], sys.implementation[1])) print('machine_id :0x{}'.format( ubinascii.hexlify(machine.unique_id()).decode().upper())) print('machine_fq :{}'.format(machine.freq())) print('maxsize :{}'.format(sys.maxsize)) print('modules :{}'.format(sys.modules)) micropython.mem_info() micropython.stack_use() print( "=======================================================================\r\n" )
def mem(): global mpy_heap_free, mpy_stack_used, internal_heap_free, external_heap_free # machine.info() #print("MPy heap free=", gc.mem_free(), "alloc=", gc.mem_alloc(), "total=", gc.mem_alloc() + gc.mem_free(), "diff=", mpy_heap_free - gc.mem_free()) # micropython.mem_info() print("MPy heap free= {:>8} diff={:>6}".format( gc.mem_free(), gc.mem_free() - mpy_heap_free)) print("MPy stack used= {:>8} diff={:>6}".format( micropython.stack_use(), micropython.stack_use() - mpy_stack_used)) print("internal heap free={:>8} diff={:>6}".format( pycom.get_free_heap()[0], pycom.get_free_heap()[0] - internal_heap_free)) print("external heap free={:>8} diff={:>6}".format( pycom.get_free_heap()[1], pycom.get_free_heap()[1] - external_heap_free)) mpy_heap_free = gc.mem_free() mpy_stack_used = micropython.stack_use() internal_heap_free = pycom.get_free_heap()[0] external_heap_free = pycom.get_free_heap()[1]
def saveError(e): s = uio.StringIO() sys.print_exception(e, s) data = { "Name": type(e).__name__, "Text": e.args[0], "Time": "%04d-%02d-%02d %02d:%02d:%02d" % time.localtime()[0:6], "Trace": s.getvalue(), "Platform": sys.platform, "Version": sys.version, "ResetCause": machine.reset_cause(), "UniqueID": ubinascii.hexlify(machine.unique_id()), "StackUse": micropython.stack_use(), "Temperature": (esp32.raw_temperature()-32)*5/9, "Freq": machine.freq() } if "version.json" in uos.listdir(): with open("version.json", "r") as f: version_info = ujson.load(f) data["FileVersionInfo"] = version_info with open("lastErr.json", "w") as f: ujson.dump(data, f) print(ujson.dumps(data))
# tests stack_use function in micropython module import micropython if not hasattr(micropython, 'stack_use'): print('SKIP') else: print(type(micropython.stack_use())) # output varies
######################################################## # memory analysis ######################################################## # ESP32 4MB, heap is 111168 bytes. note: stack is fixed 15360 / 1024 = 15Kio gc.collect() free = gc.mem_free() alloc = gc.mem_alloc() print("\nmem free %d, alloc %d, sum %d, percent free %0.2f" % (free, alloc, free + alloc, free / (free + alloc))) # garbage collection can be executed manually, if alloc fails, or if 25% of currently free heap becomes occupied #gc.threshold(gc.mem_free() // 4 + gc.mem_alloc()) print("stack used: ", stack_use()) print("mem info (1 for memory map): ", mem_info()) ################################################################ # every so often, based on RTC memory, fill screen with white, than go to sleep for a while ################################################################ if avoid_ghost: print('write entiere screen with white to avoid ghosts') buf = bytearray(800 * 480 // 8) fb = framebuf.FrameBuffer(buf, 800, 480, framebuf.MONO_HLSB) white = 1 # frame buffer is all white fb.fill(white) refresh_epaper(buf)
def print_stack_usage(tag): print(tag) print(stack_use())
import _thread import time import machine import gc import micropython import pycom keep_going = True mpy_heap_free = gc.mem_free() mpy_stack_used = micropython.stack_use() internal_heap_free = pycom.get_free_heap()[0] external_heap_free = pycom.get_free_heap()[1] def mem(): global mpy_heap_free, mpy_stack_used, internal_heap_free, external_heap_free # machine.info() #print("MPy heap free=", gc.mem_free(), "alloc=", gc.mem_alloc(), "total=", gc.mem_alloc() + gc.mem_free(), "diff=", mpy_heap_free - gc.mem_free()) # micropython.mem_info() print("MPy heap free= {:>8} diff={:>6}".format( gc.mem_free(), gc.mem_free() - mpy_heap_free)) print("MPy stack used= {:>8} diff={:>6}".format( micropython.stack_use(), micropython.stack_use() - mpy_stack_used)) print("internal heap free={:>8} diff={:>6}".format( pycom.get_free_heap()[0], pycom.get_free_heap()[0] - internal_heap_free))
def get_stack_current(self): import micropython stack_mem = micropython.stack_use() return stack_mem