# Cut down on memory usage

## Recommended Posts

Im making a game, and works perfect for me, but when i let someone try it, they said it was too laggy and such.
here is [u]their[/u] info:

[font=arial, sans-serif][size=2]
[b]Operating System[/b]: Windows Vista Home Premium (6.0, Build 6002)
[b]System Model[/b]: Toshiba Satellite Pro L300
[b]Processor[/b]: Intel Celeron CPU - 550 @ 2.00GHz
[b]Memory[/b]: 1014MB
[b]DirectX[/b]: 11

[b]Display[/b]: Mobile Intel 965 Express Chipset Family (250mb)

it is a quite simple game.

im using C++ and SDL, i have no knowledge of using the [u]new[/u] type definition.

what could the problems be?[/size][/font]

##### Share on other sites
There are many possible causes..
How much memory is the game using?
How is the cpu usage when the game runs?

##### Share on other sites
Unless you are doing something horrible horribly wrong then memory isn't going to be the problem.

Unless you are using OpenGL with SDL then chances are all your graphics operations are being done in software on the CPU which would be why the game is running slow.

##### Share on other sites
on my computer it runs fine, its just that one person who has problems. i am using SDL, but not with openGL, and i could be making some mistakes. what kind of mistakes are there to be made? would it be better to use a new int* than just a int in a function?

##### Share on other sites
You are asking questions that cannot be answered at all. You have to identify exactly what the problem is. So far you say it is slow, that doesnt mean anything. It could be so many different potential problems, it wont help to guess. You must identify what is slow. Is the hardware underpowered. Is your code written poorly. Their computer could have a virus scanner installed that really sucks. Who knows. . . .

##### Share on other sites
If your game is laggy on someone else's computer, maybe memory usage is not the problem. You are loosing speed somewhere. You should identify where this happens (usually in the update or render methods). Maybe you're doing things like loading textures every frame. You can use a profiler to see where you are loosing speed.

##### Share on other sites
[quote]
what kind of mistakes are there to be made?
[/quote]
Lots. More than could be enumerated without more information. Answering Wooh's questions would be illuminating. Other questions to ask is how much I/O and page swaps the program is doing.

I recommend building a version of your game with a basic, built in profiler and logging. So, you might record number of time units spent updating, rendering, etc, etc. Append the values every few seconds to a file. By looking at the minimum and maximum values, the average value and the standard deviation, plus the duration of "spikes", you can get an accurate idea of what your program is spending its time doing. Consider also logging any event that should be rare or unusual, for example file access. You might be able to correlate the latency spikes with such events.

The simplest cause is just doing more work than the CPU/GPU/Disk can handle. For example, running your game at some ultra hi-res 1,000,000 x 1,000,000 pixels on your powerful development machine can cause problems for users with more modest computers.

Another common cause are memory leaks, which is allocating memory but not freeing it correctly once it you are finished using it.

Simple games are sometimes written so that each game object loads a file or set of files when it is created. Caching these images at load time, sharing them at runtime and freeing them during shutdown saves I/O time, CPU time and memory. For small games, you should be able to avoid touching the disk during runtime (except maybe at level transitions, etc).

[quote]
would it be better to use a new int* than just a int in a function?
[/quote]
No. In general, this would be worse.

##### Share on other sites
One problem I have seen other people do is when calling SDL_SetVideoMode. If you use a bits per pixel (bpp) argument that is different from the native display bpp it will have to make slow conversions each time you call SDL_Flip (or similar). Best is to pass 0 because that will pick the best for you. [code]SDL_SetVideoMode(width, height, 0, flags);[/code] Another common mistake is to not use SDL_DisplayFormat. Without seeing your code we can only guess.

##### Share on other sites
here is all the source, i trust no one will try to steal my work. [img]http://public.gamedev.net/public/style_emoticons/default/unsure.gif[/img]

[url="http://www.mediafire.com/?9e549l4wjbbnp0g"]http://www.mediafire.com/?9e549l4wjbbnp0g[/url]
the password is survivor.

please look and reply back with anything that might be causing this guy to have issues. [img]http://public.gamedev.net/public/style_emoticons/default/rolleyes.gif[/img]

##### Share on other sites
You assume bpp is 32 and you never call SDL_DisplayFormat, as I thought..

EDIT: To clarify: When you blit one surface to another surface they must have the same format. If they have different formats SDL_BlitSurface will automatically convert the source surface into a copy with the same format as the destination surface before blitting. This is slow and it happens every time. For this reason SDL_DisplayFormat is often used when loading surfaces to convert the surface into the same format as the screen surface. When you call SDL_Flip(screen), if screen has a different format than the real screen settings it will also have to convert here each time you call SDL_Flip. I guess your display is set to 32 bpp but what about your friend?

EDIT2: I also noticed your timing code is not working.[code]StartNum = SDL_GetTicks();
if ((SDL_GetTicks()-StartNum)<(1000/APP.FPS)){SDL_Delay((1000/APP.FPS)-(SDL_GetTicks()-StartNum));}[/code]If we assume all calls to SDL_GetTicks() returns the same number we can simplify the code to this [code]if (0 < 1000/APP.FPS)
{
SDL_Delay(1000/APP.FPS);
}[/code]You are sleeping the same amount each frame so the game will run slower on a slow computer and faster on a faster computer.

##### Share on other sites
Thanks for the second edit, i knew i messed up but never got around to fixing it... what should i do to fix it?

so what would it look like to do this?

would i need to change (when done with applying surfaces) create a new surface to flip with the correct BitsPerPixel?
if not, would i need to upload each image then convert each image?

##### Share on other sites
[quote name='bennettbugs' timestamp='1313896498' post='4851790']Thanks for the second edit, i knew i messed up but never got around to fixing it... what should i do to fix it?[/quote]
maybe something like this:[code]// somewhere outside the loop you need to initialize nextFrameTime
Uint32 nextFrameTime = SDL_GetTicks();

...

// in the loop
Uint32 currentTime = SDL_GetTicks();
if (nextFrameTime > currentTime)
{
SDL_Delay(nextFrameTime - currentTime);
nextFrameTime += FRAME_TIME; // FRAME_TIME is defined as 1000/APP.FPS
}
else
{
// This else part is not strictly necessary but it
// makes so that if the game got behind schedule it
// will not speed up to catch up for lost time.
// Maybe you want to handle this differently
nextFrameTime = currentTime + FRAME_TIME;
}[/code]

[quote name='bennettbugs' timestamp='1313896498' post='4851790']would i need to change (when done with applying surfaces) create a new surface to flip with the correct BitsPerPixel?
if not, would i need to upload each image then convert each image? [/quote]
You set the format of the screen surface when you call SDL_SetVideoMode. This should never have to be changed. As I said earlier, if you pass 0 as the bpp to SDL_SetVideoMode it will automatically pick the best format. You also want to have the same format on all the other surfaces. By using SDL_DisplayFormat you can convert the surfaces into the screen format. Do this right after you have loaded a surface and you will not have to do it again. Do this for all surfaces (except the screen surface)! If the surface have an alpha channel use SDL_DisplayFormatAlpha instead of SDL_DisplayFormat.

See the SDL documentation for more information:
http://sdl.beuc.net/sdl.wiki/SDL_DisplayFormat
http://sdl.beuc.net/sdl.wiki/SDL_SetVideoMode

##### Share on other sites
Their computer stats are quite dated. 1gb or ram, on board video(shared ram with system) and a Celeron 550. And then Vista installed on it to boot. Sounds like a netbook, and a very old one at that. 1gb of ram? I wouldn't expect they would be able to run anything, after vista is loaded, effectively even assuming a fresh install and a fully optimized OS.

##### Share on other sites
Thanks alot.

I had no clue what the meaning of their computer stats were, so i just decided there was something wrong with my code. But thanks again, this was very helpful!

##### Share on other sites
[quote name='0Circle0' timestamp='1313939816' post='4851916']
Their computer stats are quite dated. 1gb or ram, on board video(shared ram with system) and a Celeron 550. And then Vista installed on it to boot. Sounds like a netbook, and a very old one at that. 1gb of ram? I wouldn't expect they would be able to run anything, after vista is loaded, effectively even assuming a fresh install and a fully optimized OS.
[/quote]
It should be easily capable of running a SDL game.

##### Share on other sites
Well my SDL game should have been made with OpenGL, its actually not that simple when i think of it...

##### Share on other sites
[quote name='rip-off' timestamp='1313944931' post='4851938']
[quote name='0Circle0' timestamp='1313939816' post='4851916']
Their computer stats are quite dated. 1gb or ram, on board video(shared ram with system) and a Celeron 550. And then Vista installed on it to boot. Sounds like a netbook, and a very old one at that. 1gb of ram? I wouldn't expect they would be able to run anything, after vista is loaded, effectively even assuming a fresh install and a fully optimized OS.
[/quote]
It should be easily capable of running a SDL game.
[/quote]

They keyword is "should", however if everyone else who uses the program runs it fine and then it is tested against a system with very limited resources and performs slowly then I would have to say the system is dated or unmaintained.

"Should" is a relative term, which implies that on new hardware, fresh vista install, and fully updated drivers as well as optimized OS then sure the Celeron 550 and 1GB of ram "should" run SDL games easily; however this hardware isn't new, the OS isn't new, and I am willing to bet the OS isn't frequently re-installed(or maintained) to keep it running as fast as possible, as the typical computer user never even re-installs(or even knows how to maintain a computer) any OS when their computer gets slow, they just assume their computer is just getting old and accepts the sluggish behavior.

"Should" and "Will" are completely different terms and "should" declares the statement as an opinion. Opinions(as the word "Should" implies) on if a specified hardware can or cannot run a specified application needs to be kept to real world testing rather than a guess with out any testing. My netbook with 2GB of ram and a much faster N260(Dual Core) will bog down against a high resource SDL or Allegro at times, and the netbook is frequently kept in prime condition and well maintained.

You're talking about a Celeron 550 (Single Core Processor released in late '06) with 1GB system ram that is shared with the video(which can take anywhere from 32MB-128 MB (Mine takes 128 as I use the same integrated video) depending on driver support), and then make the assumption that the said system with minimum system requirement for just running the OS (Windows Vista in this case) will then be able to effectively run anything else. This just isn't the case, as the test showed.

Is that to say there isn't any problems with the code, no. There may be many problems; however there may be none. No room for opinionated responses that completely contradict a real world test that was conducted against the program the OP mentioned to the hardware they also mentioned.

##### Share on other sites
[quote]
"Should" and "Will" are completely different terms and "should" declares the statement as an opinion.
[/quote]
Yes, [u]of course it is an opinion[/u]. Just as it is [u]your opinion[/u] that the machine is "outdated".

Maybe I'm just old fashioned, but that machine would have been considered extremely high end during the heyday of 2D gaming. I find it hard to think of a reasonable 2D game that should cause such a machine a significant burden.

The OP's link seems to be broken, so I cannot gauge how intensive the game might be. Maybe they're making Plasma Pong II, or something similar that would cause trouble for such a machine, I don't know. Given the OP's posting history however, this is unlikely to be the case. What is far more likely is that they are making some minor mistake which is costing them dearly on a much slower machine.

[quote]
My netbook with 2GB of ram and a much faster N260(Dual Core) will bog down against a high resource SDL or Allegro at times, and the netbook is frequently kept in prime condition and well maintained.
[/quote]
This is ridiculous. Are you seriously suggesting you need such high specifications to run 2D games? I don't know what your definition of "high resource" is, but it seems to me it must be insane if it requires such power to run.

[quote]
No room for opinionated responses that completely contradict a real world test that was conducted against the program the OP mentioned to the hardware they also mentioned.
[/quote]
I stand by my answer (again: unless the OP is writing a very unusual SDL game). I believe the OP is doing something incorrectly if their game doesn't work on such hardware.

##### Share on other sites
[quote name='bennettbugs' timestamp='1313878186' post='4851736']
on my computer it runs fine, its just that one person who has problems.
[/quote]

Doesn't matter if the computer is or is not outdated. This single line, is evidence that they cannot run it. By the usage of "just" which indicates more than one person was involved in testing should bring the conclusion that their hardware is not capable of running the program. Also indicated by "just" is that others are not having the same problem.

Is the computer outdated by standards of today? Yes(Which makes my statement not an opinion, but rather a observation of the standards of today). Is it outdated in the standards of running a 2D game made with basic SDL? No. Could the computer have other problems that would interrupt the games ability to effectively run? Possible, but inconclusive as no relevant data can be ascertained about the said computer. Does the game run fine for every other person who tried to run it? Yes.

Should the OP have kept the password the same on the source so that one of us could possibly look through the code instead of having 2 people speculate on what could be the cause instead of actually finding the real cause? Yes. Will they allow the code to be looked over again? Maybe. Is this discussion now pointless without further data on the subject? Yes.

Btw, the OP may very well indeed be doing something wrong. I'll concede to that; however there may be more to it.

High Resource can be defined as an excessive or large amount of memory being used by an application that exceeds the standard.
Standard can be defined the average expected usage of a given program.

Average 2D games will not exceed said computers system specs, but the op did say:
[quote name='bennettbugs' timestamp='1313950839' post='4851970']
its actually not that simple when i think of it...
[/quote]

##### Share on other sites
[quote]
Doesn't matter if the computer is or is not outdated. This single line, is evidence that they cannot run it.
[/quote]
Of course the hardware isn't capable of running the [i]current implementation[/i] of application! My speculation is that the hardware [i]should be capable[/i] - i.e. the application [i]is doing something wrong[/i].

[quote]
By the usage of "just" which indicates more than one person was involved in testing should bring the conclusion that their hardware is not capable of running the program. Also indicated by "just" is that others are not having the same problem.
[/quote]
It really depends on the range of hardware tested. You're inferring an awful lot that should be stated explicitly before being relied upon.

[quote]
Is the computer outdated by standards of today? Yes(Which makes my statement not an opinion, but rather a observation of the standards of today). Is it outdated in the standards of running a 2D game made with basic SDL? No. Could the computer have other problems that would interrupt the games ability to effectively run? Possible, but inconclusive as no relevant data can be ascertained about the said computer. Does the game run fine for every other person who tried to run it? Yes.

Should the OP have kept the password the same on the source so that one of us could possibly look through the code instead of having 2 people speculate on what could be the cause instead of actually finding the real cause? Yes. Will they allow the code to be looked over again? Maybe. Is this discussion now pointless without further data on the subject? Yes.
[/quote]
Is starting every second sentence with a question incredibly annoying? Yes.

[quote]
High Resource can be defined as an excessive or large amount of memory being used by an application that exceeds the standard.
Standard can be defined the average expected usage of a given program.
[/quote]
Sounds like circular logic.

This aside, I cannot think of any reasonable definition of a "standard" 2D SDL game that requires 1GB of RAM and whatever a N260 is - a dual core processor means probably exceeds 1GHZ at least. Just because there might exist games that tax such a system, doesn't mean anything. They could be written poorly.

[quote]
Average 2D games will not exceed said computers system specs...
[/quote]
That is exactly my point!

[quote]
...but the op did say:
[quote]
its actually [b]not that simple[/b] when i think of it...
[/quote]
[/quote]

He said it was not simple. Not simple doesn't mean complex, it doesn't even mean his game is above average in terms of its resource demands.

##### Share on other sites
[quote][b]Operating System[/b]: Windows Vista Home Premium (6.0, Build 6002)
[b]System Model[/b]: Toshiba Satellite Pro L300
[b]Processor[/b]: Intel Celeron CPU - 550 @ 2.00GHz
[b]Memory[/b]: 1014MB
[b]DirectX[/b]: 11[/quote]
With this computer you can't do a great thing >_<. I think you need a better computer first . Latest game egnine need more than that .

##### Share on other sites
The 2D game more than likely will not use 1GB obviously, that isn't the issue. What is the issue is that Windows Vista [b]alone[/b] uses that 1GB, the whole thing and then some. Leaving nothing left for anything else. Apparently you do not know how a computer works and how memory is allocated. GHZ is not a rate on how fast a PC is. A Pentium 4 overclocked to 5GHZ cannot beat my N260 at 1.6GHZ. Just because his processor says 2GHZ means nothing, it doesn't mean it can do much more then run the OS and Browser(the laptop is a budget laptop as well from 2006-2007). Windows Vista ALONE is using all the systems resources and anything run after the fact is using, assuming any is even available, virtual memory. Which is based off ram installed, so 1GB in this case which is stored on the HDD, 5400 RPM and a generous 33MB/s(probably a lot lower) HDD from late 2006-early 2007. Then it is read off that drive with an access time of over 15ms (easily higher given the drives age). Take into account that the Video(integrated) on that system is using 256MB of ram leaving 768MB ram for the OS and a 1GB of virtual memory, and the system OS (Vista) requires 1GB(Minimum usually double that) alone, the OS is already loading itself into virtual memory, making everything from then on out to load from a slow HDD.

By the way: N260 is a processor and when you said you didn't even know what dual core was, I face palmed. You probably should rethink talking about speeds of a computer and what it can and cannot do with a computer tech when you apparently do not know how one works to begin with. Take yourself over to a computer forum or read wiki. I recommend ExtremeOverclocking(EOCF or anything alike) a place I have been posting my computers that I have built since before that laptop even hit mainstream.

Keep down rating my comments

Circular logic, now that is funny. Here is an example of what [b]actual[/b] circular logic looks like.
You don't know what you're talking about because you don't know what you're talking about.

This isn't even about the OP any more, but rather an inability for someone to grasp simple concepts about what a computer can and cannot do. I am done here, no reason to teach an old dog new tricks when the tricks are too difficult for the dog they are being taught to.

##### Share on other sites
[quote]
when you said you didn't even know what dual core was...
[/quote]
You misread, I never said I did not know what a dual core processor was.

I did a basic Google for N260 processor, and didn't find any specs forthcoming. I was just using the fact that it was a dual core processor to make a ball-park assumption of its approximate clock rate. Any consumer dual core processor I've heard of has been near or over 1GHz, hence my assumption.

That said, I don't keep current with hardware 100% of the time, unless I'm in the market for a new computer.

[quote]
GHZ is not a rate on how fast a PC is
[/quote]
It gives an approximate level, appropriate to the kind of granularity required for this.

[quote]
Keep down rating my comments
[/quote]
Happy to oblige!

[hr]

You're dragging the conversation away from the topic. The topic is whether the OP can do anything to fix his code to make it run on the stated hardware. I believe there are lots of things that can be done, an outline of which is enumerated in my first response.

Your response seems to imply "the computer is old, give up". You've retracted from that somewhat in later posts, but this is the core message you're giving across that I'm trying to debunk.

Your off topic rant about overclocking and computer specifications is distracting. I'm not particularly interested in continuing this debate. This will be my last post on this point.

##### Share on other sites
[quote name='rip-off' timestamp='1314027886' post='4852353']
[quote]
GHZ is not a rate on how fast a PC is
[/quote]
It gives an approximate level, appropriate to the kind of granularity required for this.
[/quote]
This statement is so wrong it isn't even funny.
[quote name='rip-off' timestamp='1314027886' post='4852353']
[quote]
Keep down rating my comments
[/quote]
Happy to oblige!
[/quote]
Obvious abuse of power. Good job.

##### Share on other sites
Circle, your argument has nothing to do with the thread, and doesn't belong here. You're also being needlessly combative and dismissive, which needs to stop.

If you're going to disagree with people, at least provide some facts to back up your claims. This "nuh uh, [i]you're[/i] wrong" business is not welcome here.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account

• ### Forum Statistics

• Total Topics
628314
• Total Posts
2982022

• 9
• 9
• 13
• 11
• 14