In the project I made was a memory leak. I rewritten all the functions to fix some, but there was still one left:
The program has an object array of panels which grows everytime I put a new one in. When it reaches 400 panels, it deletes the oldest to free some memory.
What I don't understand is the following:
tempPanels = new Panel[panels.Length];
Array.Copy(panels, 1, tempPanels, 0, panels.Length - 1);//delete the oldest history log (the first of the array)
panels = null; //empty object array
panels = new Panel[tempPanels.Length + 1]; //set new length
tempPanels.CopyTo(panels, 0);//restore panels
When I use the code above the memory usage still keeps increasing... Can someone please explain why I have to dispose the panel first before I set panels to null?
tempPanels = new Panel[panels.Length];
Array.Copy(panels, 1, tempPanels, 0, panels.Length - 1);//delete the oldest history log (the first of the array)
panels[0].Dispose();
panels = null; //empty object array
panels = new Panel[tempPanels.Length + 1]; //set new length
tempPanels.CopyTo(panels, 0);//restore panels
Thanks in advance!
EDIT @ Steve B:
The program makes a new panel: panel currentPanel;
When there is a new panel I declare currentPanel: currentPanel = new Panel();
After that I call this function: setCurrentPanelConfiguration:
public void setCurrentPanel()
{
currentPanel.Name = "panel" + panels.Length;
currentPanel.Size = new System.Drawing.Size(485, 75);
currentPanel.BackColor = Color.Transparent;
}
To fix a scroll bug I use a Panel HistoryPanel where I put the currentPanel:
HistoryPanel.Controls.Add(currentPanel);
Then I add all the controls: the username, current time and avatar.
To save the panel I add it to the array panels after I created a space as shown above:
panels[panels.Length-1] = currentPanel;
I use an array because the history shows the newest one on top. To do that every time I have to shift all panels 80px down.
GC.GetTotalMemory(true)panelsa collection (i.e.List<>) it would fit nicely in your code:panels.RemoveAt(0);and you're done.