We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Out of Memory question
Page Index Toggle Pages: 1
Out of Memory question (Read 473 times)
Out of Memory question
Oct 1st, 2006, 3:43pm
 
Hi,
I have read the FAQ about 'out of memory' issues and acted as instructed. I am however still experiencing problems with memory and would like to know more about the factors that lead to the system becoming out of memory.

My system is:
Mac G5 - Dual 2 GHz
2 GB DDR SDRam
OS X 10.4.6

The project I am currently working on includes multiple classes of quite small (100 x 200 pixels approx) sprite images. Creating multiple objects of these classes seems to prompt the problem with memory.

As I have only a basic knowledge of programming I don't understand where the memory is being used given that the complete size of my data folder is only 4.8MB. Below is an example of a class in my project:

//*********************************************************// Person 1 Class
//*********************************************************

class Person_1_Left
{
 // Person 1 Variables
 
 int num_Person_1_Frames = 34;
 int num_Person_1_Move_Right = 34;
 int Person_1_Frame = 0;
 int Person_1_Y = 210;
 
 float Person_1_X;
 
 PImage[] Person_1_Left = new PImage [num_Person_1_Frames];  


 Person_1_Left()
 {
   Person_1_Left[0] = loadImage ("Pete_L1.png");
   Person_1_Left[1] = Person_1_Left[0];
   Person_1_Left[2] = loadImage ("Pete_L2.png");
   Person_1_Left[3] = Person_1_Left[2];
   Person_1_Left[4] = loadImage ("Pete_L3.png");
   Person_1_Left[5] = Person_1_Left[4];
   Person_1_Left[6] = loadImage ("Pete_L4.png");
   Person_1_Left[7] = Person_1_Left[6];
   Person_1_Left[8] = loadImage ("Pete_L5.png");
   Person_1_Left[9] = Person_1_Left[8];
   Person_1_Left[10] = loadImage ("Pete_L6.png");
   Person_1_Left[11] = Person_1_Left[10];
   Person_1_Left[12] = loadImage ("Pete_L7.png");
   Person_1_Left[13] = Person_1_Left[12];
   Person_1_Left[14] = loadImage ("Pete_L8.png");
   Person_1_Left[15] = Person_1_Left[14];
   Person_1_Left[16] = loadImage ("Pete_L9.png");
   Person_1_Left[17] = Person_1_Left[16];
   Person_1_Left[18] = loadImage ("Pete_L10.png");
   Person_1_Left[19] = Person_1_Left[18];
   Person_1_Left[20] = loadImage ("Pete_L11.png");
   Person_1_Left[21] = Person_1_Left[20];
   Person_1_Left[22] = loadImage ("Pete_L12.png");
   Person_1_Left[23] = Person_1_Left[22];
   Person_1_Left[24] = loadImage ("Pete_L13.png");
   Person_1_Left[25] = Person_1_Left[24];
   Person_1_Left[26] = loadImage ("Pete_L14.png");
   Person_1_Left[27] = Person_1_Left[26];
   Person_1_Left[28] = loadImage ("Pete_L15.png");
   Person_1_Left[29] = Person_1_Left[28];
   Person_1_Left[30] = loadImage ("Pete_L16.png");
   Person_1_Left[31] = Person_1_Left[30];
   Person_1_Left[32] = loadImage ("Pete_L17.png");
   Person_1_Left[33] = Person_1_Left[32];
 }


 void Person_1_Walks_Left (float Person_1_X, int Person_1_Y)
 {
   Person_1_Frame = (Person_1_Frame + 1) % num_Person_1_Move_Right;
   image(Person_1_Left[Person_1_Frame], Person_1_X, Person_1_Y);  
 }
}
//*********************************************************

Could this be down to inefficient programming or am I going about this in the wrong way?

If anyone would like to take a look at the project as a whole to get a better idea I can zip up the whole folder and send it by email (if possible)

Thanks

Andy
Re: Out of Memory question
Reply #1 - Oct 1st, 2006, 6:15pm
 
I have just updated to the 0117 Beta and the memory availability function has helped quite a lot (I must not have followed the 'Out of memory' FAQ suggestions correctly).

However, the default setting of 256 MB available RAM  was completely taken up by a total data size of 4.8 MB, and so I would still like to know how all of this memory had been taken up.

I am a compete novice at this and so I am sure that there are many other things happening that I am unaware of, but I would like to know what these are in order for me to improve my overall programming.

Thanks
Re: Out of Memory question
Reply #2 - Oct 2nd, 2006, 4:23am
 
It seems to me that the problem is that every time you create an instance of the class all the images are loaded into memory. So if your images take up 4.8MB, if you create 4 instances of that class you will have loaded (4.8 * 4)MB into memory. You should change it so that all of the class instances reference the same images so that you only load the images into memory once. I think you could do this by making the image array a static property and then create a static method called loadResources() or something like that. Then before you create any instances of the class you'd do:

Person_1_Left.loadResources();
Re: Out of Memory question
Reply #3 - Oct 2nd, 2006, 7:23pm
 
Hi again,
That makes good sense to me. I'll try it and post back up to let everyone know if it works.
Thanks
Andy
Re: Out of Memory question
Reply #4 - Oct 3rd, 2006, 9:09pm
 
Hi,
that worked a treat. I can now get my app running with 8MB of RAM.

Thanks

Andy
Page Index Toggle Pages: 1