2525# 1048576 | 1
2626# 2097152 | 0
2727#=====================================
28+ #
29+ # On desktop, this hangs while outputting, for some reason...
2830
2931import gc
3032import time
3133import _thread
3234
3335# Configuration
3436ALLOCATION_TIMEOUT_MS = 100 # Timeout for a single allocation (in milliseconds)
35-
37+ keep_running = True
3638
3739def test_allocation (buffer_size , n ):
3840 """Test how many buffers of a given size can be allocated with a timeout."""
@@ -41,7 +43,7 @@ def test_allocation(buffer_size, n):
4143 count = 0
4244
4345 try :
44- while appscreen == lv . screen_active () :
46+ while keep_running :
4547 # Measure time for allocation
4648 start_time = time .ticks_ms ()
4749 # Allocate a new buffer
@@ -66,21 +68,18 @@ def test_allocation(buffer_size, n):
6668 print (f"\n Stopped after allocating { count } buffers of { buffer_size } bytes: { e } " )
6769
6870 # Free allocated buffers
69- # buffers.clear()
71+ buffers .clear ()
7072 gc .collect ()
7173 return count
7274
73-
74-
75-
7675def stress_test_thread ():
7776 summary = "Running RAM memory tests...\n \n "
7877 summary += "Buffer Size (bytes) | Max Allocated\n "
7978 summary += "-----------------------------------\n "
8079 lv .async_call (lambda l : status .set_text (summary ), None )
8180 # Test buffer sizes of 2^n, starting from n=1 (2 bytes)
8281 n = 1
83- while appscreen == lv . screen_active () :
82+ while keep_running :
8483 buffer_size = 2 ** n
8584 summary += f"{ buffer_size :>12} | "
8685 #lv.async_call(lambda l: status.set_text(summary), None)
@@ -105,6 +104,15 @@ def stress_test_thread():
105104 summary += "Test completed.\n "
106105 lv .async_call (lambda l : status .set_text (summary ), None )
107106
107+
108+
109+ def janitor_cb (timer ):
110+ global keep_running
111+ if lv .screen_active () != appscreen :
112+ print ("memtest.py backgrounded, cleaning up..." )
113+ janitor .delete ()
114+ keep_running = False
115+
108116appscreen = lv .screen_active ()
109117status = lv .label (appscreen )
110118status .align (lv .ALIGN .TOP_LEFT , 5 , 10 )
@@ -113,3 +121,5 @@ def stress_test_thread():
113121
114122_thread .stack_size (12 * 1024 )
115123_thread .start_new_thread (stress_test_thread , ())
124+
125+ janitor = lv .timer_create (janitor_cb , 400 , None )
0 commit comments