- Home /
Taking Screenshot Thumbnail causes memory spike that is not released
Do I have a memory leak here? I have a routine that runs at the top of each level to take a screenshot in JPG format and scale it down to a smaller thumbnail size to be used later in a saved game load routine. My problem is that when I monitor the memory usage in XCode, the memory spikes but does not come back down -- even after I do a System.GC or UnloadUnusedAssets -- just for good measure.
Below is the function in full (I include sections I've commented out to maybe shed light on my thinking):
function takeBoardScreenShot() {
var imageFilename : String = globalData.currSetInfo().setID + "_" + currentPlayer.currentBoard + ".jpg";
var fileNameWithPath : String = Application.persistentDataPath + "/" + imageFilename;
if (!File.Exists(fileNameWithPath)) {
// FIX ME: Find a way to take screenshot without background !!!
var screenShotCam : Camera = Camera.main;
var resWidth = Screen.width;
var resHeight = Screen.height;
var rt : RenderTexture = new RenderTexture(resWidth, resHeight, 24);
//screenShotCam.targetTexture = rt;
var screenShot : Texture2D = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false);
RenderTexture.active = rt;
screenShotCam.Render();
yield new WaitForEndOfFrame();
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0);
screenShotCam.targetTexture = null;
RenderTexture.active = null; // JC: added to avoid errors
Destroy(rt);
// reduce image to one quarter size
var absMinWidth : int = Screen.width / 4;
if (screenShot.width > absMinWidth)
TextureScale.Bilinear(screenShot, absMinWidth, (absMinWidth / parseFloat(Screen.width)) * Screen.height);
//var bytes : byte[] = screenShot.EncodeToPNG();
//System.IO.File.WriteAllBytes(fileNameWithPath, bytes);
//System.IO.File.WriteAllBytes(fileNameWithPath, screenShot.EncodeToPNG());
System.IO.File.WriteAllBytes(fileNameWithPath, screenShot.EncodeToJPG());
Debug.Log(String.Format("Took screenshot to: {0}", fileNameWithPath));
globalData.updateThumbPath(currentPlayer.currentBoard, imageFilename);
//bytes = null;
Destroy(screenShot);
}
levelState = EnumLevelState.eTakeThumbnailComplete;
}
Some things worth mentioning, "globalData" is a DontDestroyOnLoad gameObject that has a function updateThumbPath to store the name and path of the screenshot in user prefs. It only writes the text file path name of the thumb image and not the mage itself so I don't think that's where my problem is. I tried the Profiler with the Trial Pro License but could not see anything.
Any tips?
Thanks, Manny
Your answer
![](https://koobas.hobune.stream/wayback/20220613163809im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Assign Default For Large Array 1 Answer
Scripting Javascript 1 Answer
Change instance local variable through RPC to All and to one 0 Answers