Memory Leaks?
BlitzMax Forums/BlitzMax Programming/Memory Leaks?
| ||
I trying to figure out if I have a memory leak. Below shows the build from one level to the next. Does anyone know if this is normal. It seems like memory build a bit when there nothing really going on? Does it just increment slowly. Also, when the memory is freed by the garbage collector, does it happen immediately? Start Game (B):=92374 Start Game (E):=120225 Weapons Menu Initialize (B):=128613 Weapons Menu Initialize (E):=140377 OGG (B):=157767 OGG (E):=157833 Start Next Level (B):=226649 Start Next Level (E):=236723 Weapons Menu Initialize (B):=240113 Weapons Menu Initialize (E):=254679 OGG (B):=257673 OGG (E):=257739 Thanks |
| ||
No memory is not freed immediately. It is repooled and freed if not used for quite some time and there is still a remaining pool the GC can create new objects from. What das B / E mean? |
| ||
B - Begining E - Ending Some Process above allacate memory as they go. When should I be concerned that the memory is growing evreytime I restart the game or start a new level? |
| ||
Normally it isn't a problem. But with cyclic object linking (so an object is linked to itself over a few other), you must be very carefull as those are not freed even if the reference to such a structure is null-ed (BM does not keep root reference so it can't see that the whole cyclic thing is no where used anymore). One thing that can temporaly raise the memory usage is when you do a lot of string operations, as they always create a temp string. But this should only go up to a given point where it will stay afterwards (the freeing and creation of new strings keeps each other balanced) Restarting should not be a problem, nor should a new level, as long as you make sure that you null-ify everything you do not need (I always use TList and TMap to take care of existance handling for my object so I normally do not need global nor manual null-ifying. I remove them from the list and thats it) |
| ||
So what about in the following example: [Creating a Ninja] Create Sword Create Ninja and pass Sword as a parameter Attach Nija to the World in TLIST [When Ninja Dies] Remove Ninja from TLIST Also, I assume GCCollect() will not fix this? What is still in memory? Both Sword and Nija? |
| ||
Assuming you have no other references to Ninja but in the World list, everything should be gone. When ninja is removed from the list, there are no remaining handles, so ninja is removed. Because sword's handle was in the memory that's just been freed, it should be gone too. I *believe* what Dreamora means is if your Sword had a handle to Ninja ( perhaps an owner field ) then this would cause problems because they both have references to each other and could never be freed without specifically clearning the owner reference as well. |
| ||
Yes, thats what I meant. Or if you have something like GrandParents, Parents and Childs where all of them know their parents, grandparents and childs, then it would not help to free the family, as they are all linked within the family as well so the whole family would still exist within the active data (as each object has at least one reference pointing to it) but it would not be accessable anymore to you. |
| ||
Hmm, memory not being freed is a lot of work. Is there anyway to determine which objects are not cleared? |
| ||
A simple one: If a reference to this object exists, it is not freed. You only have to have a clear structure for handling references, so you can easily say how many references exist to an object. |