- Home /
Kinect SensorData Memory Leak
Hello all,
As in the title, I have a memory leak which I've had no luck in solving as of yet, it's something to do with this code:
private IEnumerator KinectFeedHandler()
{
Debug.Log("KinectFeedHandler");
while (!KinectManager.Instance)
{
Debug.Log("KinectColourFeed waiting for kinectmanager instance");
yield return new WaitForEndOfFrame();
}
KinectInterop.SensorData currentData = KinectManager.Instance.GetSensorData();
Renderer renderer = GetComponent<Renderer>();
while (true)
{
yield return new WaitForEndOfFrame();
Texture2D currentTexture2D = new Texture2D(currentData.colorImageWidth, currentData.colorImageHeight);
currentTexture2D.SetPixels32(currentData.colorImageTexture.GetPixels32());
currentTexture2D.Apply();
renderer.material.mainTexture = currentTexture2D;
}
}
if I have my script not run this enumerator, then it will stay at a steady ~235MB (Value from Windows 10 Task manager), whereas running this, the memory usage will rise a steady 100MB every second or so - testing with a kinect this enumerator does work as expected (taking a color feed and transferring it onto a material every frame) but is producing this unexpected memory usage.
I've tested nullifying the texture2D directly after assigning it to the mainTexture, declaring the variable outside the while(true) scope, I'm ensuring and have checked that there is only one KinectFeedHandler enumerator running, and trying to check the sensor data colorImage before assignment has been fruitless too.
This is being tested in an empty scene save for a camera, Quad (+ this script), KinectManager which is only computing the Color Map, and is using the "Kinect v2 with MS-SDK" unitypackage, and the material the texture2D is being set onto is a base Unlit Texture.
Answer by BigHandInSky · Feb 11, 2016 at 05:28 PM
Found it through the help of this reddit post, turns out unity doesn't handle the disposal of new Texture2D's nicely, so to get a kinect's color feed use this:
private IEnumerator KinectFeedHandler()
{
Debug.Log("KinectFeedHandler");
while (!KinectManager.Instance)
{
Debug.Log("KinectColourFeed waiting for kinectmanager instance");
yield return new WaitForEndOfFrame();
}
KinectInterop.SensorData currentData = KinectManager.Instance.GetSensorData();
Renderer renderer = GetComponent<Renderer>();
Texture2D currentTexture2D = new Texture2D(currentData.colorImageWidth, currentData.colorImageHeight);
while (true)
{
yield return new WaitForEndOfFrame();
currentTexture2D.SetPixels32(currentData.colorImageTexture.GetPixels32());
currentTexture2D.Apply();
renderer.material.mainTexture = currentTexture2D;
}
}
Your answer
Follow this Question
Related Questions
Memory Leak with creating a new Textrure2D 2 Answers
Dynamic face mesh from Kinect- applying a texture 0 Answers
cleanup allocated memory for texture2D 1 Answer
How to release memory of Texture2D dynamic object, or is there a memory-leak? 0 Answers
Loading external Image as Texture2D increases memory consumption dramatically on iOS 0 Answers