Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program Memory usage problem

Status
Not open for further replies.

djones7936

Programmer
Jul 20, 2002
24
0
0
US
Hi

I have a weird problem. I have a program that loads about 20-30MB worth of arrays using the new operator, the program does some processing, then it uses the delete operator to release the all of the said memory that was allocated. My understanding is the delete operator unallocates memory which can be used again. The program continues with this loading and unloading process many times before it actually exits.

I'm pretty sure I do not have "memory leaks"; that is I use a delete operator for every new operator, (there shouldn't be any orphaned allocated memory taking up space.)

In Windows XP's Task Manager, as my program runs, it's "Memory Usage" (under the "Processes" tab) flucuates between 40MB-100MB (which is good). Unfortunately, also as my program runs, the Page File Usage continues to rise well past 800MB. (I only have 256MB of RAM so this sucks. :) ) I can't have my program "use" so much memory. :-(

Currently, the amount of memory taken up by all of my processes is about 60MB (including my program), and the page file usage is at 870MB.

Does anyone know why the page file usage is so high even though the memory usage for the program is under 100MB? I thought using the delete operator frees memory. Is there any way to decrease the amount of page file usage (Virtual Memory usage)? How can I get my program to use a normal amount of memory?

Help! Thanks!,
-Dave
 
Hmm, are you sure you called the delete-operator correctly?
When you allocate an array, you should do it like this:

Code:
Something* blabla = new Something[100];
// ...
delete [] blabla;

Otherwise I would suggest using a tool like BoundsChecker to check for MemoryLeaks or memory errors.
 
Hi FloViel,

I think you are right. There has to be a memory leak problem someplace. I ran a test program which, allocates about 100 MB of memory, then deallocates it. It does that 4 times.

The memory monitor rose and fell as the program ran, as it was suppose to, and all of the memory was returned at the end of the program.

My theory of how the new and delete operators work seems to hold, so there must by a problem with my main program.

Here is the test code I used...
Code:
#include <stdio.h>

struct HQRGB24
{
	double clrR[1024][768];
	double clrG[1024][768];
	double clrB[1024][768];
	HQRGB24* next;
};

void main()
{
	int i;
	int k;
	HQRGB24* root = NULL;
	HQRGB24* ptr;

	for(i=0;i<4;i++)
	{
		printf(&quot;Creating...\n&quot;);
		
		root = new HQRGB24();
		ptr=root;
		for(k=0;k<7;k++)
		{
			ptr->next = new HQRGB24();
			ptr=ptr->next;
		}
		ptr->next=NULL;

		printf(&quot;Deleting...\n&quot;);
		while(root!=NULL)
		{
			ptr=root->next;
			delete(root);
			root=ptr;
		}
	}

	printf(&quot;Check your memory usage, then press Enter...\n&quot;);
	getchar();

}
I will start looking for memory leaks,

-Dave
 
Well, as far as I see it, your program is completly correct!
No memory leaks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top