The GPU device instance has been suspended. Monogame.Forms.DX

Hi,
I am working with a Monogame.Forms.DX for my editor that is supposed to draw a shape for every object from a json file that will be loaded using Open File Dialog and it does it, but the problem is that I get error
“SharpDX.SharpDXException: 'HRESULT: [0x887A0005], Module: [SharpDX.DXGI], ApiCode: [DXGI_ERROR_DEVICE_REMOVED/DeviceRemoved], Message: The GPU device instance has been suspended. Use GetDeviceRemovedReason to determine the appropriate action.”
after a while from loading the file depending on how big the array is
I also notices a rapid increase in the process memory in my diagnostic tools(around 2.6-3.6 GB )when the error occurs
Screenshot 2023-02-14 044917
is it possible to fix this problem ?

for more context on my editor functionality.
I store the JArray in my main form then call it to my editor class and request an update function to update the array every frame with the update method in order to catch any change that can happen to array

//Form1
 public static void UpdateArray()
        {
           Editor.UpdateSj(array);
        }
//Editor
 public static void UpdateArray(JArray updatedarray)
        {
            array= updatedarray;
        }
protected override void Update(GameTime gameTime)
        {
          
            Form1.UpdateArray();
            base.Update(gameTime);
        }
  protected override void Draw()
        {

           base.Draw();
            GraphicsDevice.Clear(MBlue);
            Editor.spriteBatch.Begin();
          DisplayArray();
            Editor.spriteBatch.End();

        }

also if this might help

See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
SharpDX.SharpDXException: HRESULT: [0x887A0005], Module: [SharpDX.DXGI], ApiCode: [DXGI_ERROR_DEVICE_REMOVED/DeviceRemoved], Message: The GPU device instance has been suspended. Use GetDeviceRemovedReason to determine the appropriate action.

at SharpDX.Result.CheckError() in C:\projects\sharpdx\Source\SharpDX\Result.cs:line 195
at SharpDX.Direct3D11.Device.CreateBuffer(BufferDescription& descRef, Nullable`1 initialDataRef, Buffer bufferOut) in C:\projects\sharpdx\Source\SharpDX.Direct3D11\Generated\DESKTOP_APP\Interfaces.cs:line 1236
at Microsoft.Xna.Framework.Graphics.ConstantBuffer.PlatformInitialize()
at Microsoft.Xna.Framework.Graphics.ConstantBuffer…ctor(ConstantBuffer cloneSource)
at Microsoft.Xna.Framework.Graphics.Effect.Clone(Effect cloneSource)
at Microsoft.Xna.Framework.Graphics.Effect…ctor(GraphicsDevice graphicsDevice, Byte[] effectCode, Int32 index, Int32 count)
at Microsoft.Xna.Framework.Graphics.BasicEffect…ctor(GraphicsDevice device)
at MonoGame.Forms.DX.Controls.ViewPort.DrawMiniAxes(Vector3 ParentPos, GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection) in C:\Users\ahmed\source\repos\Editor1\Editor1\Controls\ViewPort.cs:line 1236
at MonoGame.Forms.DX.Controls.ViewPort.BoneDisplay() in C:\Users\ahmed\source\repos\Editor1\Editor1\Controls\ViewPort.cs:line 510
at MonoGame.Forms.DX.Controls.ViewPort.Draw() in C:\Users\ahmed\source\repos\Editor1\Editor1\Controls\ViewPort.cs:line 1208
at MonoGame.Forms.Controls.GraphicsDeviceControl.OnPaint(PaintEventArgs e)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9105.0 built by: NET481REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll

Bone Json Editor
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/Bone%20Json%20Editor.exe

System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9075.0 built by: NET481REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll

System
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9065.0 built by: NET481REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll

System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll

Newtonsoft.Json
Assembly Version: 13.0.0.0
Win32 Version: 13.0.2.27524
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/Newtonsoft.Json.DLL

System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9115.0 built by: NET481REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll

MonoGame.Forms.DX
Assembly Version: 2.4.0.0
Win32 Version: 2.4.0.0
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/MonoGame.Forms.DX.DLL

MonoGame.Framework
Assembly Version: 3.8.0.270
Win32 Version: 3.8.0.270
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/MonoGame.Framework.DLL

SoulsFormats
Assembly Version: 1.0.2.0
Win32 Version: 1.0.2.0
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/SoulsFormats.DLL

SharpDX.DXGI
Assembly Version: 4.2.0.0
Win32 Version: 4.2.0
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/SharpDX.DXGI.DLL

SharpDX.Direct3D11
Assembly Version: 4.2.0.0
Win32 Version: 4.2.0
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/SharpDX.Direct3D11.DLL

System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll

System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll

SharpDX
Assembly Version: 4.2.0.0
Win32 Version: 4.2.0
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/SharpDX.DLL

SharpDX.XAudio2
Assembly Version: 4.0.1.0
Win32 Version: 4.0.1
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/SharpDX.XAudio2.DLL

Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll

AssimpNet
Assembly Version: 3.3.1.0
Win32 Version: 3.3.1
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/AssimpNet.DLL

SharpDX.XInput
Assembly Version: 4.2.0.0
Win32 Version: 4.2.0
CodeBase: file:///C:/Users/ahmed/source/repos/Editor1/Editor1/bin/Debug/SharpDX.XInput.DLL

System.Numerics
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll

System.Runtime.Serialization
Assembly Version: 4.0.0.0
Win32 Version: 4.8.9032.0 built by: NET481REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll

Microsoft.VisualStudio.Debugger.Runtime.Desktop
Assembly Version: 17.0.0.0
Win32 Version: 17.4.11109.4
CodeBase: file:///D:/SteamLibrary/steamapps/common/Visual%20Studio/Common7/IDE/Remote%20Debugger/x86/Runtime/Microsoft.VisualStudio.Debugger.Runtime.Desktop.dll

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Content of

DisplayArray();

Is only relevant thing, code you posted are basic monogame functions that work perfectly fine, we need to see your code to know what went wrong. Based on error it seems you are creating very high amount of GPU buffers each frame, but it’s long shot based on what you shared.

also you can follow provided suggestion for more data as well as enabling graphic debugging layer
Use GetDeviceRemovedReason to determine the appropriate action

here the rest of my code

protected override void Initialize()
        {
            instance = this;
          
            this.graphicsDevice = GraphicsDevice;

          
            spriteBatch = new SpriteBatch(GraphicsDevice);
            basicEffect = new BasicEffect(GraphicsDevice);
            basicEffect.EnableDefaultLighting();
            basicEffect.LightingEnabled = true;
            basicEffect.DirectionalLight0.Enabled = true;
            basicEffect.DirectionalLight0.DiffuseColor = Color.Yellow.ToVector3();
            basicEffect.AmbientLightColor = Color.Red.ToVector3();
            basicEffect.DirectionalLight0.Direction = new Vector3(1, 1, 1);
            blendState.AlphaBlendFunction = BlendFunction.Add;
            blendState.AlphaSourceBlend = Blend.One;
            blendState.AlphaDestinationBlend = Blend.InverseSourceAlpha;
            
            float aspectRatio = (float)GraphicsDevice.Viewport.AspectRatio;

            ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(
                                           fieldOfView,
                                           aspectRatio,
                                           nearClipPlane, farClipPlane);
            ViewMatrix = Matrix.CreateLookAt(cameraPosition, cameraLookAtVector, cameraUpVector);
            WorldMatrix = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Up);

            Bones = new List<Bone>();

            
          
            base.Initialize();
        }

public void DisplayArray()
         {
            // Current object
            float TranslationX ;
            float TranslationY ;
            float TranslationZ ;
            float ScaleX ;
            float ScaleY ;
            float ScaleZ ;
            // Parent object
            float ParentRotationX ;
            float ParentRotationY ;
            float ParentRotationZ ;

            float ParentTranslationX ;
            float ParentTranslationY ;
            float ParentTranslationZ ;

            float ParentScaleX ;
            float ParentScaleY ;
            float ParentScaleZ ;
            if (Array == null || Array.Count == 0)
             {
                 return;
             }


             foreach (var obj in Array)
             {
                 TranslationX = (float)(double)obj["Translation"]["X"];
                 TranslationY = (float)(double)obj["Translation"]["Y"];
                 TranslationZ = (float)(double)obj["Translation"]["Z"];
                 ScaleX = (float)(double)obj["Scale"]["X"];
                 ScaleY = (float)(double)obj["Scale"]["Y"];
                 ScaleZ = (float)(double)obj["Scale"]["Z"];

                 string parentIndex = (string)obj["ParentIndex"];
                 JToken parent = null;
                 if (parentIndex != "edge")
                 {
                     parent = Array.FirstOrDefault(x => (string)x["Name"] == parentIndex);
                 }

                 if (parent != null)
                 {
                     ParentRotationX = (float)(double)parent["Rotation"]["X"];
                     ParentRotationY = (float)(double)parent["Rotation"]["Y"];
                     ParentRotationZ = (float)(double)parent["Rotation"]["Z"];
                     ParentTranslationX = (float)(double)parent["Translation"]["X"];
                     ParentTranslationY = (float)(double)parent["Translation"]["Y"];
                     ParentTranslationZ = (float)(double)parent["Translation"]["Z"];
                     ParentScaleX = (float)(double)parent["Scale"]["X"];
                     ParentScaleY = (float)(double)parent["Scale"]["Y"];
                     ParentScaleZ = (float)(double)parent["Scale"]["Z"];
                 }
                 else
                 {
                     ParentRotationX = 0f;
                     ParentRotationY = 0f;
                     ParentRotationZ = 0f;
                     ParentTranslationX = 0f;
                     ParentTranslationY = 0f;
                     ParentTranslationZ = 0f;
                     ParentScaleX = 1;
                     ParentScaleY = 1;
                     ParentScaleZ = 1;
                 }

                 Vector3 Translation = new Vector3(TranslationX * ScaleX, TranslationY * ScaleY, TranslationZ * ScaleZ);

                 Vector3 ParentRotation = new Vector3(ParentRotationX, ParentRotationY, ParentRotationZ);
                 Vector3 ParentTranslation = new Vector3(ParentTranslationX * ParentScaleX, ParentTranslationY * ParentScaleY, ParentTranslationZ * ParentScaleZ);

                 upperHead = Translation;
                 Matrix translationMatrix = Matrix.CreateTranslation(ParentTranslation);
                 Matrix rotationMatrix = Matrix.CreateFromYawPitchRoll(ParentRotation.Y, ParentRotation.X, ParentRotation.Z);
                 NewWorld = rotationMatrix * translationMatrix * WorldMatrix;
                 DrawStick(upperHead,Vector3.Zero , NewWorld);
             
             }
         }
 private void DrawStick(Vector3 upperHead , Vector3 ParentPos, Matrix World)
        {
            // Create a red line with a radius of 0.05f
            basicEffect = new BasicEffect(graphicsDevice);
            basicEffect.World = World;
            basicEffect.View = ViewMatrix;
            basicEffect.Projection = ProjectionMatrix;

            RasterizerState rasterizerState = new RasterizerState();
           // rasterizerState.FillMode = FillMode.WireFrame;
            rasterizerState.CullMode = CullMode.None;
            GraphicsDevice.RasterizerState = rasterizerState;
            basicEffect.Alpha = 0.7f;
            basicEffect.VertexColorEnabled = true;
            basicEffect.CurrentTechnique.Passes[0].Apply();
            VertexPositionColor[] lineVertices = new VertexPositionColor[2];
            lineVertices[0].Position = ParentPos;
            lineVertices[0].Color = Color.Gray;
            lineVertices[1].Position = upperHead;
            lineVertices[1].Color = Color.Gray;
            short[] lineIndices = new short[2];
            lineIndices[0] = 0;
            lineIndices[1] = 1;
            GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.LineList, lineVertices, 0, 2, lineIndices, 0, 1);
            Vector3 position = upperHead;
            int sphereResolution = 12;
            int numVertices = (sphereResolution + 1) * (sphereResolution + 1);
            VertexPositionColor[] sphereVertices = new VertexPositionColor[numVertices];
            int index = 0;
            float radius = 0.01f;
            for (int i = 0; i <= sphereResolution; i++)
            {
                float latitude = ((float)i / (float)sphereResolution) * MathHelper.Pi - MathHelper.Pi;
                for (int j = 0; j <= sphereResolution; j++)
                {
                    float longitude = ((float)j / (float)sphereResolution) * MathHelper.TwoPi;
                    sphereVertices[index].Position = new Vector3((float)Math.Sin(latitude) * (float)Math.Cos(longitude), (float)Math.Cos(latitude), (float)Math.Sin(latitude) * (float)Math.Sin(longitude)) * radius + position;
                    sphereVertices[index].Color = Color.White;
                    index++;
                }
            }
            int numIndices = sphereResolution * sphereResolution * 6;
            short[] sphereIndices = new short[numIndices];
            index = 0;
            for (int i = 0; i < sphereResolution; i++)
            {
                for (int j = 0; j < sphereResolution; j++)
                {
                    sphereIndices[index++] = (short)(i * (sphereResolution + 1) + j);
                    sphereIndices[index++] = (short)((i + 1) * (sphereResolution + 1) + j);
                    sphereIndices[index++] = (short)(i * (sphereResolution + 1) + j + 1);
                    sphereIndices[index++] = (short)((i + 1) * (sphereResolution + 1) + j);
                    sphereIndices[index++] = (short)((i + 1) * (sphereResolution + 1) + j + 1);
                    sphereIndices[index++] = (short)(i * (sphereResolution + 1) + j + 1);
                }
            }
            GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, sphereVertices, 0, numVertices, sphereIndices, 0, numIndices / 3);
        }

the DisplayArray(); uses DrawStick(); to draw a line and a sphere for every object
there is also

 private void DrawAxes(GraphicsDevice graphicsDevice, Matrix world, Matrix view, Matrix projection)
        {
           

            BasicEffect basicEffect = new BasicEffect(graphicsDevice);
            basicEffect.World = world;
            basicEffect.View = view;
            basicEffect.Projection = projection;
            basicEffect.VertexColorEnabled = true;

            graphicsDevice.BlendState = blendState;

           

            VertexPositionColor[] vertices = new VertexPositionColor[6];

            vertices[0].Position = Vector3.Zero;
            vertices[0].Color = Color.Red;
            vertices[1].Position = xAxis;
            vertices[1].Color = Color.Red;

            vertices[2].Position = Vector3.Zero;
            vertices[2].Color = Color.Green;
            vertices[3].Position = yAxis;
            vertices[3].Color = Color.Green;

            vertices[4].Position = Vector3.Zero;
            vertices[4].Color = Color.Blue;
            vertices[5].Position = zAxis;
            vertices[5].Color = Color.Blue;
            if (isXZGridEnabled)
            {
                VertexPositionColor[] verticesG = new VertexPositionColor[(gridSize + gridSize) * 8];

                int index = 0;

                for (int i = -gridSize; i <= gridSize; i++)
                {
                    graphicsDevice.BlendState = BlendState.AlphaBlend;
                    if (i != 0)
                    {
                        verticesG[index++].Position = new Vector3(i, 0, -gridSize);
                        verticesG[index - 1].Color = lowOpacityWhite;
                        verticesG[index++].Position = new Vector3(i, 0, gridSize);
                        verticesG[index - 1].Color = lowOpacityWhite;
                    }
                    if (i != 0)
                    {
                        verticesG[index++].Position = new Vector3(-gridSize, 0, i);
                        verticesG[index - 1].Color = lowOpacityWhite;
                        verticesG[index++].Position = new Vector3(gridSize, 0, i);
                        verticesG[index - 1].Color = lowOpacityWhite;
                    }
                    graphicsDevice.BlendState = BlendState.Opaque;
                }
                foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
                {
                    pass.Apply();
                    graphicsDevice.DrawUserPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.LineList, verticesG, 0, (gridSize + gridSize) * 4);
                  //  graphicsDevice.DrawUserPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.LineList, vertices, 0, 3);
                }
            }




            foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
              // graphicsDevice.DrawUserPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.LineList, verticesG, 0, (gridSize + gridSize) * 4);
                graphicsDevice.DrawUserPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.LineList, vertices, 0, 3);
            }
        }

to draw XYZ axis and a grid
so the drew methed

   protected override void Draw()
        {

           base.Draw();


            GraphicsDevice.Clear(MBlue);
            Editor.spriteBatch.Begin();

          
            DrawAxes(GraphicsDevice, WorldMatrix, ViewMatrix, ProjectionMatrix);

           DisplayArray();
          
              GraphicsDevice.SetVertexBuffer(vertexBuffer);
            GraphicsDevice.Indices = indexBuffer;

        


            foreach (EffectPass pass in basicEffect.CurrentTechnique.
                      Passes)
              {
                  pass.Apply();

                  if (indices != null)
                  {
                      GraphicsDevice.DrawIndexedPrimitives(Microsoft.Xna.Framework.Graphics.PrimitiveType.TriangleList, 0, 0, vertices.Length, 0, indices.Length / 3);
                  }

              }
            /*  */


            Editor.spriteBatch.End();

        }

about Useing GetDeviceRemovedReason i am a begginer so i am really confused on how to use it
thanks for the reply .

You are creating a new BasicEffect on each Draw/Frame and the GPU is running out of memory.

2 Likes