Visual c#方法调用使用太多内存

本文关键字:太多 内存 调用 方法 Visual | 更新日期: 2023-09-27 18:04:48

我正在使用XNA 4.0框架编写一款游戏。我已经编写了一组方法,将2D鼠标坐标转换为3d世界中的一条线,然后检查该线是否与平面相交,以及交点是否在该平面中的人脸范围内。

数学工作,但由于某种原因,当我做这些计算超过500帧时,它使程序停止。在垃圾收集决定清理之前,我可以看到内存使用量从开始的15 MB上升到大约130 MB。我明确地知道它在这段代码中,因为当我把它注释掉时,其他的一切都运行得很顺利。

我将粘贴我的代码下面,任何见解将是有帮助的,谢谢!

循环:

            GraphicObject me = new GraphicObject();
            Intersection intersect;
            double? dist = null;
            foreach (GraphicObject obj in GraphicObjects)
            {
                intersect = obj.intersectMe(line);
                if (intersect.Distance != null)
                {
                    if (intersect.Distance < dist || dist == null)
                    {
                        dist = intersect.Distance;
                        me = obj;
                    }
                    else
                    {
                        obj.Highlight(false);
                    }
                }
                else
                {
                    obj.Highlight(false);
                }
            }
            if (dist != null)
            {
                me.Highlight(true);
            }

intersectMe:

    public override Intersection intersectMe(Ray _line)
    {
        GraphicHelper.Intersects(_line, rect.Vertices[0].Normal, rect.Vertices[0].Position, intersect);
        if (intersect.Distance != null)
        {
            if (!rect.PointOnMe(intersect.X - position.X, intersect.Y - position.Y, intersect.Z - position.Z))
            {
                intersect.Distance = null;
            }
        }
        return intersect;
    }

GraphicsHelper。相交:

    // _l = line, _n = normal to plane, _p = point on the plane
    public static void Intersects(Ray _l, Vector3 _n, Vector3 _p, Intersection _i)
    {
        _i.Distance = null;
        float num = (_n.X * (_p.X - _l.Position.X) + _n.Y * (_p.Y - _l.Position.Y) + _n.Z * (_p.Z - _l.Position.Z));
        float denom = (_n.X * _l.Direction.X + _n.Y * _l.Direction.Y + _n.Z * _l.Direction.Z);
        if (denom != 0 && num != 0)
        {
            float t = num / denom;
            if (t > 0)
            {
                _i.X = _l.Position.X + _l.Direction.X * t;
                _i.Y = _l.Position.Y + _l.Direction.Y * t;
                _i.Z = _l.Position.Z + _l.Direction.Z * t;
                _i.Distance = _i.X * _i.X + _i.Y * _i.Y + _i.Z * _i.Z;
            }
        }
    }

PointOnMe:

    public bool PointOnMe(float _x, float _y, float _z)
    {
        float ex = _x - Vertices[3].Position.X;
        float ey = _y - Vertices[3].Position.Y;
        float ez = _z - Vertices[3].Position.Z;
        float ae = a.X * ex + a.Y * ey + a.Z * ez;
        float be = b.X * ex + b.Y * ey + b.Z * ez;
        ex = _x - Vertices[1].Position.X;
        ey = _y - Vertices[1].Position.Y;
        ez = _z - Vertices[1].Position.Z;
        float ce = c.X * ex + c.Y * ex + c.Z * ez;
        float de = d.X * ex + d.Y * ey + d.Z * ez;
        if (ae > 0 && be > 0 && ce > 0 && de > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

Visual c#方法调用使用太多内存

感谢大家花时间帮我看这个。错误实际上是在我如何处理obj.Highlight(), TaW的一脚在屁股上得到一个分析器设置帮助我弄清楚了。

    public override void Highlight(bool toggle)
    {
        if(toggle)
        {
            rect.Texture = new Texture2D(GraphicsManager.Graphics.GraphicsDevice, 1, 1);
            rect.Texture.SetData<Color>(new Color[] { Color.Yellow });
        }
        else
        {
            rect.Texture = new Texture2D(GraphicsManager.Graphics.GraphicsDevice, 1, 1);
            rect.Texture.SetData<Color>(new Color[] { squareColor });
        }
    }

每一帧所有的对象都有新的纹理生成。这是一种糟糕的做事方式。