Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Exit Sub from inner loop?
#10
(02-03-2023, 02:13 AM)SMcNeill Wrote: After 1 minute:


[Image: image.png]


After a few minutes:


[Image: image.png]


And after another small wait:


[Image: image.png]

Now, what's our memory usage look like in those various screenshots?   See the issue now?

Exiting a GOSUB without a corresponding RETURN is a memory leak.  Eventually it's going to add up and cause you issues in one form or another.



Seems the website that hosts our screenshots and images is currently not working.  If you try the code below and watch your task manager, you can see the endless cimb of memory.

Code: (Select All)
DO
    foo
LOOP

SUB foo
    FOR i = 1 TO 100
        GOSUB fooGosub
    NEXT
    EXIT SUB

    fooGosub:
    PRINT i
    IF i = 10 THEN EXIT SUB
    RETURN
END SUB

We start at 60MB of ram usage, then go up to 100MB and then keep climbing to 200MB, and then keep going up, up, and up endlessly until the program crashes or you start using swap file memory and writing to your hard drive... until your drive is full and THEN the whole OS probably freezes and crashes....

Ahah, got it!
CPU usage stays the same, but memory used climbs, presumably to crash at some stage.
But I think I'm still ok to exit early if no other calls are involved, i.e. the sub has not called another sub or process that's still active?
Wrong! I see it still creeps up in my example.
I guess I'll have to be careful how I exit. Thanks.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Please visit my Website at: http://oldendayskids.blogspot.com/
Reply


Messages In This Thread
Exit Sub from inner loop? - by PhilOfPerth - 02-02-2023, 09:32 AM
RE: Exit Sub from inner loop? - by SMcNeill - 02-02-2023, 09:43 AM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-02-2023, 10:39 AM
RE: Exit Sub from inner loop? - by OldMoses - 02-02-2023, 04:10 PM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-02-2023, 11:09 PM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-02-2023, 11:48 PM
RE: Exit Sub from inner loop? - by SMcNeill - 02-03-2023, 12:04 AM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-03-2023, 12:56 AM
RE: Exit Sub from inner loop? - by SMcNeill - 02-03-2023, 02:13 AM
RE: Exit Sub from inner loop? - by PhilOfPerth - 02-03-2023, 02:40 AM



Users browsing this thread: 1 Guest(s)