Results 1 to 7 of 7

Thread: why is this simple function slowing down, then crashing as time goes by

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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?

  2. #2
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    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?

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: why is this simple function slowing down, then crashing as time goes by

    Quote 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

  5. #5
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width