13 Replies - 1120 Views - Last Post: 23 September 2009 - 02:58 PM Rate Topic: -----

#1 lute  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-September 09

double values memory usage

Post icon  Posted 21 September 2009 - 01:54 PM

I'm sure I'm missing something very basic here, but here it is anyway..
I have this simple program :

#include <iostream>
#include <windows.h>
using namespace std;

double *p;
int i;

int main () {
for (i=0;i<10000000;i++) p=new double;
Sleep(10000);
return 0;
}


I can't figure out why the windows task manager is indicating that it uses ~548mb.
Shouldn this use like ~(10000000*8byte)/1024^2=76.3mb

Maybe I should go to sleep?

Is This A Good Question/Topic? 0
  • +

Replies To: double values memory usage

#2 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: double values memory usage

Posted 21 September 2009 - 02:00 PM

It could be because when creating dynamic the data is stored in different places of the RAM not in line or even close so it takes alot of work to find them. Also that is why there is a delete function once the new is finished using you have to delete it.
Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: double values memory usage

Posted 21 September 2009 - 02:10 PM

Anything else aside, you have a serious memory leak problem. You allocating quite a bit, but not freeing any of it. How many times can you run the program before your system crashes?
Was This Post Helpful? 1
  • +
  • -

#4 lute  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-September 09

Re: double values memory usage

Posted 21 September 2009 - 02:13 PM

View Postponcho4all, on 21 Sep, 2009 - 01:00 PM, said:

It could be because when creating dynamic the data is stored in different places of the RAM not in line or even close so it takes alot of work to find them. Also that is why there is a delete function once the new is finished using you have to delete it.

Thanks! I know I have to delete it at some point but the large memory usage wories me, as I'm trying to create a parser that loads a lot of double values into memory using dynamic allocation.
It uses about 7 times the amount of the actual data. Isn't this a bit ro much?
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: double values memory usage

Posted 21 September 2009 - 02:29 PM

Is that all you have going on or is it just a snippet? What compiler are you using? Any additional libraries? Any thing linked at runtime? There could be unaccounted for overhead.
Was This Post Helpful? 0
  • +
  • -

#6 lute  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-September 09

Re: double values memory usage

Posted 21 September 2009 - 02:33 PM

View PostKYA, on 21 Sep, 2009 - 01:29 PM, said:

Is that all you have going on or is it just a snippet? What compiler are you using? Any additional libraries? Any thing linked at runtime? There could be unaccounted for overhead.



This is just a small stupid test I ran after I've seen the memory usage on my actuall program.The numbers I provided in my post are from this test.I use VS2008.
Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: double values memory usage

Posted 21 September 2009 - 02:46 PM

You sure you read it right? This only takes up 2,804 KB:

#include <iostream>
#include <istream>
#include <windows.h>
#include <vector>
using namespace std;

int main(){
	double* p;
	vector<double*> largeVect;
	for (int i=0;i<10000000;i++) {
		p=new double;
		largeVect.push_back(p);//save the handle for later freeing
	}
	for(vector<double*>::iterator iter = largeVect.begin(); iter != largeVect.end(); ++iter){
		delete (*iter);//free
	}
	return 0;
}



edit: Point being, I'm running several windows of firefox and even they don't take up 500 MB worth of memory.

This post has been edited by KYA: 21 September 2009 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#8 lute  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-September 09

Re: double values memory usage

Posted 21 September 2009 - 03:00 PM

View PostKYA, on 21 Sep, 2009 - 01:46 PM, said:

You sure you read it right? This only takes up 2,804 KB:

#include <iostream>
#include <istream>
#include <windows.h>
#include <vector>
using namespace std;

int main(){
	double* p;
	vector<double*> largeVect;
	for (int i=0;i<10000000;i++) {
		p=new double;
		largeVect.push_back(p);//save the handle for later freeing
	}
	for(vector<double*>::iterator iter = largeVect.begin(); iter != largeVect.end(); ++iter){
		delete (*iter);//free
	}
	return 0;
}



edit: Point being, I'm running several windows of firefox and even they don't take up 500 MB worth of memory.


Well here is a print screen of the task manager when I run your code which column is the right one to read?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: double values memory usage

Posted 21 September 2009 - 03:08 PM

Are you running it from the IDE or running the .exe it makes?

From inside the IDE I get ~2,500K peak mem usage, whereas if i run it stand alone I peak out at 196,548K which is ~196 MB.

10000000*8 = 80000000 bytes = 80000 KB

The vector holds 10000000 pointers which are 4 bytes each = 40000000 = 40000 KB

80000+40000 = 120,000 KB I imagine the other 70 ish KB is overhead from out STL/Windows headers + anything Vs might have added in terms of project overhead.

edit: I'm using VS 2005.

This post has been edited by KYA: 21 September 2009 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#10 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: double values memory usage

Posted 21 September 2009 - 03:16 PM

What is the effect of the 196MB on the comp?
Since its a simple program it would go up to 196 only for a few seconds right?
But what about a larger program, at some point you are using lots more of variables and reserving lots of memory. If this simple program takes 196MB what if 12 of this ran at the same time. Would it kill a 2GB RAM comp?
Was This Post Helpful? 0
  • +
  • -

#11 lute  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-September 09

Re: double values memory usage

Posted 21 September 2009 - 03:21 PM

View PostKYA, on 21 Sep, 2009 - 02:08 PM, said:

Are you running it from the IDE or running the .exe it makes?

From inside the IDE I get ~2,500K peak mem usage, whereas if i run it stand alone I peak out at 196,548K which is ~196 MB.

10000000*8 = 80000000 bytes = 80000 KB

The vector holds 10000000 pointers which are 4 bytes each = 40000000 = 40000 KB

80000+40000 = 120,000 KB I imagine the other 70 ish KB is overhead from out STL/Windows headers + anything Vs might have added in terms of project overhead.

edit: I'm using VS 2005.


I'm running the .exe it makes. Your numbers make sense. On my system it peaks at 590mb.
Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: double values memory usage

Posted 21 September 2009 - 03:47 PM

View Postponcho4all, on 21 Sep, 2009 - 03:16 PM, said:

What is the effect of the 196MB on the comp?
Since its a simple program it would go up to 196 only for a few seconds right?
But what about a larger program, at some point you are using lots more of variables and reserving lots of memory. If this simple program takes 196MB what if 12 of this ran at the same time. Would it kill a 2GB RAM comp?


The peak would be with all pointers allocated with the vector at full capacity. This only occurs for a short while, then it decreases as memory is freed. For large programs (namely games) the allocation and usage of memory has to be carefully planned out as it is a limited resource.
Was This Post Helpful? 1
  • +
  • -

#13 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: double values memory usage

Posted 21 September 2009 - 04:05 PM

Thank you for the response KYA.
Was This Post Helpful? 0
  • +
  • -

#14 lute  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-September 09

Re: double values memory usage

Posted 23 September 2009 - 02:58 PM

View PostKYA, on 21 Sep, 2009 - 02:47 PM, said:

View Postponcho4all, on 21 Sep, 2009 - 03:16 PM, said:

What is the effect of the 196MB on the comp?
Since its a simple program it would go up to 196 only for a few seconds right?
But what about a larger program, at some point you are using lots more of variables and reserving lots of memory. If this simple program takes 196MB what if 12 of this ran at the same time. Would it kill a 2GB RAM comp?


The peak would be with all pointers allocated with the vector at full capacity. This only occurs for a short while, then it decreases as memory is freed. For large programs (namely games) the allocation and usage of memory has to be carefully planned out as it is a limited resource.


Thanks alot! I figured out why the memory usage was so hi...
I was building in debug mode and executing that .exe file :wub:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1