I have created a simple application with one button from UI that executes just one line of code when I click on it. This is the code:
public void CaptureScreenShot()
{
Application.CaptureScreenshot ("Screenshot.png");
}
After I capture one screenshot, the memory increases by about 10 MB then drops only 7 MB
http://postimg.org/image/6r5tiaqjv/
When I take second screenshot, it goes up 7 MB, but doesn't drop at all, until I flip the device, and only then it goes to the starting amout of memory + 3 MB
http://postimg.org/image/am4m74kbv/
And then, if I want to take 2 screenshots in the row, it goes up like +25 MB of starting memory and drops 2 MB after a little period of time
http://postimg.org/image/5yyk5cwyz/
And even if I flip the device, it goes like 2 MB lower, but it is like +20 MB of the memory I use when I start the device. The point is, after taking at least one screenshot, I will never get the memory back. (The memory I had in the start)
Then I decided to try capture a screenshot manually with this code:
public void TakeScreen()
{
// Timestamp returns the date, so each screenshot name would be unique
string pngFile = Application.persistentDataPath+"/+Timestamp+".png";
Texture2D texture = new Texture2D(Screen.width, Screen.height);
texture.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
texture.Apply();
byte[] bytes = texture.EncodeToPNG();
File.WriteAllBytes(pngFile,bytes);
DestroyObject( texture );
}
Same thing happens with memory usage as with Application.CaptureScreenshot();
Could someone explain, what am I doing wrong, or is there a way to avoid this memory leak?
I have the same problem when I try to save a picture from WebCamTexture():
public void TakeSnap ()
{
// Timestamp returns the date, so each screenshot name would be unique
string pngFile = Application.persistentDataPath+"/+Timestamp+".png";
Texture2D texture = new Texture2D(wct.width, wct.height);
texture.SetPixels32(wct.GetPixels32());
texture.Apply ();
var bytes = texture.EncodeToPNG();
File.WriteAllBytes( pngFile,bytes);
DestroyObject( texture );
}
↧