VB 2013 Application Eating Memory!
Hi All,
So I started coding VB last week. I posted here a couple of times, but went away, did some homework and built what I wanted to build... Still with some help along the way.
Anyway, in pursuit of improvements I wanted to add a progress bar. But it struggled to update. I then learnt that I should use a background worker to perform the actual operation and leave the UI thread to update the progress bar. I did this, but my app seriously degraded. During one load I got a runtime out of memory error.
I then learnt that I had poor implementation of the background worker as I was trying to cross-thread and pull information from the controls on the form. I'm working to resolve this, but as a test I removed the background worker but the app still performed in the same manner. At a loss I started with a new form and planned to add all code back in slowly to see where the issue was. Anyway I fell at the first hurdle with a form load sub that restored the controls states from those on closing. Below is the code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' FORM 1.
' Initialise specialised string collections.
If My.Settings.StartNameListBoxItems1 Is Nothing Then
My.Settings.StartNameListBoxItems1 = _
New System.Collections.Specialized.StringCollection
End If
If My.Settings.StartNameListBoxSelectedItems1 Is Nothing Then
My.Settings.StartNameListBoxSelectedItems1 = _
New System.Collections.Specialized.StringCollection
End If
' TAB PAGE 1.
' Restore controls from saved settings.
StartPathTextBox1.Text() = My.Settings.StartPathTextBox1
ZipPathTextBox1.Text() = My.Settings.ZipPathTextBox1
CopyPathTextBox1.Text() = My.Settings.CopyPathTextBox1
ZipSelectCheckBox1.Checked = My.Settings.ZipSelectCheckBox1
CopySelectCheckBox1.Checked = My.Settings.CopySelectCheckBox1
For Each s As String In My.Settings.StartNameListBoxItems1()
StartNameListBox1.Items.Add(s)
Next
For Each s As String In My.Settings.StartNameListBoxSelectedItems1()
StartNameListBox1.SelectedItems.Add(s)
Next
' Decide controls initial states.
If StartNameListBox1.SelectedItems.Count = 0 Then
ZipSelectCheckBox1.Enabled = False
RunButton1.Enabled = False
End If
If ZipSelectCheckBox1.Checked = False Then
ZipPathTextBox1.Enabled = False
ZipBrowseButton1.Enabled = False
End If
If ZipPathTextBox1.Text = String.Empty Then
CopySelectCheckBox1.Enabled = False
End If
If CopySelectCheckBox1.Checked = False Then
CopyPathTextBox1.Enabled = False
CopyBrowseButton1.Enabled = False
End If
End Sub
Can anyone see an issue? Thing is it was working before introducing the background worker, but now with the background worker removed it doesn't work. I'm at a loss. I'll fix the issue with cross threading by packing all the controls together in a single object to feed into the background worker (If anyone has some examples I would be grateful, but I have an idead so stick with the main issue) but I have no idea why my app no longer works. Can anyone pick up a connection here or is there any settings I should know about when saving variables in the apps settings. Thanks for reading!
UPDATE: I should add that I removed all the saves to settings for persistence and the app works. If I add back in then I get a problem. I still can't fathom the problem as a previous app worked with this save to my settings.
Re: VB 2013 Application Eating Memory!
In what way is it not working? Is there an exception or something else?
Re: VB 2013 Application Eating Memory!
Well in the first instance it struggled to load. Then on closing, it again takes some time. If I start Task Manager and look at Processes, you can see the memory useage just climb and climb. One time when I was in debug, it didn't atart up at all and eventually got a "out of memory" error. I should have noted any error coeds at that point, but I've not experienced that again.
So, to recap. At this point I'm not using a background worker. I've taken out all the instances were I save to my settings. The program works. But if I add the saves back in for persistence than when I close the app the memory useage shoots up and eventually the process ends.
Re: VB 2013 Application Eating Memory!
Nothing in the code you showed should take even a millisecond, so the problem isn't directly in the code you showed. You might test this in a couple ways. The simplest thing to do is to put a messagebox saying pretty nearly anything as the first line in that method. That will tell you whether the slowdown is occuring before or after that code. You can also use a Stopwatch to time that code by starting the stopwatch before the code, then stopping it after the code and looking at the elapsedMilliseconds. If it turns out that the code you have shown is where the slowdown is happening, then it is most likely some kind of side effect, such as altering a control could be triggering some events on the control.
Re: VB 2013 Application Eating Memory!
Thanks for the advice. Along similar lines. I noticed that if I removed the loading from settings, than the conditions for the buttons are performed. If I add in the initialise for the specialised string conditions, then the conditions aren't performed. There's something up with the loading for settings, but it worked in another form. Now the past two forms its not. I'm stumped. When loading from application settings are there any settings that I should look out for?
Re: VB 2013 Application Eating Memory!
I even received this message when just implementing saving the settings during my form close.
System.Configuration.ConfigurationErrorsException: Failed to save settings: An error occurred executing the configuration section handler for userSettings/Backup_Tool.My.MySettings. ---> System.Configuration.ConfigurationErrorsException: An error occurred executing the configuration section handler for userSettings/Backup_Tool.My.MySettings. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown
Re: VB 2013 Application Eating Memory!
Resolved. I wasn't clearing out old memory during the form closed. Just adding, adding etc.... Thanks for all replies!