• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by manny003 · Sep 26, 2014 at 10:54 AM · javascriptmemory leakscreencapture

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Instantiate prefab on a grid pattern? 1 Answer

Array of custom class objects, possible? 1 Answer

NAT target not connected to NAT facilitator 67.225.180.24:50005 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges