Sign in to follow this  

GC collecting ghost? :

Recommended Posts

I am trying to optimize my engine (C# + SlimDX) to make as less allocations as possible (to prevent the GC from firing too often) using as guide a profiler that gives me where the garbaged object are generated. There is a method where I can't find anything declarated and i don't know what's to do. It seems this method generate 2 garbaged object per execution :
            if (LastCheckInstancesNumber != Instances.Count)
                LastCheckInstancesNumber = Instances.Count;
                _needToRegenerateUpdate = true;
            // Crea byte array da usare nel prossimo draw.
            if (_needToRegenerateUpdate)
                Int32 PrimitivesCount = Instances.Count;
                Int32 Size = PrimitivesCount * 80;

                if ((ByteUpdateTemp != null) && (ByteUpdateTemp.Length < Size))
                    ByteUpdateTemp = new byte[Size];
                int offset = 0;

                PrimitivesCount = 0;
                Int32 Count = Instances.Count;
                for (int i = 0; i < Count; i++)
                    InstancedBase3DObjectInstanceValues ib = Instances[i];
                    if (ib.Process)
                        MathHelper.CopyMatrix(ref ib._matrix, ref MatrixTemp);
                        MathHelper.CopyVector(ref ib._diffuseColor, ref ColorTemp);

                        ObjectUpdateTemp[0] = MatrixTemp.M11;
                        ObjectUpdateTemp[1] = MatrixTemp.M12;
                        ObjectUpdateTemp[2] = MatrixTemp.M13;
                        ObjectUpdateTemp[3] = MatrixTemp.M14;
                        ObjectUpdateTemp[4] = MatrixTemp.M21;
                        ObjectUpdateTemp[5] = MatrixTemp.M22;
                        ObjectUpdateTemp[6] = MatrixTemp.M23;
                        ObjectUpdateTemp[7] = MatrixTemp.M24;
                        ObjectUpdateTemp[8] = MatrixTemp.M31;
                        ObjectUpdateTemp[9] = MatrixTemp.M32;
                        ObjectUpdateTemp[10] = MatrixTemp.M33;
                        ObjectUpdateTemp[11] = MatrixTemp.M34;
                        ObjectUpdateTemp[12] = MatrixTemp.M41;
                        ObjectUpdateTemp[13] = MatrixTemp.M42;
                        ObjectUpdateTemp[14] = MatrixTemp.M43;
                        ObjectUpdateTemp[15] = MatrixTemp.M44;
                        ObjectUpdateTemp[16] = ColorTemp.X;
                        ObjectUpdateTemp[17] = ColorTemp.Y;
                        ObjectUpdateTemp[18] = ColorTemp.Z;
                        ObjectUpdateTemp[19] = ColorTemp.W;
                        ByteConverter.WriteSingleArrayToByte(ref ObjectUpdateTemp, ref ByteUpdateTemp, offset);
                        offset += 20;


                SynchronizedObject so = SynchronizationEventWriter.LockData();
                so.Synchronizedobject = ByteUpdateTemp;
                _needToRegenerateUpdate = false;

                so = SynchronizationEventWriterNum.LockData();
                so.Synchronizedobject = PrimitivesCount;

Can somebody guide me to understand why this function generate object to be garbaged? I really don't have a clue. Notes : The new byte[Size] is NEVER called due to caching. The MathHelper function simply copy each element (Single) from one object to another without creating anything. The base.Update() does almost nothing (and anyway is derived from ALL object in my engine, but only here i have the garbage object)

Share this post

Link to post
Share on other sites
I've resolved!!!

The problem was that the so.Synchronizedobject = PrimitivesCount; was assigning an Int32 to an Object class. It seems that this replaces every time the object causing the old object to be garbaged. I resolved by using a box class to enclose the Int32 object and simply change the value inside.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this