Why is the amount of used memory increasing?
c#, memory
Solution
You never disposed your `Graphics` object.
Use `using`, like:
using (Graphics gr = Graphics.FromImage(image)) {
...
}
pcImageBox.Image = image;
Also, don't create a brand new `Bitmap` every frame. Save the old one in a member variable, and draw over it during the next call. Only make a new one if the screen size changes (`Screen.PrimaryScreen.Bounds != image.Size`).
Problem
``` private void Draw(){ int width = Screen.PrimaryScreen.Bounds.Width; int height = Screen.PrimaryScreen.Bounds.Height; Bitmap image= new Bitmap(width, height); Graphics gr = Graphics.FromImage(image); gr.CopyFromScreen(0, 0, 0, 0, new Size(width, height)); Random rnd = new Random(); gr.DrawEllipse(new Pen(Color.Red, rnd.Next(100)), rnd.Next(300), rnd.Next(100), rnd.Next(600), rnd.Next(1000)); Point[] p = new Point[3]; p[0] = new Point(rnd.Next(30), rnd.Next(60)); p[1] = new Point(rnd.Next(100), rnd.Next(260)); p[2] = new Point(rnd.Next(30), rnd.Next(10)); gr.DrawPolygon(Pens.AliceBlue, p); gr.DrawBeziers(Pens.Yellow, p); pcImageBox.Image = image; } ``` When I use `Timer` and call the `Draw()` method every 300 milliseconds everything works fine, but when I look into process manager, my project uses more memory (memory increases every 300 milliseconds) Maybe I should use garbage collector or use `p = null;` etc. How can I resolve this problem? Thank you for help and sorry for my bad english..