I run GetPixels32() every frame and getting data from it , it is running smoothly around ~40 fps , but behind the background , the memory is keep building , the first time i didnt know about it and my unity just hangs when the memory is full and i had to force end unity from task manager . I already tried disposing data by setting all the variable to null everytime the variable is no longer needed , but the memory still increase for unknown reason. Forcing garbage cleaner to run does not solve the problem , the memory still increasing , forcing garbage cleaner to run not only did not free up the memory but also reducing fps because it is very expensive.
Here are the video showing the memory building up , i had to stop the unity at certain memory percentage otherwise my computer will hangs (froze) and it takes a lot of time to get my computer back responding
Here is the code , the brief explanation of the code : i get rendertexture from camera , then i convert it to Texture2D using readpixels and apply() , then i copy the data i need and return it to the function caller .
public List<float> getdata()
{
List<float> toreturn = new List<float>();
// ---- thecamera data start ----
RenderTexture rendText = new RenderTexture(thecameraresolution.x,thecameraresolution.y,thecameradepth,RenderTextureFormat.ARGB32);
thecamera.targetTexture = rendText;
RenderTexture.active = rendText;
// render the texture
thecamera.Render();
// create a new Texture2D with the camera's texture, using its height and width
Texture2D cameraImage = new Texture2D(thecamera.targetTexture.width, thecamera.targetTexture.height, TextureFormat.RGB24, false);
cameraImage.ReadPixels(new Rect(0, 0, thecamera.targetTexture.width, thecamera.targetTexture.height), 0, 0);
cameraImage.Apply();
// dispose
RenderTexture.active = null;
// get data
Color32[] getpixels = cameraImage.GetPixels32();
foreach(Color32 pixeldata in getpixels)
{
toreturn.Add(pixeldata.r);
toreturn.Add(pixeldata.g);
toreturn.Add(pixeldata.b);
toreturn.Add(pixeldata.a);
}
// dispose
rendText = null;
cameraImage = null;
getpixels = null;
// ---- thecamera data end ----
// ---- thecamera rotation data start ----
toreturn.Add(thecamera.transform.rotation.x + 1);
toreturn.Add(thecamera.transform.rotation.y + 1);
toreturn.Add(thecamera.transform.rotation.z + 1);
toreturn.Add(thecamera.transform.rotation.w + 1);
// ---- thecamera rotation data end ----
return toreturn;
}
And the function caller in update :
void Update()
{
List<float> thedata;
thedata = getdata();
Debug.Log(thedata.Count);
// dispose
thedata = null;
}
The code is straightforward and should not be that hard too understand.
How do i fix this memory leak problem ?
Edit : i already tried removing debug log from code, but it does not fix the problem