Advertisement

out of memory question

Started by February 14, 2000 09:47 AM
0 comments, last by CarlFGauss 24 years, 7 months ago
I've remember reading a post in newsgroup saying that if you keep repeating allocating and deallocating memory, you will run out of memory because of poor memory management by the computer/OS. I don't remember reading that post in DJGPP newsgroup or MFC newsgroup because I read that post long time ago (in 1998 or 1999). I want to create a 3D editor that keep creating and destorying some windows control using MFC, and repeating creating and destroying vertex structure. My question is will I run out of memory like mentioned above (repeating allocating and deallocating) in MFC in Visual C++? For my information, I would also like to know whether I will run out of memory if I repeating allocating and deallocating in DJGPP. Thanks in advance. Edited by - CarlFGauss on 2/14/00 9:49:11 AM
When you repeatedly create and destroy memory objects, you can sometimes get Memory Fragmentation. when you allocate objects on the heap, they take up residence in the earliest block they can, and if you don''t delete things alot, the allocation is theoretically compact. however, when you delete something, a hole the size of the deleted object becomes available in the middle of the heap. That hole is only big enough for an object the same size, so if you allocate more memory that can''t fit inside of that hole, it gets placed at the end. After several repetitions, there will be lots of free memory, but in very small chunks scattered throughout the heap.

In your particular case, i''m not entirely sure if allocating and deallocating the same object over and over will really cause this... Use a heapwalking utility to find out how bad it really gets. This will happen regardless of what compiler you use.

To solve this problem, you can implement a memory reoranization algorithm that basically "defragments" your heap... it works, but is slower than dirt.
Better yet, is to keep a list of "free" objects. For instance, say you have a vertex structure that you keep dynamically deleting and allocating. When deleting the object, simply add it to a list of deleted vertecies, and do NOT deallocate the memory. When intializing an object, first check to see if there is available memory saved on the "deleted" stack, and if there is, use a chunck of that.
Its a mite slower, but if you are diligent, you will not have the memory fragmentation problem.

Of course, it would take a LOT of allocation/deallocation of memory to run out of space on most modern machines, so most people don''t worry about it. (My copy of 3DS Max seems to have a pretty severe problem with this in fact). Worry first about general memory leaks and functionality... then if you feel its necessary, keep your heap clean

*oof*

This topic is closed to new replies.

Advertisement