- Home /
Save Webcamtextures. Why running out of memory (ram)?
I know this is not a nice code but I need help with the overall problem that I am running out of memory (same happens if I use a LinkedList). What the code does is, that I save a picture around every 40ms till I have 1000 texture2Ds in an array and then start overwriting from the begin. But is seems that the overwritten textures are not deleted from the memory although they are out of scope.
public RawImage display;
public AspectRatioFitter imageFitter;
public static Texture2D[] picArray;
public static WebCamTexture webCamTex;
public static int numberSavedPics = 1000;
public static int counter = 0;
private int firstPic = 0;
private int lastPic = -1;
void Start () {
picArray = new Texture2D[numberSavedPics];
WebCamDevice cam = WebCamTexture.devices [0];
webCamTex = new WebCamTexture (cam.name);
if (display != null) {
display.texture = webCamTex;
imageFitter = display.gameObject.GetComponent<AspectRatioFitter> ();
}
webCamTex.Play ();
}
// Update is called once per frame
void FixedUpdate () {
if (display != null) {
float videoRatio = (float)StartScript.webCamTex.width/(float)StartScript.webCamTex.height;
imageFitter.aspectRatio = videoRatio;
}
if (counter < numberSavedPics) {
counter++;
} else {
deletePic ();
}
savePic ();
}
private void savePic(){
Texture2D snap = new Texture2D (webCamTex.width, webCamTex.height);
snap.SetPixels (webCamTex.GetPixels ());
snap.Apply ();
lastPic = ((lastPic + 1) % numberSavedPics);
picArray [lastPic] = snap;
}
private void deletePic(){
firstPic = ((firstPic + 1) % numberSavedPics);
}
Answer by Bunny83 · Nov 26, 2017 at 04:21 PM
Any object that is derived from UnityEngine.Object has to be manually Destroy ed. This holds true for any such object which includes GameObjects, Meshes, Textures, ... Of course assets which are shipped with your application should not be destroyed (though it's possible).
Any UnityEngine.Object derived object in memory can be found again by using FindObjectsOfType. Those objects are "tracked" objects which actually live on the C++ side of Unity. Those objects have a managed part (which exists in the managed memory of the Mono framework) and a native part which lives in unmanaged memory of the engine.
Hey, thank you for your answer. But it is not all clear for me. I added the line`Destroy(picArray[firstpic])` to the deletePic() function but the memory usage still increases. I intend to map these textures from the array onto a rawimage every 40ms from an other script. Thats why the array is static. Do I have to destroy the rawimage.texture as well?
Your answer
Follow this Question
Related Questions
Out of memory(Max Size) 2 Answers
how do i get differences between a background and webcam texture like a green screen? 2 Answers
Texture 2D Arrays in Parallax Occlusion Mapping 0 Answers
Check if no webcam 0 Answers
Texture 2D array to GUI.Window rows iOS 0 Answers