Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Out-of-memory Exception

Out-of-memory Exception

Scheduled Pinned Locked Moved C#
data-structuresregexperformancehelpquestion
13 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y yonidebest

    Hi, I get the above exception while using my code and I really do not know why. The code creates a jagged string array which includes two arrays. Each array holds 20 string items. (In the following code I removed some of it as to not overload this page...). I then use a for statement (it made en exception using foreach as well), inwhich I create a new Regex variable and use it to replace text. The code is:

    string[][] newInfo = new string[2][];
    newInfo[0] = new string[20] {"string 1", "string 2", "string 3", ... }; // 20 of them
    newInfo[1] = new string[20] {"string 1", "string 2", "string 3", ... }; // 20 of them
    for (int i = 0; i < newInfo[0].Length; i++) {
    string parameter = newInfo[0][i];
    string value = newInfo[1][Array.IndexOf(newInfo[0], parameter)];
    if (value != "-")
    {
    Regex rxParameter = new Regex("\x7C" + parameter + "=" + ".*" + "\x7C"); // \x7C is a pipe (|)
    p.text = rxParameter.Replace(p.text, "\x7C" + parameter + "=" + value + "\x7C");
    }
    }

    After compiling and running this, the program get stuck at i=3, at the replace function. I dont know why the program is out of memory. Does anyone know what I should do? Thanks, Yoni

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #3

    How is declared p? :confused:

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

    In testa che avete, signor di Ceprano?

    Y 1 Reply Last reply
    0
    • S S Senthil Kumar

      Did you look at the call stack for the exception? Did it have any InnerException set? Is the OutOfMemoryException reproducible always at i = 3?

      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

      Y Offline
      Y Offline
      yonidebest
      wrote on last edited by
      #4

      I am new to C#. I use a text writer called EditPlus to edit the code; and MSDOS to compile it and make an exe out of it. All I get is the exception. I do not know what the "call stack for the exception" or else enything else. I do know that the OutOfMemoryException apears every time I run the program, and every time at i=3. I can also add that i=0 and i=1 goes through quick, less than half a second; i=2 has a small delay, of like half a second, and then i=3 just stalls. I have the free Visual C++ 2005 Express but I have no idea how to use it (i.e. how to load my .cs file). I know this sound hopeless, but I really do not see a way out of this... Thanks, Yoni

      A S 2 Replies Last reply
      0
      • CPalliniC CPallini

        How is declared p? :confused:

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        Y Offline
        Y Offline
        yonidebest
        wrote on last edited by
        #5

        I am using the DotNetWikiBot framework[^]. p is of type Page. I dont think this is the problem, since p.text is of type String. Yoni

        1 Reply Last reply
        0
        • Y yonidebest

          Hi, I get the above exception while using my code and I really do not know why. The code creates a jagged string array which includes two arrays. Each array holds 20 string items. (In the following code I removed some of it as to not overload this page...). I then use a for statement (it made en exception using foreach as well), inwhich I create a new Regex variable and use it to replace text. The code is:

          string[][] newInfo = new string[2][];
          newInfo[0] = new string[20] {"string 1", "string 2", "string 3", ... }; // 20 of them
          newInfo[1] = new string[20] {"string 1", "string 2", "string 3", ... }; // 20 of them
          for (int i = 0; i < newInfo[0].Length; i++) {
          string parameter = newInfo[0][i];
          string value = newInfo[1][Array.IndexOf(newInfo[0], parameter)];
          if (value != "-")
          {
          Regex rxParameter = new Regex("\x7C" + parameter + "=" + ".*" + "\x7C"); // \x7C is a pipe (|)
          p.text = rxParameter.Replace(p.text, "\x7C" + parameter + "=" + value + "\x7C");
          }
          }

          After compiling and running this, the program get stuck at i=3, at the replace function. I dont know why the program is out of memory. Does anyone know what I should do? Thanks, Yoni

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #6

          Hi, when running your code, the text will explode (increase exponentially in the for loop), until the out-of-memory situation occurs. Just print the length (or the content) after each iteration. Check your regular expression, vertical bar (or pipe) is a special character that means OR, but its left operand is empty??? :)

          Luc Pattyn

          Y 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, when running your code, the text will explode (increase exponentially in the for loop), until the out-of-memory situation occurs. Just print the length (or the content) after each iteration. Check your regular expression, vertical bar (or pipe) is a special character that means OR, but its left operand is empty??? :)

            Luc Pattyn

            Y Offline
            Y Offline
            yonidebest
            wrote on last edited by
            #7

            Hi, Thanks for your help. What do you mean "Just print the length or the content"? lenght of content of what? p.text? Regarding the regular expression, I have pipes because the text has pipes. i.e. that's what I want to replace. When I tried to escape them using "\", the compiler gave me an error - unrecognized escape character. I then tried using "@" but it jammed with an Out-of-memory Exception. I then moved on to a Hex escape, which also gives me this exepction. Yoni

            L 1 Reply Last reply
            0
            • Y yonidebest

              I am new to C#. I use a text writer called EditPlus to edit the code; and MSDOS to compile it and make an exe out of it. All I get is the exception. I do not know what the "call stack for the exception" or else enything else. I do know that the OutOfMemoryException apears every time I run the program, and every time at i=3. I can also add that i=0 and i=1 goes through quick, less than half a second; i=2 has a small delay, of like half a second, and then i=3 just stalls. I have the free Visual C++ 2005 Express but I have no idea how to use it (i.e. how to load my .cs file). I know this sound hopeless, but I really do not see a way out of this... Thanks, Yoni

              A Offline
              A Offline
              Argus2
              wrote on last edited by
              #8

              Express edition is really easy to use. You may have to create a new "empty project", then copy and past in your c# program. That should give you all the debugging info so you can see the stack there are several vedio files for getting started with the express edition. Check microsoft web page. ;)

              Y 1 Reply Last reply
              0
              • Y yonidebest

                Hi, Thanks for your help. What do you mean "Just print the length or the content"? lenght of content of what? p.text? Regarding the regular expression, I have pipes because the text has pipes. i.e. that's what I want to replace. When I tried to escape them using "\", the compiler gave me an error - unrecognized escape character. I then tried using "@" but it jammed with an Out-of-memory Exception. I then moved on to a Hex escape, which also gives me this exepction. Yoni

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #9

                Hi Yoni,

                to see what goes wrong, look at the intermediate results, so print at least p.text.Lenght,
                and if necessary (but this may get very large) p.text itself, or part of it, e.g.
                p.text.Substring(0,80)
                You may want to create a function that does this in an intelligent way, i.e.
                printing length, and full or partial contentdepending on size.

                regular expressions use special characters such as \ and | and some more.
                to get them literally, you should escape them so to get a | you need a \|
                But then the C/C++/C# languages will try to interpret the backslash, so you must
                either escape it again (hence \\|) or prefix the entire constant string by a @ sign
                (@"...\|...")

                I dont think there is a need to use 0x7C when what you want is a vertical bar;
                just type | and you will get more readable code.

                general advice: if something goes wrong, break it down into smaller pieces, until]
                you can easily see where it starts to go wrong. In this particular case:
                print intermediate values, reduce the size of the problem (fewer than 20 strings!);
                if this does not help, perform some experiments (e.g. on the behaviour of Regex).

                Greetings,

                :cool:

                Luc Pattyn

                Y 1 Reply Last reply
                0
                • Y yonidebest

                  I am new to C#. I use a text writer called EditPlus to edit the code; and MSDOS to compile it and make an exe out of it. All I get is the exception. I do not know what the "call stack for the exception" or else enything else. I do know that the OutOfMemoryException apears every time I run the program, and every time at i=3. I can also add that i=0 and i=1 goes through quick, less than half a second; i=2 has a small delay, of like half a second, and then i=3 just stalls. I have the free Visual C++ 2005 Express but I have no idea how to use it (i.e. how to load my .cs file). I know this sound hopeless, but I really do not see a way out of this... Thanks, Yoni

                  S Offline
                  S Offline
                  S Senthil Kumar
                  wrote on last edited by
                  #10

                  Is your application a console app or a windows app? If it's a console app, after the OutOfMemoryException occurs, you should be able to see the stack trace on the console window. The stack trace simply lists the sequence of function calls that triggered the exception. Even otherwise, you can wrap the code that you think is causing this exception with a try catch block like this

                  try
                  {
                  // Exception causing code
                  }
                  catch(Exception e)
                  {
                  Console.WriteLine(e); // Or if it's a Windows app, use MessageBox.Show(e.ToString());
                  }

                  VC# Express is a pretty good IDE, you must learn how to use it. You get a very good debugger, at the very least.

                  Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                  Y 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi Yoni,

                    to see what goes wrong, look at the intermediate results, so print at least p.text.Lenght,
                    and if necessary (but this may get very large) p.text itself, or part of it, e.g.
                    p.text.Substring(0,80)
                    You may want to create a function that does this in an intelligent way, i.e.
                    printing length, and full or partial contentdepending on size.

                    regular expressions use special characters such as \ and | and some more.
                    to get them literally, you should escape them so to get a | you need a \|
                    But then the C/C++/C# languages will try to interpret the backslash, so you must
                    either escape it again (hence \\|) or prefix the entire constant string by a @ sign
                    (@"...\|...")

                    I dont think there is a need to use 0x7C when what you want is a vertical bar;
                    just type | and you will get more readable code.

                    general advice: if something goes wrong, break it down into smaller pieces, until]
                    you can easily see where it starts to go wrong. In this particular case:
                    print intermediate values, reduce the size of the problem (fewer than 20 strings!);
                    if this does not help, perform some experiments (e.g. on the behaviour of Regex).

                    Greetings,

                    :cool:

                    Luc Pattyn

                    Y Offline
                    Y Offline
                    yonidebest
                    wrote on last edited by
                    #11

                    Thanks for your help. It turns out the regex was a problem. Indeed - the "\\|" in the regex worked. And regarding 3 you were also correct, just a | did the job as well and made the code more readable. Yoni

                    1 Reply Last reply
                    0
                    • S S Senthil Kumar

                      Is your application a console app or a windows app? If it's a console app, after the OutOfMemoryException occurs, you should be able to see the stack trace on the console window. The stack trace simply lists the sequence of function calls that triggered the exception. Even otherwise, you can wrap the code that you think is causing this exception with a try catch block like this

                      try
                      {
                      // Exception causing code
                      }
                      catch(Exception e)
                      {
                      Console.WriteLine(e); // Or if it's a Windows app, use MessageBox.Show(e.ToString());
                      }

                      VC# Express is a pretty good IDE, you must learn how to use it. You get a very good debugger, at the very least.

                      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

                      Y Offline
                      Y Offline
                      yonidebest
                      wrote on last edited by
                      #12

                      Thanks, I am aware of the try-catch statement, tho it didnt help me so much. Anyway, the problem with the regex has been fixed. The VC# Express is actually a VC++ Express, but I guess C# will work there as well. Thanks for your help, Yoni

                      1 Reply Last reply
                      0
                      • A Argus2

                        Express edition is really easy to use. You may have to create a new "empty project", then copy and past in your c# program. That should give you all the debugging info so you can see the stack there are several vedio files for getting started with the express edition. Check microsoft web page. ;)

                        Y Offline
                        Y Offline
                        yonidebest
                        wrote on last edited by
                        #13

                        Thanks for the tip, I'll check out Microsoft's site.. Yoni

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups