|
-
Jun 7th, 2006, 02:04 PM
#1
Thread Starter
Frenzied Member
why is this simple function slowing down, then crashing as time goes by
Here is the code:
Code:
private void button1_Click(object sender, System.EventArgs e)
{
string s="aaaaaaaaaaT";
for(int i=0;i<1000;i++)
{
func(ref s);
}
label1.Text = s;
}
private void func(ref string s)
{
s = s + s;
}
My machine has about 710 MB left in the harddrive. When this program runs and "i" variable is at around 20, the functions slows down and eventually stops/crashes. I look at the taskmanager and it shows that the memory usage is at maximum. Is there a way around this?
-
Jun 7th, 2006, 02:08 PM
#2
Hyperactive Member
Re: why is this simple function slowing down, then crashing as time goes by
When something is slowing down and eventually crash it's usually a sign on a looping structure that does not end. However your code does end. The only thing I could think about is either the string is concatenating to a very large string that is sucking up your cpu or the label has a limit on the amount of characters it could hold.
What is the error message you got is there is any?
-
Jun 7th, 2006, 05:50 PM
#3
Re: why is this simple function slowing down, then crashing as time goes by
Your code first creates a String object that is 11 characters long. It then concatenates that String with itself to create a second string that is 22 characters long. It then creates a third String that is 44 characters long. By the time your code is finished it would have created 1000 String objects with each being twice as long as the one before it with the last being 11 * 2 ^1000 characters long. That's approximately 1.18 * 10 ^ 302 characters in the last String alone. With all the others as well there's almost twice that many. Gee, I can't see a problem with that.
-
Jun 8th, 2006, 02:24 AM
#4
Re: why is this simple function slowing down, then crashing as time goes by
 Originally Posted by jmcilhinney
Your code first creates a String object that is 11 characters long. It then concatenates that String with itself to create a second string that is 22 characters long. It then creates a third String that is 44 characters long. By the time your code is finished it would have created 1000 String objects with each being twice as long as the one before it with the last being 11 * 2 ^1000 characters long. That's approximately 1.18 * 10 ^ 302 characters in the last String alone. With all the others as well there's almost twice that many. Gee, I can't see a problem with that. 

That's right, what he means "YOUR CODE IS EATING YOUR MEMORY" not your disk space
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 8th, 2006, 06:34 AM
#5
Re: why is this simple function slowing down, then crashing as time goes by
This is the most depressing thing I have ever seen.
I don't live here any more.
-
Jun 9th, 2006, 10:31 AM
#6
Thread Starter
Frenzied Member
Re: why is this simple function slowing down, then crashing as time goes by
I had a feeling it had something to do with memory. Thanks.
-
Jun 9th, 2006, 09:31 PM
#7
Re: why is this simple function slowing down, then crashing as time goes by
Don't forget to resolve your thread from the Thread Tools menu.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|