How to Reduce Memory & CPU Usage in GDI+ , C#

saravanan_comp2001
saravanan_comp2001 used Ask the Experts™
on
Hi,

I am using c# and GDI+ fro drawing images and Creating Graphs. it's working fine but, the problem is Memory and CPU Usage. it's taking so much memory and CPU usage around 30% - 40 % . is there any way to reduce the memory Usage?

For example: just i am put one panel in the form. i am using panel to draw the images. Panel size is 600x600.  i am drawing concentric circle in that panel. for drawing 10 circles it's taking 18 - 20MB Memory. i want to reduce the Memory into 6 - 7 MB.
// Creating Objects
graphics g = panel1.CreateGraphics();
g.Clear(Color.White); //Clear the Panel
SolidBrush sb = new SolidBrush(Color.Black); //Drawing Color
Pen p=new Pen(sb,2f); //Pen drawing with 2f size
int Top,Bottom;
int radius = 32; 
//Drawing circle
for (int i = 0; i < 10; i++)
{
Top = (300 - (i * radius)) ;
Bottom = (i * radius*2);
g.DrawEllipse(p, new Rectangle(new Point(Top, Top), new Size(Bottom, Bottom)));
}
 
// Dispose Objects.
p.Dispose();
g.Dispose();

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The only thing I can figure is to use "Using" blocks go manage object scoping.  This would guarantee that all of the objects are being properly disposed (Also I added scoping for the SolidBrush object since it also implements the IDisposable class):

// Creating Objects
using (graphics g = panel1.CreateGraphics()) 
{
	g.Clear(Color.White); //Clear the Panel
	using (SolidBrush sb = new SolidBrush(Color.Black) //Drawing Color
    {        
		using (Pen p=new Pen(sb,2f) //Pen drawing with 2f size
        {        
			int Top,Bottom;
			int radius = 32; 
			//Drawing circle
			for (int i = 0; i < 10; i++)
			{
				Top = (300 - (i * radius)) ;
				Bottom = (i * radius*2);
				g.DrawEllipse(p, new Rectangle(new Point(Top, Top), new Size(Bottom, Bottom)));
			}
        }     
    }
}

Open in new window

Author

Commented:
hi, thanks for reply. i tried your code. it's disposing all the objects, but still memory usage is there!. any idea to reduce thememory load?
Commented:
I think the main region where memory is being consumed is the CreateGraphics().

You should not use CreateGraphics(). Instead you should handle the PaintEvent of the panel and use the PaintEventArgs.Graphics.
This way you need NOT dispose the Graphics object too.
Check this link for more details
http://www.bobpowell.net/creategraphics.htm

Just add this to your code instead of the code that you have given
protected void OnPaint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
    g.Clear(Color.White); //Clear the Panel
        using (SolidBrush sb = new SolidBrush(Color.Black) //Drawing Color
    {        
                using (Pen p=new Pen(sb,2f) //Pen drawing with 2f size
        {        
                        int Top,Bottom;
                        int radius = 32; 
                        //Drawing circle
                        for (int i = 0; i < 10; i++)
                        {
                                Top = (300 - (i * radius)) ;
                                Bottom = (i * radius*2);
                                g.DrawEllipse(p, new Rectangle(new Point(Top, Top), new Size(Bottom, Bottom)));
                        }
        }     
    }
 
}

Open in new window

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Commented:
You don't say where your original code you posted was located.

If that was in some loop, then you're creating many many more than just 10 circles...

You probably didn't know it because you can't see them.  They get drawn on top of each other.

You only want to draw the circles once.  Let the underlying panel handle the repaint/redraw.

CuteBug is right; handle all this in the OnPaint() and make sure that you're not in a hard loop to handle the drawing of your objects.


>> for drawing 10 circles it's taking 18 - 20MB Memory

How did you verify that?

Author

Commented:
it solved the problem but not that much efficient....

Author

Commented:
hi ikework

i checked in the task manager.

i am using windows XP SP3 and visual studio 2005.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial