LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Memory leaks when main loop is a while loop

Root / Programming Questions / [.]

PetrifiedLasagnaCreated:
I found this interesting little issue while working on a program that processes *potentially* large dynamic data sets. Most of us here are probably familiar with the limit on array sizes being 2^31 depending on available memory. Well I barely even scratched the surface of that with my program (600^2), but I was getting out of memory errors. My original loop looked liked the following.
WHILE RUNNING
 VSYNC
 'Call drawing function
 'Call input function
WEND
However after I changed the type of loop that was used, the error went away.
@MAIN

VSYNC
'Call drawing function
'Call input function

IF RUNNING GOTO @MAIN
This brings me to the conclusion that garbage collection is only properly handled when control belongs to the main thread. Does anyone else get this error? You can try experimenting with the following code. If you really get an out of memory error then FREEMEM should be close/equal to zero.
VAR I
DIM ARR[0]

WHILE 1
 INC I
 PRINT I
 ARR=RESIZEM(I*I)
WEND

DEF RESIZEM(NS)
 DIM TMP[NS]
 RETURN TMP
END

lol, never mind. It appears that I had managed to extend the amount of memory that I could allocate by setting the array to null (an empty array) before allocating the memory. I have found no correlation between the type of loop used and the amount of memory that I could allocate. :3 For those of you familiar with C pointers this is exactly what you need to keep in mind. If you are allocating brand new memory, free the stuff you already have. If only there was INT24 then I could squeeze FREEMEM/24 - FREEMEM/32 additional bytes out of it!