Mail Archives: djgpp/1998/09/25/09:18:19
On 25 Sep 98 at 7:59, Bitland wrote:
> I got version 2.8.1 and still have malloc-problems. Allocating memory in an
> absolutely simple program and freeing it in the next moment, displaying the
> memory with __dpmi_get_free_memory_information(&dfmi); gives confusing
> memory values and then the memory slowly gets low.
When you free blocks they won't be added to the free DPMI
memory information, but they will be available for future
mallocs to use.
> Here's the main loop (simulating making allegro bitmaps), with MAXSIZE 1024,
> CDEPTH 32 and MAPS so, that you cannot allocate more than you have, but be sure
> to get closely to the limit, then the effect comes faster.
>
> do
> {
> for (i=0;i<MAPS;i++)
> {
> hm[i] = malloc((CDEPTH>>3)*(random()%MAXSIZE * (random()%MAXSIZE)));
> if (!hm[i])
> textout(screen,font,"there it is !",200,20,COLWHITE); /* via allegro
> */
> }
> checkmem("allocated");
>
> for (i=0;i<MAPS;i++)
> if (hm[i])
> free(hm[i]);
> checkmem("freed ");
>
> } while (!keypressed());
>
> After this, you SOMETIMES cannot get a large memory block !
> And 'there it is' shouldn't appear at all, what if allocated with 'new' without
> a newhandler ? The program crashes...
I can understand this; your block sizes are random. Initially
then you get MAPS blocks of various sizes, then free them all.
Now the allocatable memory is fragmented into whatever block
sizes you used. This means that if you malloc again with
smaller block sizes then they'll fit in the old blocks, but
when you use a larger block it won't fit, and a new chunk of
DPMI memory must be allocated. You could be wasting a huge
amount of memory with these allocations which are effectively
slowly increasing in size. Allocate the largest possible size
initially, and either leave it allocated (generally unused
memory won't be paged in anyway) or free it if you must.
I don't think your example is very typical of a game program
(which I presume is what you're writing) anyway. I imagine it
took quite a few iterations of the loop before the problem
occured. In any case, running your system close to its memory
limit is not a good idea; set MAPS lower and you probably won't
have the problem, even if you leave it running for a very long
time.
--
george DOT foot AT merton DOT oxford DOT ac DOT uk
- Raw text -