Bonjour,

J'ai construit une classe heritée de Panel pour dessiner des courbes (un peu à la maniere d'Excel, Matlab etc). Mon probleme c'est la quantite de memoire que consomme mon application, en particulier lors des changements de taille de la fenetre. A chaque passage d'une fenetre reduite (environ 1/4 de l'ecran) à la fenetre en plein ecran, mon application consomme 6 Mo de RAM en plus. Et si je continue a alterner entre les tailles il n'est pas rare que j'arrive à près d'1 Go de RAM pour une fenetre n'affichant que 9 panels...
Biensur, a partir d'un moment le framework (je sais pas si c'est comme ca que ca s'appelle) fait le menage et la memoire utilisée par mon application retourne dans la normale, mais j'aimerais pouvoir optimiser mon code si possible.

Voici un bout de mon code:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
 
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        if (objBitmap != null)
        {
            Graphics g = this.CreateGraphics();
            g.DrawImage(objBitmap, 0, 0, this.Width, this.Height);
        }
    }
 
    protected override void OnSizeChanged(EventArgs e)
    {
        base.OnSizeChanged(e);
        DrawGraphImage();
    }
 
 
    private void DrawGraphImage()
    {
        int i;
        objBitmap = new Bitmap(this.Width, this.Height);
        g = Graphics.FromImage(objBitmap);
 
        //White background
        g.FillRectangle(new SolidBrush(Color.White), 0, 0, this.Width, this.Height);
 
        if (wafeForms.Count != 0)
        {
            //Axes
            g.DrawLine(axePen, this.marginLeft, this.Height - yMargin, this.Width - this.marginRight, this.Height - yMargin);
            g.DrawLine(axePen, this.marginLeft, this.yMargin, this.marginLeft, this.Height - yMargin);
 
            double[,] markYArray = GetYMarkers();
            double[,] markXArray = GetXMarkers();
 
            for (i = 0; i < markYArray.GetUpperBound(0) + 1; i++)
            {
                g.DrawLine(axePen, this.marginLeft - 5, this.Height - this.yMargin - (int)Math.Round(markYArray[i, 0]), this.marginLeft, this.Height - this.yMargin - (int)Math.Round(markYArray[i, 0]));
                g.DrawString(markYArray[i, 1].ToString(), Font, new SolidBrush(Color.Black), this.marginLeft - 30, this.Height - this.yMargin - (int)Math.Round(markYArray[i, 0]) - 5);
            }
 
            for (i = 0; i < markXArray.GetUpperBound(0) + 1; i++)
            {
                g.DrawLine(axePen, this.marginLeft + (int)Math.Round(markXArray[i, 0]), this.Height - this.yMargin, this.marginLeft + (int)Math.Round(markXArray[i, 0]), this.Height - this.yMargin + 5);
                g.DrawString(markXArray[i, 1].ToString(), Font, new SolidBrush(Color.Black), this.marginLeft + (int)Math.Round(markXArray[i, 0]), this.Height - this.yMargin + 10);
            }
 
            //Draw waveforms
            foreach (WaveForm wf in wafeForms)
            {
                double xS = GetXScale(wf.length);
                double yS = GetYScale();
 
                int modx = 1;
                for (i = 0; i < wf.length - modx; i += modx)
                {
                    g.DrawLine(wf.pen, (int)Math.Round((wf.xScale[i] - minX) * xS) + this.marginLeft, this.Height - (int)Math.Round((wf.data[i] - minY) * yS) - this.yMargin, (int)Math.Round((wf.xScale[i + modx] - minX) * xS) + this.marginLeft, this.Height - (int)Math.Round((wf.data[i + modx] - minY) * yS) - this.yMargin);
                }
            }
        }
 
        this.Refresh();
    }
Je me demande si ca ne vient pas de la ligne suivante dans mon override de OnPaint:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
 
g.DrawImage(objBitmap, 0, 0, this.Width, this.Height);
Je connais pas bien tous les meandres du C# mais je pense que je continue de superposer des images et qu'elles restent en memoire jusqu'a ce que la machine virtuelle se rende compte que les precedentes ne servent plus à rien et les efface.

Des idées pour eviter de monter si haut en consommation de memoire?

Merci.