• Print

Author Topic: "Out Of Memory"  (Read 708 times)

pitt

  • Full Member
  • ***
  • Posts: 243
    • Email
"Out Of Memory"
« on: April 20, 2012, 06:03:24 AM »
Hello,

I have been developing a 2D RPG game for over six months now. I have incorporated the game editor into the engine for a quick way to switch between game play and editing and outsourced most if not all of the game script to external files. I have now run into an error "Out of Memory". I am wondering why this appears as when compiled the game takes up 148MB of RAM. This is out of 8GB under Windows 7 Ultimate 64-bit. One would assume you could utilize all of the RAM + SWAP your system can handle and 148MB for an engine game engine + editor is not that much compared to commercial games that use 500MB-2GB RAM. Separating the two engines and keeping both updated when a global change is made would take many, many, many extra hours and be a complete headache. Even if I were to separate the two and develop a system from switching from one EXE to the other I still need space to develop the game engine which I predict when done will take up 5 times the space the current editor does. Is there some sort of way around this error?

Assistance with this issue will be appreciated,
Pitt

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 4664
  • QB Forever
    • Email
Re: "Out Of Memory"
« Reply #1 on: April 20, 2012, 06:12:47 AM »
Hi,

Does it give an error number when it displays "Out of Memory"?
Something old... Something new... Something borrowed... Something blue...

pitt

  • Full Member
  • ***
  • Posts: 243
    • Email
Re: "Out Of Memory"
« Reply #2 on: April 20, 2012, 06:26:23 AM »
No it does not. See attached image.

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 4664
  • QB Forever
    • Email
Re: "Out Of Memory"
« Reply #3 on: April 20, 2012, 06:32:35 AM »
When the error is being displayed, open up 'task manager' and see what comes after the ... in the Critical Erro... title, it should be listed as one of the program names.
Something old... Something new... Something borrowed... Something blue...

LeChuck

  • Hero Member
  • *****
  • Posts: 895
  • 18 * 37
Re: "Out Of Memory"
« Reply #4 on: April 20, 2012, 06:52:17 AM »
Not sure if I can help but from my experience this comes from setting an array(s) to large.

This line of code when compiled takes up 5MB of memory on my system but will display "Out of memory", because of trying to create an array to large for the 2GB limit on 32-bit apps QB64 currently compiles.

Code: [Select]
DIM array(4000000000) AS LONG
Two or more, use a FOR!

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 4664
  • QB Forever
    • Email
Re: "Out Of Memory"
« Reply #5 on: April 20, 2012, 06:58:21 AM »
I'll follow this up tomorrow.
Something old... Something new... Something borrowed... Something blue...

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: "Out Of Memory"
« Reply #6 on: April 20, 2012, 07:04:44 AM »
Are you freeing image memory handles? Free them when they are no longer being used. Don't use the same handle over and over as the memory will stay in use.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

pitt

  • Full Member
  • ***
  • Posts: 243
    • Email
Re: "Out Of Memory"
« Reply #7 on: April 20, 2012, 10:46:37 AM »
Quote from: Galleon on April 20, 2012, 06:32:35 AM
When the error is being displayed, open up 'task manager' and see what comes after the ... in the Critical Erro... title, it should be listed as one of the program names.

Critical Error #1

Quote from: Clippy
Are you freeing image memory handles? Free them when they are no longer being used. Don't use the same handle over and over as the memory will stay in use.

I only use 1 image handle named ImageHandle& and I free it before each time I load a new image.

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: "Out Of Memory"
« Reply #8 on: April 20, 2012, 11:12:49 AM »
So what would you suppose the program is doing to use all of that memory?

You would know best.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

TerryRitchie

  • Hero Member
  • *****
  • Posts: 2264
  • FORMAT C:\ /Q /U /AUTOTEST (How to repair Win8)
    • Email
Re: "Out Of Memory"
« Reply #9 on: April 20, 2012, 11:35:44 AM »
Quote from: pitt on April 20, 2012, 06:03:24 AM
Hello,

I have been developing a 2D RPG game for over six months now. I have incorporated the game editor into the engine for a quick way to switch between game play and editing and outsourced most if not all of the game script to external files. I have now run into an error "Out of Memory". I am wondering why this appears as when compiled the game takes up 148MB of RAM. This is out of 8GB under Windows 7 Ultimate 64-bit. One would assume you could utilize all of the RAM + SWAP your system can handle and 148MB for an engine game engine + editor is not that much compared to commercial games that use 500MB-2GB RAM. Separating the two engines and keeping both updated when a global change is made would take many, many, many extra hours and be a complete headache. Even if I were to separate the two and develop a system from switching from one EXE to the other I still need space to develop the game engine which I predict when done will take up 5 times the space the current editor does. Is there some sort of way around this error?

Assistance with this issue will be appreciated,
Pitt

Hello and Welcome! :)

Are you saying that the compiled program size is 148MB (the .EXE)or that the program, through the use of variables, arrays and such, uses a total of 148MB by your calculations?

pitt

  • Full Member
  • ***
  • Posts: 243
    • Email
Re: "Out Of Memory"
« Reply #10 on: April 20, 2012, 05:41:18 PM »
The size of RAM the program takes up is 148MB.

I think I tracked down the error to the constant writing to

workscreen(1) = _NEWIMAGE(2048, 1536, 32)

... and many, many, many more variables like it.

Here is the problem code

Code: [Select]
' Draw Action Block Lines
draw_ab:

IF did_ab = 1 THEN RETURN

'IF g + 40 < s1 - 640 THEN LOCATE 8, 4: RETURN
'IF h + 40 < s2 - 480 THEN LOCATE 9, 4: RETURN

IF show_ab = 1 THEN

    IF x$ = "BL" THEN z1 = 255: z2 = 255: z3 = 255
    IF x$ = "SC" THEN z1 = 255: z2 = 55: z3 = 0
    IF x$ = "MV" THEN z1 = 255: z2 = 222: z3 = 0
    IF x$ = "WP" THEN z1 = 255: z2 = 0: z3 = 240

    _DEST workscreen(1)

    IF d$ = "D" THEN
        LINE (e - 20, f + 20)-(e - 10, f + 20), _RGB(0, 0, 0), B
        LINE (e - 20, f + 20)-(e - 10, f + 20), _RGB(z1, z2, z3), B
        IF show_arrows = 1 THEN
            IF x$ = "BL" THEN ch4r%(90) = SPRITENEW(char%(90), 23, DONTSAVE): SPRITEPUT e - 15, f + 30, ch4r%(90)
            IF x$ = "SC" THEN ch4r%(90) = SPRITENEW(char%(90), 23, DONTSAVE): SPRITEPUT e - 15, f + 30, ch4r%(90)
            IF x$ = "MV" THEN ch4r%(90) = SPRITENEW(char%(90), 23, DONTSAVE): SPRITEPUT e - 15, f + 30, ch4r%(90)
            IF x$ = "WP" THEN ch4r%(90) = SPRITENEW(char%(90), 23, DONTSAVE): SPRITEPUT e - 15, f + 30, ch4r%(90)

            IF x$ = "BL" THEN ch4r%(90) = SPRITENEW(char%(90), 7, DONTSAVE): SPRITEPUT e - 15, f + 30, ch4r%(90)
            IF x$ = "SC" THEN ch4r%(90) = SPRITENEW(char%(90), 11, DONTSAVE): SPRITEPUT e - 15, f + 30, ch4r%(90)
            IF x$ = "MV" THEN ch4r%(90) = SPRITENEW(char%(90), 15, DONTSAVE): SPRITEPUT e - 15, f + 30, ch4r%(90)
            IF x$ = "WP" THEN ch4r%(90) = SPRITENEW(char%(90), 19, DONTSAVE): SPRITEPUT e - 15, f + 30, ch4r%(90)
        END IF
    END IF

    IF d$ = "U" THEN
        LINE (e - 20, f - 20)-(e - 10, f - 20), _RGB(0, 0, 0), B
        LINE (e - 20, f - 20)-(e - 10, f - 20), _RGB(z1, z2, z3), B
        IF show_arrows = 1 THEN
            IF x$ = "BL" THEN ch4r%(90) = SPRITENEW(char%(90), 22, DONTSAVE): SPRITEPUT e - 15, f - 30, ch4r%(90)
            IF x$ = "SC" THEN ch4r%(90) = SPRITENEW(char%(90), 22, DONTSAVE): SPRITEPUT e - 15, f - 30, ch4r%(90)
            IF x$ = "MV" THEN ch4r%(90) = SPRITENEW(char%(90), 22, DONTSAVE): SPRITEPUT e - 15, f - 30, ch4r%(90)
            IF x$ = "WP" THEN ch4r%(90) = SPRITENEW(char%(90), 22, DONTSAVE): SPRITEPUT e - 15, f - 30, ch4r%(90)

            IF x$ = "BL" THEN ch4r%(90) = SPRITENEW(char%(90), 6, DONTSAVE): SPRITEPUT e - 15, f - 30, ch4r%(90)
            IF x$ = "SC" THEN ch4r%(90) = SPRITENEW(char%(90), 10, DONTSAVE): SPRITEPUT e - 15, f - 30, ch4r%(90)
            IF x$ = "MV" THEN ch4r%(90) = SPRITENEW(char%(90), 14, DONTSAVE): SPRITEPUT e - 15, f - 30, ch4r%(90)
            IF x$ = "WP" THEN ch4r%(90) = SPRITENEW(char%(90), 18, DONTSAVE): SPRITEPUT e - 15, f - 30, ch4r%(90)
        END IF
    END IF

    IF d$ = "L" THEN
        LINE (e - 20, f - 20)-(e - 20, f - 10), _RGB(0, 0, 0), B
        LINE (e - 20, f - 20)-(e - 20, f - 10), _RGB(z1, z2, z3), B
        IF show_arrows = 1 THEN
            IF x$ = "BL" THEN ch4r%(90) = SPRITENEW(char%(90), 25, DONTSAVE): SPRITEPUT e - 30, f - 15, ch4r%(90)
            IF x$ = "SC" THEN ch4r%(90) = SPRITENEW(char%(90), 25, DONTSAVE): SPRITEPUT e - 30, f - 15, ch4r%(90)
            IF x$ = "MV" THEN ch4r%(90) = SPRITENEW(char%(90), 25, DONTSAVE): SPRITEPUT e - 30, f - 15, ch4r%(90)
            IF x$ = "WP" THEN ch4r%(90) = SPRITENEW(char%(90), 25, DONTSAVE): SPRITEPUT e - 30, f - 15, ch4r%(90)

            IF x$ = "BL" THEN ch4r%(90) = SPRITENEW(char%(90), 9, DONTSAVE): SPRITEPUT e - 30, f - 15, ch4r%(90)
            IF x$ = "SC" THEN ch4r%(90) = SPRITENEW(char%(90), 13, DONTSAVE): SPRITEPUT e - 30, f - 15, ch4r%(90)
            IF x$ = "MV" THEN ch4r%(90) = SPRITENEW(char%(90), 17, DONTSAVE): SPRITEPUT e - 30, f - 15, ch4r%(90)
            IF x$ = "WP" THEN ch4r%(90) = SPRITENEW(char%(90), 21, DONTSAVE): SPRITEPUT e - 30, f - 15, ch4r%(90)
        END IF
    END IF

    IF d$ = "R" THEN
        LINE (e + 20, f - 20)-(e + 20, f - 10), _RGB(0, 0, 0), B
        LINE (e + 20, f - 20)-(e + 20, f - 10), _RGB(z1, z2, z3), B
        IF show_arrows = 1 THEN
            IF x$ = "BL" THEN ch4r%(90) = SPRITENEW(char%(90), 24, DONTSAVE): SPRITEPUT e + 30, f - 15, ch4r%(90)
            IF x$ = "SC" THEN ch4r%(90) = SPRITENEW(char%(90), 24, DONTSAVE): SPRITEPUT e + 30, f - 15, ch4r%(90)
            IF x$ = "MV" THEN ch4r%(90) = SPRITENEW(char%(90), 24, DONTSAVE): SPRITEPUT e + 30, f - 15, ch4r%(90)
            IF x$ = "WP" THEN ch4r%(90) = SPRITENEW(char%(90), 24, DONTSAVE): SPRITEPUT e + 30, f - 15, ch4r%(90)

            IF x$ = "BL" THEN ch4r%(90) = SPRITENEW(char%(90), 8, DONTSAVE): SPRITEPUT e + 30, f - 15, ch4r%(90)
            IF x$ = "SC" THEN ch4r%(90) = SPRITENEW(char%(90), 12, DONTSAVE): SPRITEPUT e + 30, f - 15, ch4r%(90)
            IF x$ = "MV" THEN ch4r%(90) = SPRITENEW(char%(90), 16, DONTSAVE): SPRITEPUT e + 30, f - 15, ch4r%(90)
            IF x$ = "WP" THEN ch4r%(90) = SPRITENEW(char%(90), 20, DONTSAVE): SPRITEPUT e + 30, f - 15, ch4r%(90)
        END IF
    END IF
END IF
_DEST backscreen
RETURN

This code is called to load small arrows on the screen which define boundary lines by color, etc ... and can be called 50 - 500,000 times per map. Every time it's called another 300-400KB adds to the total RAM.

Are you saying that the image handle "workscreen(1)" needs to be _FREEIMAGE after every use or after 50, 100, 1000000 uses? That is absurd! Why should the image handle keep old data that is no longer present in RAM? How would I track down all of the image handles and somehow _FREEIMAGE them without losing what is currently per-say "onscreen" in each image? Do normal variables like this%, freaking& sucks(1) also need to be cleared all of the time?

I also just noticed if I load another map (basically, the engine starts by loading the map and it's data then goes from there) the RAM increases around 8MB per map load. So I think it's apparent all sorts of image handles [and variables?] are just stacking up data in them for no good damn reason. If you write over something you would assume the old data is free'ed from RAM!!!

My program is 7,561 lines of code and the functions, subs, etc ... that all share variables that are used to draw the map, display the characters, objects, menus, etc ... are changed by many of the functions so I just cannot "CLEAR/_FREEIMAGE" them in any single location or locations. Say I start with a whiteboard, I draw something on it, 8-20 maps later I draw something else, or I draw character movement and save the location, add this, add that .... I just can't clear the handles on a whim! I never remember QBasic not letting go of old data when new was wrote to a image handle or variable....

There, I said it .... six months of coding, drawing graphics, spites, writing scripts and I have to come up with a solution to a problem that should not exist!

Where do I start? HELP!!!

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 4664
  • QB Forever
    • Email
Re: "Out Of Memory"
« Reply #11 on: April 20, 2012, 06:14:06 PM »
_FREEIMAGE does not clear all images. You pass the specific image handle that you want to free to _FREEIMAGE

'load image
i = _LOADIMAGE(10,10,32)
'use image
_PUTIMAGE(20,20),i
'and when you are done, free the image
_FREEIMAGE i

I don't understand what the problem is...

The code you provided doesn't really help to illustrate the problem because the subs/functions you are calling are not QB64 commands. Are you using someone's library?

Quote
If you write over something you would assume the old data is free'ed from RAM!!!
How are you writing over the old data?
« Last Edit: April 20, 2012, 06:22:21 PM by Galleon »
Something old... Something new... Something borrowed... Something blue...

pitt

  • Full Member
  • ***
  • Posts: 243
    • Email
Re: "Out Of Memory"
« Reply #12 on: April 20, 2012, 07:38:31 PM »
Thank You, but I know the syntax to _FREEIMAGE.

I am using Ritchie's QB64 Sprite Library for handling sprite sheets. It has a SPRITEFREE function that works exactly as _FREEIMAGE does, but when I call ...

_FREEIMAGE workscreen(1)

It does indeed clear up 5MB of RAM, but when I rebuild the workscreen(1) it doesn't help at all.

You probably don't support 3rd party LIBs, but I've since added the SPRITEFREE to clear the ch4r%(90), but this seems to have little effect.

I believe the main problem is that I did not understand how variables and RAM work (which I still don't) and have coded for 6 months for this big headache. I believe a "CLEAR" statement then loading only the necessary save data and then having the engine pick up and start off from there during map transitions is the only viable option I have. However, during MAP EDITING or possibly during the game itself (depending on how much you walk - I'm leaking RAM there too) the RAM will run out and I have the slightest clue on how to address the issue.

Why is an app that is only taking up 148MB of RAM is crashing due to RAM issues when most modern systems have 2-4GB, mine having 8GB and of course there's the practically unlimited Windows SWAP file.

A heads up on a detailed guide/post of how QB64 handles it's variables/images and RAM would be appreciated.

Thanks for all of the replies so far.

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: "Out Of Memory"
« Reply #13 on: April 20, 2012, 09:27:04 PM »
_LOADIMAGE and _NEWIMAGE handles are references to the actual image stored in memory. The more images stored, the more memory. Now if you alter an image, that is just changing the information already in memory. If you make a new image it takes memory somewhere else.

If you are done with an image, freeing it will return that memory for other uses. _COPYIMAGE can also add memory usage as the image is copied to a new handle.

If you want further help you can send the code to Galleon.
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

pitt

  • Full Member
  • ***
  • Posts: 243
    • Email
Re: "Out Of Memory"
« Reply #14 on: April 21, 2012, 02:44:29 PM »
Quote from: Clippy on April 20, 2012, 09:27:04 PM
_LOADIMAGE and _NEWIMAGE handles are references to the actual image stored in memory. The more images stored, the more memory. Now if you alter an image, that is just changing the information already in memory. If you make a new image it takes memory somewhere else.

If you are done with an image, freeing it will return that memory for other uses. _COPYIMAGE can also add memory usage as the image is copied to a new handle.

If you want further help you can send the code to Galleon.

Thanks again, but this is information I knew. You see, I have multiple layers per map ex- workscreen(1), workscreen(2), workscreen(3) and workscreen(4). Each time a map is loaded I _LOADIMAGE a default image to each layer (sometimes all layers aren't "active"). No matter, when I make a change to a layer some RAM is used/added. I'm not just "changing the information already in memory" I am ADDING TO IT. The solution is to _FREEIMAGE; however, I have built these images up with adding/subtracking over time and I cannot just "FREE" them during the middle of a map. Now, if I make a new image handle and _COPYIMAGE it copies the image and the current total RAM the old image handle holds.

Per say (full code not included, you get the point);

1)

_NEWIMAGE workscreen(1) - 5MB of RAM ADDED
_COPYIMAGE workscreen(1) - 5MB of RAM TRANSFERED
_FREEIMAGE workscreeen(1) - 5MB of RAM FREED
_NEWIMAGE worksceen(1)
_COPYIMAGE back to workscreen(1) and _FREEIMAGE to hanlde used to store the _COPYIMAGE - TOTAL 5MB RAM USED

2)

_NEWIMAGE workscreen(1) - 5MB of RAM ADDED
 [ add a bunch of stuff] - 8MB RAM TOTAL
_COPYIMAGE workscreen(1) - 8MB of RAM TRANSFERED
_FREEIMAGE workscreen(1) - 8MB of RAM FREED
_NEWIMAGE worksceen(1)
_COPYIMAGE back to workscreen(1) and _FREEIMAGE to hanlde used to store the _COPYIMAGE - TOTAL 8MB RAM USED

You see, using the _FREEIMAGE is ineffective unless I start the entire drawing process over again, but still, no matter what I do each time I write to an image handle it uses MORE RAM. It just does not change what is currently in RAM. So no matter what the more I draw onto an image the more RAM is added. An example is each time I move my character 4KB of RAM is added or if I place an external image on part of a map then let's say 500KB is added. It's not staying the same.

Also, sending my code to anyone is out of the question. It's not open source and I won't have leaks no matter what. No way. Not an option. Ever.

  • Print