mctrivia Posted May 24, 2011 Share Posted May 24, 2011 i have made 2 classes. the first "point" has an array of 3 floats and some functions to help manipulate the values the second "triangle" has an array of 3 points. in this way if i want to make a tetrahedron i can make 4 points and assign them to 4 triangles. problem is for some reason each triangle point group seems to use 1k. any idea why? Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/ Share on other sites More sharing options...
Psycho Posted May 24, 2011 Share Posted May 24, 2011 any idea why? Inefficient code? Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/#findComment-1219617 Share on other sites More sharing options...
mctrivia Posted May 24, 2011 Author Share Posted May 24, 2011 i guess my question is what makes an object require ram? i would think point would need only enough space to store the 3 floats and triangle need only enough space to hold the 3 pointers. my math says 80 bit*3 for floats+64bit*3 for pointers=54 bytes but 1000 are being used. Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/#findComment-1219624 Share on other sites More sharing options...
Psycho Posted May 24, 2011 Share Posted May 24, 2011 Without knowing HOW it is coded this is just a hypothetical discussion. I can write two scripts that produce the exact same results but one uses exponentially more memory. You say you are using a class, right? So just initiating the class will take memory. I have no idea how you are running this or what output is produced. Are you running the client and the server on the same machine? This is the PHP Help forum where people post requests asking for help with their code. You have not provided code nor asked for help. If you are not going to post code, then this should be moved to the "Application Design" forum which includes "Programming theory, database design, performance, etc." Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/#findComment-1219639 Share on other sites More sharing options...
mctrivia Posted May 24, 2011 Author Share Posted May 24, 2011 class Point { protected $v=array(); public function __construct() { $this->id=++$_GET['zzz_library_model_lastpoint']; if (func_num_args()==0) { $this->v=array(0,0,0); } elseif (func_num_args()==1) { $this->v=func_get_arg(0); } elseif (func_num_args()==3) { $this->v=array(func_get_arg(0),func_get_arg(1),func_get_arg(2)); } } public function __get($name) { switch (strtolower($name)) { case 'x': return $this->v[0]; break; case 'y': return $this->v[1]; break; case 'z': return $this->v[2]; break; case 'v': return $this->v; break; case 'id': return $this->id; break; } } public function __set($name,$value) { switch (strtolower($name)) { case 'x': $this->v[0]=$value; break; case 'y': $this->v[1]=$value; break; case 'z': $this->v[2]=$value; break; case 'v': $this->v=$value; break; } } public function __toString() { return pack("f",$this->v[0]) . pack("f",$this->v[1]) . pack("f",$this->v[2]); } public function rotate($theta,$axis) { //theta in rads use deg2rad() if you want deg. switch (strtolower($axis)) { case 'x': $x=cos($theta)*$this->v[1]-sin($theta)*$this->v[2]; $y=sin($theta)*$this->v[1]+cos($theta)*$this->v[2]; $this->v[1]=$x; $this->v[2]=$y; break; case 'y': $x=cos($theta)*$this->v[2]-sin($theta)*$this->v[0]; $y=sin($theta)*$this->v[2]+cos($theta)*$this->v[0]; $this->v[2]=$x; $this->v[0]=$y; break; case 'z': $x=cos($theta)*$this->v[0]-sin($theta)*$this->v[1]; $y=sin($theta)*$this->v[0]+cos($theta)*$this->v[1]; $this->v[0]=$x; $this->v[1]=$y; break; } } public function translate($x,$y,$z) { $this->v[0]+=$x; $this->v[1]+=$y; $this->v[2]+=$z; } public function scale($x,$y=null,$z=1) { if ($y==null) { $y=$x; $z=$x; } $this->v[0]*=$x; $this->v[1]*=$y; $this->v[2]*=$z; } } // ************************************************************************************************************ class Triangle { protected $v=array(); public function __construct() { if (func_num_args()==0) { $this->v=array(new Point(),new Point(),new Point()); } elseif (func_num_args()==3) { $this->v=array(func_get_arg(0),func_get_arg(1),func_get_arg(2)); } elseif (func_num_args()==9) { $this->v=array( new Point(func_get_arg(0),func_get_arg(1),func_get_arg(2)), new Point(func_get_arg(3),func_get_arg(4),func_get_arg(5)), new Point(func_get_arg(6),func_get_arg(7),func_get_arg() ); } return !(($v[0]==$v[1]) || ($v[0]==$v[2]) || ($v[1]==$v[2])); } public function __set($name,$value) { $t=substr($value,1,1); switch (substr(strtolower($name),0,1)) { case 'x': $this->v[$t]->x=$value; break; case 'y': $this->v[$t]->y=$value; break; case 'z': $this->v[$t]->z=$value; break; case 'v': $this->v[$t]=$value; break; } } public function __get($name) { if (strlen($name>1)) { $t=substr($value,1,1); } else { $t=0; } switch (substr(strtolower($name),0,1)) { case 'x': return $this->v[$t]->x; break; case 'y': return $this->v[$t]->y; break; case 'z': return $this->v[$t]->z; break; case 'v': return $this->v[$t]; break; case 'p': return array_merge($this->v[0]->v,$this->v[1]->v,$this->v[2]->v); break; case 'n': return $this->getNormal(); break; } } public function __toString() { $n=$this->getNormal(); return pack("f",$n[0]).pack("f",$n[1]).pack("f",$n[2]).$this->v[0].$this->v[1].$this->v[2].pack("S",0); } protected function getNormal() { $a0=$this->v[2]->x-$this->v[0]->x; $a1=$this->v[2]->y-$this->v[0]->y; $a2=$this->v[2]->z-$this->v[0]->z; $b0=$this->v[1]->x-$this->v[0]->x; $b1=$this->v[1]->y-$this->v[0]->y; $b2=$this->v[1]->z-$this->v[0]->z; $x=$a1*$b2-$a2*$b1; $y=$a2*$b0-$a0*$b2; $z=$a0*$b1-$a1*$b0; $l=sqrt($x*$x+$y*$y+$z*$z); $x/=$l; $y/=$l; $z/=$l; return array($x,$y,$z); } } Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/#findComment-1219644 Share on other sites More sharing options...
Psycho Posted May 24, 2011 Share Posted May 24, 2011 i guess my question is what makes an object require ram? i would think point would need only enough space to store the 3 floats and triangle need only enough space to hold the 3 pointers. my math says 80 bit*3 for floats+64bit*3 for pointers=54 bytes but 1000 are being used. Ok, looking at your code there are a heck of a lot more variables than what you state above. Just because your class is returning x number variables doesn't mean that more variables were created (and stored in memory). Case in point: protected function getNormal() { $a0=$this->v[2]->x-$this->v[0]->x; $a1=$this->v[2]->y-$this->v[0]->y; $a2=$this->v[2]->z-$this->v[0]->z; $b0=$this->v[1]->x-$this->v[0]->x; $b1=$this->v[1]->y-$this->v[0]->y; $b2=$this->v[1]->z-$this->v[0]->z; $x=$a1*$b2-$a2*$b1; $y=$a2*$b0-$a0*$b2; $z=$a0*$b1-$a1*$b0; $l=sqrt($x*$x+$y*$y+$z*$z); $x/=$l; $y/=$l; $z/=$l; return array($x,$y,$z); } That function returns three variables, but there are ten total variables generated within that function ($a1, $a2, $a3, etc.). I also suspect that some memory is used as overhead in performing the mathematical operations that isn't necessarily freed up immediately. Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/#findComment-1219648 Share on other sites More sharing options...
mctrivia Posted May 24, 2011 Author Share Posted May 24, 2011 those are all temporary. i thought if they were within a function they were automatically given up when function completed. Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/#findComment-1219653 Share on other sites More sharing options...
Psycho Posted May 24, 2011 Share Posted May 24, 2011 Without knowing how you are tracking memory usage and at what time there are any number of reasons why you are getting the results you have stated. Besides, memory usage is a tricky thing. Also, just because a function/script ends doesn't mean the memory is instantaneously released. Take a look at this post with some relevant info: http://stackoverflow.com/questions/1145775/why-does-this-simple-php-script-leak-memory Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/#findComment-1219670 Share on other sites More sharing options...
mctrivia Posted May 25, 2011 Author Share Posted May 25, 2011 thanks I will initialize the garbage collector and see what else I can do to save memory. Quote Link to comment https://forums.phpfreaks.com/topic/237341-saving-ram/#findComment-1219821 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.