Dealing with "Out of Memory" errors - Printable Version

+- Forum (
+-- Forum: Compilers and Computer Languages (
+--- Forum: ZX Basic Compiler (
+---- Forum: Help & Support (
+---- Thread: Dealing with "Out of Memory" errors (/showthread.php?tid=1034)

Dealing with "Out of Memory" errors - georgeo - 03-08-2021

Hi everyone,

I am relatively new to ZXBASIC, though think I have mostly got the hang of things and am using it to write a new game.

I am seeing intermittent Out of Memory issues (when compiling with --debug-memory), which I believe means I am running out of heap space. However, I've tried increasing the heap quite significantly (e.g., up to 8 kb) with little effect. I've also tried reducing the dependence on string arrays/ variables, in case this was the problem.

If I see an intermittent Out of Memory error mid-way through a program, long after most variables should have been defined, will it be heap-related or could it be something else?

Any suggestions for debugging my code, or properly sizing the heap, would be much appreciated.

Thanks in advance,

RE: Dealing with "Out of Memory" errors - RandomiserUsr - 03-08-2021

I am new too :-) but what is your compile cmd?
I use this at the moment as my program I writing is heavy on strings

zxbc myprog.bas --tzx --BASIC --autorun --heap=8192 --optimize 2 --org=25000 --debug-memory --debug-array --mmap=memory_%1.txt

Not sure if this helps?

RE: Dealing with "Out of Memory" errors - georgeo - 03-08-2021

Hi RandomiserUsr,

Up until now, I've been using something like this:

zxbc --org 26000 -t --strict --debug-memory --heap 6144 -B -a galacticon.bas

--though have tried varying the heap size from 4096 up to 8192 (at the highest level, I get out of memory almost immediately) and also the origin address.

With the above compile command, I get a code block of length 32,628, which I believe includes a 6k heap at the top of the block, so occupies memory 26,000 through to 58,628. I also have some UDGs that are loaded separately to 65,368.

I can't use the --debug-array option as it causes the compiler to crash (I use a pre-existing Python installation, so suspect something isn't quite as required).

The --mmap option looks potentially useful. At least I can see if the extent of the program is as expected.


RE: Dealing with "Out of Memory" errors - RandomiserUsr - 03-08-2021

Oddly enough I was getting Out Of Memory errors and putting in the debug-memory and debug-arrary I got a Subscript out of range error and
it was correct, I was accessing a higher dimension than there was.

I am using the Windows version and the latest version of zxbc

RE: Dealing with "Out of Memory" errors - georgeo - 03-09-2021

Thanks. I've so far been unable to use --debug-array, as it throws a Python error during compilation, which I suspected was because I'd downloaded the platform-independent version and used a separate Python installation. So, I have been trying to check array bounds by hand.

However, fuelled by your feedback, I tried installing a self-contained version of ZXBasic, pre-packaged with Python, on a different computer. Oddly enough I got the same error message, which makes me wonder if it is a problem in my code after all. So, I tried building a simple test program with --debug-array and it worked.

So, now I need to work out why my program can't be compiled with --debug-array. It throws the following error:

Traceback (most recent call last):
  File "/home/gb/zxbasic/zxbasic/", line 10, in <module>
    sys.exit(libzxbc.main())  # Exit
  File "/home/gb/zxbasic/zxbasic/src/libzxbc/", line 246, in main
  File "/home/gb/zxbasic/zxbasic/src/arch/zx48k/", line 1401, in start
  File "/home/gb/zxbasic/zxbasic/src/ast/", line 35, in visit
  File "/home/gb/zxbasic/zxbasic/src/arch/zx48k/", line 196, in visit_ARGLIST
    upper = node.parent.entry.bounds[i].upper
  File "/home/gb/zxbasic/zxbasic/src/symbols/", line 29, in __getitem__
    return self.children[key]
  File "/home/gb/zxbasic/zxbasic/src/ast/", line 40, in __getitem__
    return self._children[key]
IndexError: list index out of range

--though I don't know exactly where this happens in my code.

I'll keep digging. Thanks again,

RE: Dealing with "Out of Memory" errors - georgeo - 03-10-2021

Hi everyone,

I found the bug, which was -- as RandomiserUser guessed -- in my array bounds. Specifically I have  a teletype-style print routine, which looks like:
sub teletype(p$ as string)
    dim k as uinteger


    for k=0 to len(p$)-1
        beep 0.0002,-10
    next k


end sub

Sadly, I didn't spot that this can't cope with an empty string. I think, as k is a uinteger, what I get is a loop from 0 to 65,535 with an empty string, which is entertaining to watch print.

Thanks, Georgeo.

RE: Dealing with "Out of Memory" errors - RandomiserUsr - 03-12-2021

Nice to see things working for you :-)