I am fetching strings from a file (lines) using the `with open __ as file:` command.
I am using the garbage collector's `mem_free()` method to allocate 20% of the free memmory as a "buffer" for the data.
The code is meant to fetch lines untill the length of the data is >= to the "20% of free mem buffer" size.
I keep getting a MemmoryException although the GC seems to be working and also there is plenty of RAM left. Any ideas?
Thanks for any help! The GC is working...i can see where mem_free gets to around 644bytes and then it suddenly frees it back up to 64000 ish.
Code: Select all
def getAllFromIndex(self, model, first):
print(self._instance)
print("GET FROM " + first)
print(model + " file size = " + str(uos.stat("/database/" + model)[6]) + " bytes")
print("Free Heap Size = " + str(gc.mem_free()))
bufferRamSize = gc.mem_free() * 0.2
print("Fetching to 0.2x Free HEAP = " + str(bufferRamSize))
try:
data = ""
with open("/database/" + model, "r") as file:
print("Mem free = " + str(gc.mem_free()))
for line in file:
if len(data) >= bufferRamSize:
print("Break!")
break
print(str(len(data)) + " / "+str(gc.mem_free()))
data = data + line
data = "[" + data + "]"
print("GOT DATA!")
gc.collect()
print(data)
file.close()
return data
except Exception as e:
print("Whoopsies! " + str(e))
================== Log Serial Output: ================
Code: Select all
<GETDaoService object at 3ffec960>
GET FROM 10
user file size = 58774 bytes
Free Heap Size = 68560
Fetching to 0.2x Free HEAP = 13696.0
Mem free = 68256
0 / 68032
58 / 67824
116 / 67472
174 / 67072
232 / 66608
290 / 66080
348 / 65504
..........
........
.....
..
13340 / 43072
13398 / 29440
13456 / 42832
13514 / 42720
13572 / 28912
13630 / 42512
13688 / 42384
Break!
Whoopsies! memory allocation failed, allocating 13749 bytes