tips for memory managment
Moderator: Coders of Rage
- mv2112
- Chaos Rift Junior
- Posts: 240
- Joined: Sat Feb 20, 2010 4:15 am
- Current Project: Java Tower Defence Game
- Favorite Gaming Platforms: N64/Xbox 360/PC/GameCube
- Programming Language of Choice: C/++, Java
- Location: /usr/home/mv2112
- Contact:
Re: tips for memory managment
What type of memory managment? Need more details.cndr wrote:does any one have any tips for memory management?
- cndr
- Chaos Rift Cool Newbie
- Posts: 65
- Joined: Sat Apr 24, 2010 7:03 am
- Programming Language of Choice: etc.
Re: tips for memory managment
any really, I'm trying to improve my coding, so anything as basic as ints take up more space than short ints, I'm new at memory management so any help would be great.
Long Live The Queen!
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: tips for memory managment
the amount of memory used (even by very larg programs) is typically pretty low. You really only have to worry about dynamic allocation, and that's only if you're forgetting to free A LOT of memory. Aside from that, it shouldn't be a major issue.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- short
- ES Beta Backer
- Posts: 548
- Joined: Thu Apr 30, 2009 2:22 am
- Current Project: c++, c
- Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
- Programming Language of Choice: c, c++
- Location: Oregon, US
Re: tips for memory managment
a crude way to see if you have a memory leak is too:
a) make sure every new has a delete (preventative)
b) watch your systems task manager resource usage. if it is going up over time, for your program, you may have a leak. remember when marcel's editor had sys req's of over a gig ram and they caught it in task manager? baha
a) make sure every new has a delete (preventative)
b) watch your systems task manager resource usage. if it is going up over time, for your program, you may have a leak. remember when marcel's editor had sys req's of over a gig ram and they caught it in task manager? baha
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
link: https://github.com/bjadamson
-
- Chaos Rift Junior
- Posts: 345
- Joined: Tue Jan 12, 2010 7:23 pm
- Favorite Gaming Platforms: PC - Windows 7
- Programming Language of Choice: c++;haxe
- Contact:
Re: tips for memory managment
With today's computers with gigabytes of memory, basic types wont make much difference no matter what you are trying. Even with embedded and hadnhelds and such, the amount of memory your using for basic types doesn't matter so much anymore. If it matters, you'll know anyway. I'd only think of it if you were doing a LARGE allocation of the same class, such as a particle manager, and even then I wouldn't think of it that much.
Or simply said: You'll know if you need to conserve the memory usage of your basic types.
The one thing I will suggest is to use references in place of pointers whenever possible. It's very easy to create a memory leak with pointers, and not so easy if it's even possible with references.
Or simply said: You'll know if you need to conserve the memory usage of your basic types.
The one thing I will suggest is to use references in place of pointers whenever possible. It's very easy to create a memory leak with pointers, and not so easy if it's even possible with references.
Re: tips for memory managment
There's a few options available:
1. Use smart/shared pointer containers
2. Write a garbage collector
3. Explictly allocate & deallocate memory on demand
Using smart pointers gives you the advantage of regular pointers with automatic reference counting to destroy objects and deallocate the associated memory once no references to an object exist.
Garbage collection has the advantage of allocating memory is large chunks to reduce the overhead of single object allocations/deallocations and uses a set of heuristics to determine if memory is no longer in use.
Option 3 is what you should avoid unless you need strict control over how much memory your application is using (e.g. when dealing with embedded devices).
1. Use smart/shared pointer containers
2. Write a garbage collector
3. Explictly allocate & deallocate memory on demand
Using smart pointers gives you the advantage of regular pointers with automatic reference counting to destroy objects and deallocate the associated memory once no references to an object exist.
Garbage collection has the advantage of allocating memory is large chunks to reduce the overhead of single object allocations/deallocations and uses a set of heuristics to determine if memory is no longer in use.
Option 3 is what you should avoid unless you need strict control over how much memory your application is using (e.g. when dealing with embedded devices).
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: tips for memory managment
Use destructors. Their entire point is to encapsulate the memory deallocation process.cndr wrote:does any one have any tips for memory management?
Coming from a C/++ background, I'm not so sure how realistic these are.Genesis wrote:1. Use smart/shared pointer containers
2. Write a garbage collector
3. Explictly allocate & deallocate memory on demand
Writing a garbage collector is pretty overkill when you're using a language like C++ whose explicit memory management comes from the low-level nature of the language. If you aren't going to be handling your resources yourself, then perhaps you should consider writing whatever project in Java/C# or another language with a runtime and garbage collector?
I'm not exactly sure how to interpret this. In the C/++ world, manual memory management is just how things work. You are responsible for deallocating what you allocate--embedded devices or not. You can't just "avoid" that in the language.Genesis wrote:Option 3 is what you should avoid unless you need strict control over how much memory your application is using (e.g. when dealing with embedded devices).
Unless your advice is to use C#/Java for their garbage collection and avoid C++ and manual memory allocation unless you're working on an embedded system?
Re: tips for memory managment
here are some pitfalls i have learnt to avoid.
1. Be very careful of std function that allocate memory, strdup for example. its easy to forget to free those.
2. Be very careful when using class constructor in function call as a perameter. for example, you might have a function foo(bar *obj), then you call it foo(new bar(params)); (this goes for anything that allocate memory really)
3. Make very good destructors. especially when you have composition. and in times of inheritance you can run into issues as well.
4. Umm... all i can think of now. but generally its fairly easy to spot where a leak is occuring. sometimes its not so easy to fix.
also, avoid allocating and deallocating frequently. malloc, free, new, delete. can be time costly.
if you can get away witrh using the stack. sure go for it. if you need persistance, estimate how much you need and allocate it once.
1. Be very careful of std function that allocate memory, strdup for example. its easy to forget to free those.
2. Be very careful when using class constructor in function call as a perameter. for example, you might have a function foo(bar *obj), then you call it foo(new bar(params)); (this goes for anything that allocate memory really)
3. Make very good destructors. especially when you have composition. and in times of inheritance you can run into issues as well.
4. Umm... all i can think of now. but generally its fairly easy to spot where a leak is occuring. sometimes its not so easy to fix.
also, avoid allocating and deallocating frequently. malloc, free, new, delete. can be time costly.
if you can get away witrh using the stack. sure go for it. if you need persistance, estimate how much you need and allocate it once.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"