Most memory effective way to do this

Started by
2 comments, last by Tallkotten 11 years, 8 months ago
Hello!

I'm in need of help. I am about to build a system that displays info about an item on the screen and i need some help to understand which of 2 ways is the most memory effective.

So basically in my game i am making there are items on the ground. If the player holds down CTRL he/she gets up an UI that draws out the stats and name of the item.

I currently got a sloppy but functional system and i want to improve it. I got 2 ways in mind.

First way:

First create a object of the class in the initiation:

ItemInfo* UI = new ItemInfo();


And then when i want to draw something new i contact the build function in the class to update it.

UI->build(all the variables);


When i want to show it i switches a bool which makes it draw on the screen. I switch the bool to false when i want to hide it.

This way the UI is basically always gonna be on the screen (in the memory), just invisible to the user most times.

Second way:

When i want to draw the UI i create a new object of the class and sends all the necessary variables with it.

ItemInfo* UI = new ItemInfo(all the variables);


Then if i don't want to show it anymore i delete the object and set the pointer to NULL

delete UI;
UI = NULL;


If i want to show it again/update it i recreate it again, but with different variables. Of course if it isn't deleted already i have to do that first

ItemInfo* UI = new ItemInfo(all the variables);


I show it if the pointer != NULL, if it is NULL I don't try to draw it (since there isn't anything to draw)

This way is more memory and CPU intense when i am showing itemInfo but the memory is freed when i ain't doing anything with it.

Which way is more effective?
Advertisement
How often will you be displaying item info? If it's reasonably often, if there is almost always some item info on-screen, and if the item for which info is being displayed changes frequently enough, then you should probably accept the extra memory overhead, otherwise you're looking at a potentially bad run-time bottleneck.

Even if not, you should definitely be profiling memory usage to determine if you are or are not worrying overmuch about something that may not even be a problem. Even if you've got a coupla extra MB usage, this is almost totally insignificant by todays standards - the days of having to be ultra-frugal about memory usage, and when memory usage was highly likely to be a major program bottleneck, are over.

That's not an excuse for going nuts or doing stupid things; it just means that focussing solely or even primarily on memory usage is not as important as it used to be.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Take the first approach. Instanciate all the widgets you need and display them ,when needed. There's no reason to create/delete them all the time.

How often will you be displaying item info? If it's reasonably often, if there is almost always some item info on-screen, and if the item for which info is being displayed changes frequently enough, then you should probably accept the extra memory overhead, otherwise you're looking at a potentially bad run-time bottleneck.

Even if not, you should definitely be profiling memory usage to determine if you are or are not worrying overmuch about something that may not even be a problem. Even if you've got a coupla extra MB usage, this is almost totally insignificant by todays standards - the days of having to be ultra-frugal about memory usage, and when memory usage was highly likely to be a major program bottleneck, are over.

That's not an excuse for going nuts or doing stupid things; it just means that focussing solely or even primarily on memory usage is not as important as it used to be.


Well that depends on the player. But imagine Diablo, there you look at the stats a whole lot. Or WOW, where you check your stats on items a lot as well.


Take the first approach. Instanciate all the widgets you need and display them ,when needed. There's no reason to create/delete them all the time.


Guess i'm going with the first option. I were aware that it might no make any difference since like you said there is a lot more memory to work with today, but i always feel that i want to do it "the right way" with as little sloppiness as possible tongue.png

Thanks for a quick reply you both! :)

This topic is closed to new replies.

Advertisement