Possible memory leak using the skeletal tracking in Unity

I have a very simple scene that attaches models to the body joints of any people that enter the scene. I also show the RGB feed on a plane.

When left running for long periods of time, the idle ram usage goes up and never fully releases. There is a bump in usage each time a body enters the frame and it dips again when it leaves, but over time that dip seems to always be smaller than the bump resulting in more memory being used (and eventually a crash).

There is no object instantiation or destruction going on (unused objects are moved out of view).

I am wondering if there is anything in the Astra.BodyFrame data that isn’t being cleaned up properly when a body leaves the frame.

Has anyone else encountered this?

Hi @Curt

in the example code you’ll notice that entries are constantly added to the _bodySkeletons dictionary (every time there’s a new body.Id) but they are never actually removed… that’s probably part of the problem.

you’d need to keep a list of tracked bodies vs the list of bodies in the frame, and remove the ones that had left

do we know the max value of a body.Id? I’m assuming it recycles them eventually.

 void UpdateSkeletonsFromBodies(Astra.Body[] bodies)
    {
        foreach (var body in bodies)
        {
            GameObject[] joints;
            if (!_bodySkeletons.ContainsKey(body.Id))
            {
                joints = new GameObject[body.Joints.Length];

                for (int i = 0; i < joints.Length; i++)
                {
                    joints[i] = (GameObject)Instantiate(JointPrefab, Vector3.zero, Quaternion.identity);
                    joints[i].transform.SetParent(JointRoot);
                }

               // NB: ADDED BUT NEVER REMOVED
                _bodySkeletons.Add(body.Id, joints);
            }
            else
            {
                joints = _bodySkeletons[body.Id];
            }