   [java] Weird memory related java behaviour
  Hello,I have implemented a ray-tracer, and having some issues with trying to optimize some of my code.for rotation, I use quaternions, right now the code allocates new Objects every time i do rotation, i converted it to use pre-allocated objects, but it does not work.i feel like i am missing something but cannot see what is wrong.below is the relevant code.any help would be appreciated.the pixel loop   public void run(){  float tempx,tempy,tempz;  Vector3f RC=new Vector3f();  Quat4f Rotation=new Quat4f(),RotationConj=new Quat4f(),QuatDir=new Quat4f();  if(DEBUG) System.out.println("Starting "+ Thread.currentThread());  for(int i=x0;i<x1;i++){ for(int j=y0;j<y1;j++){//r[j].org=org;//XXX:// save original datatempx=r[j].dir.x;tempy=r[j].dir.y;tempz=r[j].dir.z;// do rotation and do intersection and color// XXX:  rotateVecAroundAxisNoAlloc is not working properly//Rotation.x=0;Rotation.y=0;Rotation.z=0;Rotation.w=0;//RotationConj.x=0;RotationConj.y=0;RotationConj.z=0;RotationConj.w=0;//QuatDir.x=0;QuatDir.y=0;QuatDir.z=0;QuatDir.w=0;//rotateVecAroundAxisNoAlloc(r[j].dir,xAxis,xRot,Rotation,RotationConj,QuatDir);//rotateVecAroundAxisNoAlloc(r[j].dir,yAxis,yRot,Rotation,RotationConj,QuatDir);r[j].dir=rotateVecAroundAxis(r[j].dir,xAxis,xRot);r[j].dir=rotateVecAroundAxis(r[j].dir,yAxis,yRot);//r[j].calcInv();RC.set(0, 0, 0);getColorFromRay(r[j],0,BIH,RC);// restore valuesr[j].dir.x=tempx;r[j].dir.y=tempy;r[j].dir.z=tempz;int rr=(int) (RC.x * 255),gg=(int) (RC.y * 255),bb=(int) (RC.z * 255);img[j*w+i]=0xFF000000 | ((rr & 0xff) << 16) | ((gg & 0xff) << 8) | (bb &0xff); }  }  synch.release();  if(DEBUG) System.out.println("Ending  "+ Thread.currentThread());   }    }Method for rotating that currently works.public static final Vector3f rotateVecAroundAxis(final Vector3f V,  Vector3f Axis,float theta){   Quat4f Rotation,RotationConj,quatV;   quatV=new Quat4f(V.x,V.y,V.z,0);   RotationConj=new Quat4f();   // build rotation   Rotation=new Quat4f( Axis.x*(float)Math.sin(theta*0.5), Axis.y*(float)Math.sin(theta*0.5), Axis.z*(float)Math.sin(theta*0.5), (float)Math.cos(theta*0.5));   // buld Conjucate of rotation   RotationConj.conjugate(Rotation);   //multiply and get final result   Rotation.mul(quatV);   Rotation.mul(RotationConj);   return new Vector3f(Rotation.x,Rotation.y,Rotation.z);    }code that is not working:public static final void rotateVecAroundAxisNoAlloc( Vector3f V, // what vector to rotate final Vector3f Axis,final float theta, // what axis and angle Quat4f Rotation,Quat4f RotationConj,Quat4f quatV)  // temps    {   quatV.x=V.x;quatV.y=V.y;quatV.z=V.z;quatV.w=0; // build rotation Rotation.x=Axis.x*((float)Math.sin(theta*0.5f)); Rotation.y=Axis.y*((float)Math.sin(theta*0.5f)); Rotation.z=Axis.z*((float)Math.sin(theta*0.5f)); Rotation.w=(float)Math.cos(theta*0.5f);   RotationConj.conjugate(Rotation);   //multiply and get final result   Rotation.mul(quatV);   Rotation.mul(RotationConj);   V.x=Rotation.x;V.y=Rotation.y;V.z=Rotation.z;    }
  Is there any difference between using or not these lines?It seems they are not needed, so if there was any difference it could point to the bug.Rotation.x=0;Rotation.y=0;Rotation.z=0;Rotation.w=0;RotationConj.x=0;RotationConj.y=0;RotationConj.z=0;RotationConj.w=0;QuatDir.x=0;QuatDir.y=0;QuatDir.z=0;QuatDir.w=0;Post code for Vector3f and Quatf constructors.Post code for Quatf.conjugate and Quatf.mul methods.Also, if rotateVecAroundAxisNoAlloc is a static function, assuming that the code doesn't need to be thread-safe, why don't you use static fields for Rotation, RotationConj and QuatDir.Why don't you follow standard Java code conventions? http://java.sun.com/docs/codeconv/[Edited by - ppgamedev on April 28, 2010 2:32:29 AM]