🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #9198

Re: Problem with memory again

#9198
From: Bob Gonder
Date: Sat, 24 Jul 2004 07:16
60 lines
2809 bytes
Sara T. wrote:

> If my application takes a lot of memory and never
>free (although I free up in my code), system will be slow down and cannot
>run another application in the same machine at all.
>
>Does it have a possible to manage this problem?

1) Use stack-based variables where you can instead of new or malloc.
I think Windows gives each thread 1 MB of stack space, so use it.
I often use
{	char message[128];
	sprintf( message,"Format"));
}
where I know for sure that message is going to be  less than 80;
I also regularly have structures on the stack that are long[50][132];
If you don't need the data to hang around after you leave a routine,
put it on the stack.


2) Allocate big chuncks of memory, and then break them up into the
smaller pieces your thread needs.
BYTE*Whole=malloc((28*sizeof(long))+sizeof(struct1)+sizeof(struct2));
long*One = Whole;
long*Two = Whole+sizeof(long);
struct1*S1 = While+28*sizeof(long);
	Use them
free(Whole);


3) Use the WinAPI
long*One = GlobalAlloc( GMEM_FIXED ,sizeof(long) );
GlobalFree( One );
But it's better to alloc arrays, and put small variables on the stack.
I prefer GMEM_FIXED because it returns an address instead of a handle,
and handles are in short supply, so don't use GMEM_MOVEABLE unless you
really need it.


4) This probably applies to all methods of allocation:  Free in the
reverse order.
	One = malloc()
	if( One )
	{	Two = malloc()
		if( Two )
		{	Three = malloc()
			if( Three )
			{	free(Three)
			}
			free(Two)
		}
		free(One)
	}
This gives the garbage collector the best shot at restoring the free
memory chain.


5) I'll say it again. Use a fixed set of (16) threads that allocate
their data once, then reuse it, only releasing the data (and
terminating) when the program is exiting.
Technical Details
Message-ID: <e114g01bs4bjh4j1ec2fbtpm4qai4c3bvt@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4100b152@newsgroups.borland.com> <4100c97e@newsgroups.borland.com> <4101d3e2@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,e9f11b4f112d3007
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Problem with memory again
Date: Sat, 24 Jul 2004 00:16:02 -0700
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <e114g01bs4bjh4j1ec2fbtpm4qai4c3bvt@4ax.com>
References: <4100b152@newsgroups.borland.com> <4100c97e@newsgroups.borland.com> <4101d3e2@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 69.34.96.208
X-Trace: newsgroups.borland.com 1090653359 69.34.96.208 (24 Jul 2004 00:15:59 -0700)
Lines: 63
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!logbridge.uoregon.edu!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail