Stack Size

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ben R. Bolton

    Stack Size

    The documentation indicates that the threads "default stack size" is 1MB.
    The work "default" implies that it can be changed.

    Is it possible to change the StackSize in .NET? If so how?

    Is it possible to determine the amount of memory used in the current stack?

    Any assistance would be appreciated.

    Ben


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Stack Size

    Ben,

    Unfortunately no. The documentation you were reading was for the
    creation of new threads in the system, which is handled by the Thread class.
    The CreateThread API function allows you to set the stack size and you can
    call it from .NET. However, I don't know if that is a good idea, since I am
    not sure how the runtime will perceive that thread.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "Ben R. Bolton" <xbridgehd@hotm ail.com> wrote in message
    news:%23otE9bQj DHA.2504@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > The documentation indicates that the threads "default stack size" is 1MB.
    > The work "default" implies that it can be changed.
    >
    > Is it possible to change the StackSize in .NET? If so how?
    >
    > Is it possible to determine the amount of memory used in the current[/color]
    stack?[color=blue]
    >
    > Any assistance would be appreciated.
    >
    > Ben
    >
    >[/color]


    Comment

    • Ben R. Bolton

      #3
      Re: Stack Size

      Thanks Nicholas,

      I hadassumed it couldn't be changed since I could find no reference as to
      how to change it. But wanted to ask anyway.

      Is it possible to determine how much of the 1MB of memory is currently being
      used (is on the stack)?

      Ben



      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
      in message news:eOMktoQjDH A.2232@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Ben,
      >
      > Unfortunately no. The documentation you were reading was for the
      > creation of new threads in the system, which is handled by the Thread[/color]
      class.[color=blue]
      > The CreateThread API function allows you to set the stack size and you can
      > call it from .NET. However, I don't know if that is a good idea, since I[/color]
      am[color=blue]
      > not sure how the runtime will perceive that thread.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nick(dot)paldin o=at=exisconsul ting<dot>com
      >
      > "Ben R. Bolton" <xbridgehd@hotm ail.com> wrote in message
      > news:%23otE9bQj DHA.2504@TK2MSF TNGP09.phx.gbl. ..[color=green]
      > > The documentation indicates that the threads "default stack size" is[/color][/color]
      1MB.[color=blue][color=green]
      > > The work "default" implies that it can be changed.
      > >
      > > Is it possible to change the StackSize in .NET? If so how?
      > >
      > > Is it possible to determine the amount of memory used in the current[/color]
      > stack?[color=green]
      > >
      > > Any assistance would be appreciated.
      > >
      > > Ben
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Jeffrey Tan[MSFT]

        #4
        Re: Stack Size


        Hi Ben,

        It seems that there is no easy way to get the current used stack size of
        the thread.
        May be you can refer to the NtQueryInformat ionThread DDK function to get
        the information of specified thread.
        Because it is a DDK function, you must install the DDK first.

        Hope this helps,

        Best regards,
        Jeffrey Tan
        Microsoft Online Partner Support
        Get Secure! - www.microsoft.com/security
        This posting is provided "as is" with no warranties and confers no rights.

        --------------------
        | Reply-To: "Ben R. Bolton" <xbridgehd@hotm ail.com>
        | From: "Ben R. Bolton" <xbridgehd@hotm ail.com>
        | References: <#otE9bQjDHA.25 04@TK2MSFTNGP09 .phx.gbl>
        <eOMktoQjDHA.22 32@TK2MSFTNGP09 .phx.gbl>
        | Subject: Re: Stack Size
        | Date: Tue, 7 Oct 2003 12:48:13 -0700
        | Lines: 51
        | X-Priority: 3
        | X-MSMail-Priority: Normal
        | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
        | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
        | Message-ID: <#fqsyvQjDHA.32 04@tk2msftngp13 .phx.gbl>
        | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
        | NNTP-Posting-Host: 12-236-76-193.client.attb i.com 12.236.76.193
        | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!tk2 msftngp13.phx.g bl
        | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1896 51
        | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
        |
        | Thanks Nicholas,
        |
        | I hadassumed it couldn't be changed since I could find no reference as to
        | how to change it. But wanted to ask anyway.
        |
        | Is it possible to determine how much of the 1MB of memory is currently
        being
        | used (is on the stack)?
        |
        | Ben
        |
        |
        |
        | "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com>
        wrote
        | in message news:eOMktoQjDH A.2232@TK2MSFTN GP09.phx.gbl...
        | > Ben,
        | >
        | > Unfortunately no. The documentation you were reading was for the
        | > creation of new threads in the system, which is handled by the Thread
        | class.
        | > The CreateThread API function allows you to set the stack size and you
        can
        | > call it from .NET. However, I don't know if that is a good idea, since
        I
        | am
        | > not sure how the runtime will perceive that thread.
        | >
        | > Hope this helps.
        | >
        | >
        | > --
        | > - Nicholas Paldino [.NET/C# MVP]
        | > - nick(dot)paldin o=at=exisconsul ting<dot>com
        | >
        | > "Ben R. Bolton" <xbridgehd@hotm ail.com> wrote in message
        | > news:%23otE9bQj DHA.2504@TK2MSF TNGP09.phx.gbl. ..
        | > > The documentation indicates that the threads "default stack size" is
        | 1MB.
        | > > The work "default" implies that it can be changed.
        | > >
        | > > Is it possible to change the StackSize in .NET? If so how?
        | > >
        | > > Is it possible to determine the amount of memory used in the current
        | > stack?
        | > >
        | > > Any assistance would be appreciated.
        | > >
        | > > Ben
        | > >
        | > >
        | >
        | >
        |
        |
        |

        Comment

        • Mattias Sjögren

          #5
          Re: Stack Size

          Ben,
          [color=blue]
          >Is it possible to change the StackSize in .NET? If so how?[/color]

          Editbin.exe /stack

          should work, I think.



          Mattias

          --
          Mattias Sjögren [MVP] mattias @ mvps.org

          Please reply only to the newsgroup.

          Comment

          • Alan Pretre

            #6
            Re: Stack Size

            "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
            news:%23pF8ftaj DHA.2616@TK2MSF TNGP11.phx.gbl. ..[color=blue]
            > Ben,[color=green]
            > >Is it possible to change the StackSize in .NET? If so how?[/color]
            > Editbin.exe /stack
            > should work, I think.[/color]

            That works.

            Test:

            class App {
            private static long _Depth = 0;

            private static void GoDeep() {
            if ((++_Depth % 10000) == 0) System.Console. WriteLine("Dept h is " +
            _Depth.ToString ());
            GoDeep();
            return;
            }

            public static void Main() {
            try {
            GoDeep();
            } finally {
            }
            return;
            }
            }




            editbin /stack:100000,10 00 q.exe
            Depth is 10000
            Depth is 20000

            Unhandled Exception: StackOverflowEx ception.

            editbin /stack:1000000,1 000 q.exe
            Depth is 10000
            Depth is 20000
            Depth is 30000
            Depth is 40000
            Depth is 50000
            Depth is 60000
            Depth is 70000
            Depth is 80000

            Unhandled Exception: StackOverflowEx ception.


            -- Alan


            Comment

            • Jeffrey Tan[MSFT]

              #7
              Re: Stack Size


              Hi Alan,

              Yes, that should work, I have found Editbin may be useful for change the
              stack size, but I have not had time to test for it.
              Thanks for you test code.

              Best regards,
              Jeffrey Tan
              Microsoft Online Partner Support
              Get Secure! - www.microsoft.com/security
              This posting is provided "as is" with no warranties and confers no rights.

              --------------------
              | Reply-To: "Alan Pretre" <no@spam>
              | From: "Alan Pretre" <no@spam>
              | References: <#otE9bQjDHA.25 04@TK2MSFTNGP09 .phx.gbl>
              <#pF8ftajDHA.26 16@TK2MSFTNGP11 .phx.gbl>
              | Subject: Re: Stack Size
              | Date: Thu, 9 Oct 2003 11:56:23 -0500
              | Lines: 55
              | X-Priority: 3
              | X-MSMail-Priority: Normal
              | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
              | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
              | Message-ID: <#GGBHZojDHA.24 36@TK2MSFTNGP09 .phx.gbl>
              | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
              | NNTP-Posting-Host: host-13-19-220-24.midco.net 24.220.19.13
              | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP09.phx.g bl
              | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1903 07
              | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
              |
              | "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
              | news:%23pF8ftaj DHA.2616@TK2MSF TNGP11.phx.gbl. ..
              | > Ben,
              | > >Is it possible to change the StackSize in .NET? If so how?
              | > Editbin.exe /stack
              | > should work, I think.
              |
              | That works.
              |
              | Test:
              |
              | class App {
              | private static long _Depth = 0;
              |
              | private static void GoDeep() {
              | if ((++_Depth % 10000) == 0) System.Console. WriteLine("Dept h is " +
              | _Depth.ToString ());
              | GoDeep();
              | return;
              | }
              |
              | public static void Main() {
              | try {
              | GoDeep();
              | } finally {
              | }
              | return;
              | }
              | }
              |
              |
              |
              |
              | editbin /stack:100000,10 00 q.exe
              | Depth is 10000
              | Depth is 20000
              |
              | Unhandled Exception: StackOverflowEx ception.
              |
              | editbin /stack:1000000,1 000 q.exe
              | Depth is 10000
              | Depth is 20000
              | Depth is 30000
              | Depth is 40000
              | Depth is 50000
              | Depth is 60000
              | Depth is 70000
              | Depth is 80000
              |
              | Unhandled Exception: StackOverflowEx ception.
              |
              |
              | -- Alan
              |
              |
              |

              Comment

              • Ben R. Bolton

                #8
                Re: Stack Size

                Thanks Mattias,

                I had hoped there was a compiler option, but at least there is a way.

                Ben


                "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                news:%23pF8ftaj DHA.2616@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                > Ben,
                >[color=green]
                > >Is it possible to change the StackSize in .NET? If so how?[/color]
                >
                > Editbin.exe /stack
                >
                > should work, I think.
                >
                >
                >
                > Mattias
                >
                > --
                > Mattias Sjögren [MVP] mattias @ mvps.org
                > http://www.msjogren.net/dotnet/
                > Please reply only to the newsgroup.[/color]


                Comment

                • Fred Mellender

                  #9
                  Re: Stack Size

                  If the answer is not too arcane can someone explain to me why the stack size
                  had to be set at compile/load time. Why did the c# designers not grow it
                  as required in virtual memory, which (so far as I know) is done by most
                  other language implementations , and let an "out of memory" exception occur?

                  In an object oriented language where most objects are allocated on the heap,
                  perhaps it is not so important. Nevertheless, some problems are most easily
                  coded as a recursive function and, if even a few value types are used, a 1MB
                  stack does not seem like much.

                  E.G. (not too realistic, maybe, given that a double would not hold the
                  answer anyway):
                  public static double fact(double x)

                  {

                  if (x <= 0)

                  return 1;

                  return x *fact(x-1);

                  }

                  overflows when fact(35000) is called. ("An unhandled exception of type
                  'System.StackOv erflowException ' occurred in Foo.exe"). So less than 35000
                  recursive calls can occur in a function that uses type double as a
                  parameter?

                  "Ben R. Bolton" <xbridgehd@hotm ail.com> wrote in message
                  news:OG7GWB4jDH A.1884@TK2MSFTN GP09.phx.gbl...[color=blue]
                  > Thanks Mattias,
                  >
                  > I had hoped there was a compiler option, but at least there is a way.
                  >
                  > Ben
                  >
                  >
                  > "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                  > news:%23pF8ftaj DHA.2616@TK2MSF TNGP11.phx.gbl. ..[color=green]
                  > > Ben,
                  > >[color=darkred]
                  > > >Is it possible to change the StackSize in .NET? If so how?[/color]
                  > >
                  > > Editbin.exe /stack
                  > >
                  > > should work, I think.
                  > >
                  > >
                  > >
                  > > Mattias
                  > >
                  > > --
                  > > Mattias Sjögren [MVP] mattias @ mvps.org
                  > > http://www.msjogren.net/dotnet/
                  > > Please reply only to the newsgroup.[/color]
                  >
                  >[/color]


                  Comment

                  • Jeffrey Tan[MSFT]

                    #10
                    Re: Stack Size


                    Hi Fred,

                    The stack is set to an arbitrarily small value when the program is first
                    loaded. The stack then grows on demand to meet the needs of the thread.
                    This is implemented by placing a page with PAGE_GUARD access at the end of
                    the current stack. When your code causes the stack pointer to point to an
                    address on this page, an exception occurs. The system will commit your
                    desired page. The 1M was the default maximum stack size that can be commit.

                    To work around the recursive problem, you can handle the over flow
                    exception, commit more page yourself, for more details please refer to
                    "Trapping the Exception with __try and __except (Full Solution)" in the
                    link below:


                    Hope this helps,
                    Best regards,
                    Jeffrey Tan
                    Microsoft Online Partner Support
                    Get Secure! - www.microsoft.com/security
                    This posting is provided "as is" with no warranties and confers no rights.

                    --------------------
                    | From: "Fred Mellender" <nospamPlease_f redm@frontierne t.net>
                    | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
                    | References: <#otE9bQjDHA.25 04@TK2MSFTNGP09 .phx.gbl>
                    <#pF8ftajDHA.26 16@TK2MSFTNGP11 .phx.gbl>
                    <OG7GWB4jDHA.18 84@TK2MSFTNGP09 .phx.gbl>
                    | Subject: Re: Stack Size
                    | Lines: 60
                    | X-Priority: 3
                    | X-MSMail-Priority: Normal
                    | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
                    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
                    | Message-ID: <hoShb.2172$e24 .1430@news01.ro c.ny>
                    | X-Complaints-To: abuse-news@frontierne t.net
                    | X-Trace:
                    52616e646f6d495 6a7c2f1d1725bf9 c71a51cc963cf8a f61d4bf1cb8db7c e0b5f504894fa32 0
                    62d943d8f53fcba f8e94930c7856e7 b450326e8242345 776f33dd68c3bef 19dbe75a3af0e5b 3
                    07cff1b21e7500f 5b9f8df775dc4ec af6611d9ba8fc8c b03247f51d5bca1 0438b45eabb4
                    | X-Abuse-Info: Please be sure to forward ALL headers so that we may
                    process your complaint properly.
                    | NNTP-Posting-Date: Sat, 11 Oct 2003 12:07:41 GMT
                    | Date: Sat, 11 Oct 2003 12:07:41 GMT
                    | Path:
                    cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new s-out.cwix.com!ne wsfeed.cwix.co
                    m!prodigy.com!r ip!news.webusen et.com!peer01.c ox.net!cox.net! newshosting.com !
                    news-xfer2.atl.newsh osting.com!news-feed01.roc.ny.f rontiernet.net! nntp.front
                    iernet.net!news 01.roc.ny.POSTE D!not-for-mail
                    | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1907 09
                    | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
                    |
                    | If the answer is not too arcane can someone explain to me why the stack
                    size
                    | had to be set at compile/load time. Why did the c# designers not grow it
                    | as required in virtual memory, which (so far as I know) is done by most
                    | other language implementations , and let an "out of memory" exception
                    occur?
                    |
                    | In an object oriented language where most objects are allocated on the
                    heap,
                    | perhaps it is not so important. Nevertheless, some problems are most
                    easily
                    | coded as a recursive function and, if even a few value types are used, a
                    1MB
                    | stack does not seem like much.
                    |
                    | E.G. (not too realistic, maybe, given that a double would not hold the
                    | answer anyway):
                    | public static double fact(double x)
                    |
                    | {
                    |
                    | if (x <= 0)
                    |
                    | return 1;
                    |
                    | return x *fact(x-1);
                    |
                    | }
                    |
                    | overflows when fact(35000) is called. ("An unhandled exception of type
                    | 'System.StackOv erflowException ' occurred in Foo.exe"). So less than 35000
                    | recursive calls can occur in a function that uses type double as a
                    | parameter?
                    |
                    | "Ben R. Bolton" <xbridgehd@hotm ail.com> wrote in message
                    | news:OG7GWB4jDH A.1884@TK2MSFTN GP09.phx.gbl...
                    | > Thanks Mattias,
                    | >
                    | > I had hoped there was a compiler option, but at least there is a way.
                    | >
                    | > Ben
                    | >
                    | >
                    | > "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                    | > news:%23pF8ftaj DHA.2616@TK2MSF TNGP11.phx.gbl. ..
                    | > > Ben,
                    | > >
                    | > > >Is it possible to change the StackSize in .NET? If so how?
                    | > >
                    | > > Editbin.exe /stack
                    | > >
                    | > > should work, I think.
                    | > >
                    | > >
                    | > >
                    | > > Mattias
                    | > >
                    | > > --
                    | > > Mattias Sjögren [MVP] mattias @ mvps.org
                    | > > http://www.msjogren.net/dotnet/
                    | > > Please reply only to the newsgroup.
                    | >
                    | >
                    |
                    |
                    |

                    Comment

                    • Fred Mellender

                      #11
                      Re: Stack Size

                      Thanks for the information and it is good to know there is a work-around.

                      However, I see that the solution requires writing in C++ and use of the
                      "_asm" keyword. Perhaps there is no way for C# to accomplish the same thing
                      in a "managed" way? I'm sure the C# designers have plenty on their plates
                      already, but it would be nice to see the stack growth handled automatically,
                      or perhaps through a more "C# pure" exception handler.

                      But the information you provided is useful to me. I am working on a
                      Prolog-like system where recursion is often a substitute for loops.


                      "Jeffrey Tan[MSFT]" <v-jetan@online.mi crosoft.com> wrote in message
                      news:KOI7PhWkDH A.2556@cpmsftng xa06.phx.gbl...[color=blue]
                      >
                      > Hi Fred,
                      >
                      > The stack is set to an arbitrarily small value when the program is first
                      > loaded. The stack then grows on demand to meet the needs of the thread.
                      > This is implemented by placing a page with PAGE_GUARD access at the end of
                      > the current stack. When your code causes the stack pointer to point to an
                      > address on this page, an exception occurs. The system will commit your
                      > desired page. The 1M was the default maximum stack size that can be[/color]
                      commit.[color=blue]
                      >
                      > To work around the recursive problem, you can handle the over flow
                      > exception, commit more page yourself, for more details please refer to
                      > "Trapping the Exception with __try and __except (Full Solution)" in the
                      > link below:
                      > http://support.microsoft.com/default...b;en-us;315937
                      >
                      > Hope this helps,
                      > Best regards,
                      > Jeffrey Tan
                      > Microsoft Online Partner Support
                      > Get Secure! - www.microsoft.com/security
                      > This posting is provided "as is" with no warranties and confers no rights.
                      >
                      > --------------------
                      > | From: "Fred Mellender" <nospamPlease_f redm@frontierne t.net>
                      > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
                      > | References: <#otE9bQjDHA.25 04@TK2MSFTNGP09 .phx.gbl>
                      > <#pF8ftajDHA.26 16@TK2MSFTNGP11 .phx.gbl>
                      > <OG7GWB4jDHA.18 84@TK2MSFTNGP09 .phx.gbl>
                      > | Subject: Re: Stack Size
                      > | Lines: 60
                      > | X-Priority: 3
                      > | X-MSMail-Priority: Normal
                      > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
                      > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
                      > | Message-ID: <hoShb.2172$e24 .1430@news01.ro c.ny>
                      > | X-Complaints-To: abuse-news@frontierne t.net
                      > | X-Trace:
                      >[/color]
                      52616e646f6d495 6a7c2f1d1725bf9 c71a51cc963cf8a f61d4bf1cb8db7c e0b5f504894fa32 0[color=blue]
                      >[/color]
                      62d943d8f53fcba f8e94930c7856e7 b450326e8242345 776f33dd68c3bef 19dbe75a3af0e5b 3[color=blue]
                      > 07cff1b21e7500f 5b9f8df775dc4ec af6611d9ba8fc8c b03247f51d5bca1 0438b45eabb4
                      > | X-Abuse-Info: Please be sure to forward ALL headers so that we may
                      > process your complaint properly.
                      > | NNTP-Posting-Date: Sat, 11 Oct 2003 12:07:41 GMT
                      > | Date: Sat, 11 Oct 2003 12:07:41 GMT
                      > | Path:
                      >[/color]
                      cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new s-out.cwix.com!ne wsfeed.cwix.co[color=blue]
                      >[/color]
                      m!prodigy.com!r ip!news.webusen et.com!peer01.c ox.net!cox.net! newshosting.com ![color=blue]
                      >[/color]
                      news-xfer2.atl.newsh osting.com!news-feed01.roc.ny.f rontiernet.net! nntp.front[color=blue]
                      > iernet.net!news 01.roc.ny.POSTE D!not-for-mail
                      > | Xref: cpmsftngxa06.ph x.gbl[/color]
                      microsoft.publi c.dotnet.langua ges.csharp:1907 09[color=blue]
                      > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
                      > |
                      > | If the answer is not too arcane can someone explain to me why the stack
                      > size
                      > | had to be set at compile/load time. Why did the c# designers not grow[/color]
                      it[color=blue]
                      > | as required in virtual memory, which (so far as I know) is done by most
                      > | other language implementations , and let an "out of memory" exception
                      > occur?
                      > |
                      > | In an object oriented language where most objects are allocated on the
                      > heap,
                      > | perhaps it is not so important. Nevertheless, some problems are most
                      > easily
                      > | coded as a recursive function and, if even a few value types are used, a
                      > 1MB
                      > | stack does not seem like much.
                      > |
                      > | E.G. (not too realistic, maybe, given that a double would not hold the
                      > | answer anyway):
                      > | public static double fact(double x)
                      > |
                      > | {
                      > |
                      > | if (x <= 0)
                      > |
                      > | return 1;
                      > |
                      > | return x *fact(x-1);
                      > |
                      > | }
                      > |
                      > | overflows when fact(35000) is called. ("An unhandled exception of type
                      > | 'System.StackOv erflowException ' occurred in Foo.exe"). So less than[/color]
                      35000[color=blue]
                      > | recursive calls can occur in a function that uses type double as a
                      > | parameter?
                      > |
                      > | "Ben R. Bolton" <xbridgehd@hotm ail.com> wrote in message
                      > | news:OG7GWB4jDH A.1884@TK2MSFTN GP09.phx.gbl...
                      > | > Thanks Mattias,
                      > | >
                      > | > I had hoped there was a compiler option, but at least there is a way.
                      > | >
                      > | > Ben
                      > | >
                      > | >
                      > | > "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                      > | > news:%23pF8ftaj DHA.2616@TK2MSF TNGP11.phx.gbl. ..
                      > | > > Ben,
                      > | > >
                      > | > > >Is it possible to change the StackSize in .NET? If so how?
                      > | > >
                      > | > > Editbin.exe /stack
                      > | > >
                      > | > > should work, I think.
                      > | > >
                      > | > >
                      > | > >
                      > | > > Mattias
                      > | > >
                      > | > > --
                      > | > > Mattias Sjögren [MVP] mattias @ mvps.org
                      > | > > http://www.msjogren.net/dotnet/
                      > | > > Please reply only to the newsgroup.
                      > | >
                      > | >
                      > |
                      > |
                      > |
                      >[/color]


                      Comment

                      • Alan Pretre

                        #12
                        Re: Stack Size

                        "Jeffrey Tan[MSFT]" <v-jetan@online.mi crosoft.com> wrote in message
                        news:KOI7PhWkDH A.2556@cpmsftng xa06.phx.gbl...[color=blue]
                        > To work around the recursive problem, you can handle the over flow
                        > exception, commit more page yourself, for more details please refer to
                        > "Trapping the Exception with __try and __except (Full Solution)" in the
                        > link below:
                        > http://support.microsoft.com/default...b;en-us;315937[/color]


                        Is all that PAGE_GUARD stuff necessary in C#? I don't see the same behavior
                        with this example as the article shows:

                        class App {
                        private static long _Depth = 0;

                        private static void GoDeep() {
                        if ((++_Depth % 10000) == 0) System.Console. WriteLine("Dept h is " +
                        _Depth.ToString ());
                        GoDeep();
                        return;
                        }

                        public static void Main() {
                        for (int i = 0; i < 3; i++) {
                        try {
                        _Depth = 0;
                        System.Console. WriteLine();
                        GoDeep();
                        } catch (System.Excepti on Ex) {
                        System.Console. WriteLine(Ex.To String());
                        }
                        }
                        return;
                        }
                        }



                        Depth is 10000
                        Depth is 20000
                        Depth is 30000
                        Depth is 40000
                        Depth is 50000
                        Depth is 60000
                        Depth is 70000
                        Depth is 80000
                        System.StackOve rflowException: Exception of type
                        System.StackOve rflowException was thrown.

                        Depth is 10000
                        Depth is 20000
                        Depth is 30000
                        Depth is 40000
                        Depth is 50000
                        Depth is 60000
                        Depth is 70000
                        Depth is 80000
                        System.StackOve rflowException: Exception of type
                        System.StackOve rflowException was thrown.

                        Depth is 10000
                        Depth is 20000
                        Depth is 30000
                        Depth is 40000
                        Depth is 50000
                        Depth is 60000
                        Depth is 70000
                        Depth is 80000
                        System.StackOve rflowException: Exception of type
                        System.StackOve rflowException was thrown.

                        -- Alan


                        Comment

                        • Jeffrey Tan[MSFT]

                          #13
                          Re: Stack Size


                          Hi Ben,

                          Yes, it seems that the C# compiler does not have the option for change the
                          stack size.
                          But forturnately, we can change it with EditBin.exe
                          If you feel this uncomfortable, you can provide some suggestion to
                          Microsoft at this link:

                          or you can mail to mswish@microsof t.com

                          Thanks for your understanding and information.

                          Best regards,
                          Jeffrey Tan
                          Microsoft Online Partner Support
                          Get Secure! - www.microsoft.com/security
                          This posting is provided "as is" with no warranties and confers no rights.

                          --------------------
                          | Reply-To: "Ben R. Bolton" <xbridgehd@hotm ail.com>
                          | From: "Ben R. Bolton" <xbridgehd@hotm ail.com>
                          | References: <#otE9bQjDHA.25 04@TK2MSFTNGP09 .phx.gbl>
                          <#pF8ftajDHA.26 16@TK2MSFTNGP11 .phx.gbl>
                          | Subject: Re: Stack Size
                          | Date: Fri, 10 Oct 2003 15:46:20 -0700
                          | Lines: 27
                          | X-Priority: 3
                          | X-MSMail-Priority: Normal
                          | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
                          | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
                          | Message-ID: <OG7GWB4jDHA.18 84@TK2MSFTNGP09 .phx.gbl>
                          | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
                          | NNTP-Posting-Host: 12-236-76-193.client.attb i.com 12.236.76.193
                          | Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP09.phx.g bl
                          | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1906 68
                          | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
                          |
                          | Thanks Mattias,
                          |
                          | I had hoped there was a compiler option, but at least there is a way.
                          |
                          | Ben
                          |
                          |
                          | "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                          | news:%23pF8ftaj DHA.2616@TK2MSF TNGP11.phx.gbl. ..
                          | > Ben,
                          | >
                          | > >Is it possible to change the StackSize in .NET? If so how?
                          | >
                          | > Editbin.exe /stack
                          | >
                          | > should work, I think.
                          | >
                          | >
                          | >
                          | > Mattias
                          | >
                          | > --
                          | > Mattias Sjögren [MVP] mattias @ mvps.org
                          | > http://www.msjogren.net/dotnet/
                          | > Please reply only to the newsgroup.
                          |
                          |
                          |

                          Comment

                          • Jeffrey Tan[MSFT]

                            #14
                            Re: Stack Size


                            Hi Fred,

                            I think that because C# on top of CLR is completely 'managed' - ie. memory
                            and stack allocations are handled under the cover.
                            C#/managed are compiled to IL - code is run in a virtual machine
                            environment.

                            There is also a set of API to allow finer control over memory allocation
                            for hosts in the coming version of .Net Framework.

                            Best regards,
                            Jeffrey Tan
                            Microsoft Online Partner Support
                            Get Secure! - www.microsoft.com/security
                            This posting is provided "as is" with no warranties and confers no rights.

                            --------------------
                            | From: "Fred Mellender" <nospamPlease_f redm@frontierne t.net>
                            | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
                            | References: <#otE9bQjDHA.25 04@TK2MSFTNGP09 .phx.gbl>
                            <#pF8ftajDHA.26 16@TK2MSFTNGP11 .phx.gbl>
                            <OG7GWB4jDHA.18 84@TK2MSFTNGP09 .phx.gbl> <hoShb.2172$e24 .1430@news01.ro c.ny>
                            <KOI7PhWkDHA.25 56@cpmsftngxa06 .phx.gbl>
                            | Subject: Re: Stack Size
                            | Lines: 145
                            | X-Priority: 3
                            | X-MSMail-Priority: Normal
                            | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
                            | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
                            | Message-ID: <Mxuib.1750$Rj5 .1087@news02.ro c.ny>
                            | X-Complaints-To: abuse-news@frontierne t.net
                            | X-Trace:
                            52616e646f6d495 603120f292ac5ee 1f96e15ff40a289 0a76ee2e67f3bae cd4d8941f5b87c8 e
                            5d6e5833b660d4c 3166e956bfc51cc 3f300260143400f e3142f055e9ae0f 831dd0e6503ac8d 4
                            e25f2ae19e5806b b4028cd1de61038 10f79e0835853ba 16c478c11dc9c64 4967267b9d7d
                            | X-Abuse-Info: Please be sure to forward ALL headers so that we may
                            process your complaint properly.
                            | NNTP-Posting-Date: Mon, 13 Oct 2003 09:48:28 GMT
                            | Date: Mon, 13 Oct 2003 09:48:28 GMT
                            | Path:
                            cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new s-out.cwix.com!ne ws-out!news!ne
                            wsfeed.cwix.com !prodigy.com!in .100proofnews.c om!in.100proofn ews.com!news-xfe
                            r.cox.net!peer0 1.cox.net!peer0 2.cox.net!cox.n et!news-feed01.roc.ny.f rontiern
                            et.net!nntp.fro ntiernet.net!ne ws02.roc.ny.POS TED!not-for-mail
                            | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1909 16
                            | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
                            |
                            | Thanks for the information and it is good to know there is a work-around.
                            |
                            | However, I see that the solution requires writing in C++ and use of the
                            | "_asm" keyword. Perhaps there is no way for C# to accomplish the same
                            thing
                            | in a "managed" way? I'm sure the C# designers have plenty on their plates
                            | already, but it would be nice to see the stack growth handled
                            automatically,
                            | or perhaps through a more "C# pure" exception handler.
                            |
                            | But the information you provided is useful to me. I am working on a
                            | Prolog-like system where recursion is often a substitute for loops.
                            |
                            |
                            | "Jeffrey Tan[MSFT]" <v-jetan@online.mi crosoft.com> wrote in message
                            | news:KOI7PhWkDH A.2556@cpmsftng xa06.phx.gbl...
                            | >
                            | > Hi Fred,
                            | >
                            | > The stack is set to an arbitrarily small value when the program is first
                            | > loaded. The stack then grows on demand to meet the needs of the thread.
                            | > This is implemented by placing a page with PAGE_GUARD access at the end
                            of
                            | > the current stack. When your code causes the stack pointer to point to
                            an
                            | > address on this page, an exception occurs. The system will commit your
                            | > desired page. The 1M was the default maximum stack size that can be
                            | commit.
                            | >
                            | > To work around the recursive problem, you can handle the over flow
                            | > exception, commit more page yourself, for more details please refer to
                            | > "Trapping the Exception with __try and __except (Full Solution)" in the
                            | > link below:
                            | > http://support.microsoft.com/default...b;en-us;315937
                            | >
                            | > Hope this helps,
                            | > Best regards,
                            | > Jeffrey Tan
                            | > Microsoft Online Partner Support
                            | > Get Secure! - www.microsoft.com/security
                            | > This posting is provided "as is" with no warranties and confers no
                            rights.
                            | >
                            | > --------------------
                            | > | From: "Fred Mellender" <nospamPlease_f redm@frontierne t.net>
                            | > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
                            | > | References: <#otE9bQjDHA.25 04@TK2MSFTNGP09 .phx.gbl>
                            | > <#pF8ftajDHA.26 16@TK2MSFTNGP11 .phx.gbl>
                            | > <OG7GWB4jDHA.18 84@TK2MSFTNGP09 .phx.gbl>
                            | > | Subject: Re: Stack Size
                            | > | Lines: 60
                            | > | X-Priority: 3
                            | > | X-MSMail-Priority: Normal
                            | > | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
                            | > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
                            | > | Message-ID: <hoShb.2172$e24 .1430@news01.ro c.ny>
                            | > | X-Complaints-To: abuse-news@frontierne t.net
                            | > | X-Trace:
                            | >
                            |
                            52616e646f6d495 6a7c2f1d1725bf9 c71a51cc963cf8a f61d4bf1cb8db7c e0b5f504894fa32 0
                            | >
                            |
                            62d943d8f53fcba f8e94930c7856e7 b450326e8242345 776f33dd68c3bef 19dbe75a3af0e5b 3
                            | > 07cff1b21e7500f 5b9f8df775dc4ec af6611d9ba8fc8c b03247f51d5bca1 0438b45eabb4
                            | > | X-Abuse-Info: Please be sure to forward ALL headers so that we may
                            | > process your complaint properly.
                            | > | NNTP-Posting-Date: Sat, 11 Oct 2003 12:07:41 GMT
                            | > | Date: Sat, 11 Oct 2003 12:07:41 GMT
                            | > | Path:
                            | >
                            |
                            cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new s-out.cwix.com!ne wsfeed.cwix.co
                            | >
                            |
                            m!prodigy.com!r ip!news.webusen et.com!peer01.c ox.net!cox.net! newshosting.com !
                            | >
                            |
                            news-xfer2.atl.newsh osting.com!news-feed01.roc.ny.f rontiernet.net! nntp.front
                            | > iernet.net!news 01.roc.ny.POSTE D!not-for-mail
                            | > | Xref: cpmsftngxa06.ph x.gbl
                            | microsoft.publi c.dotnet.langua ges.csharp:1907 09
                            | > | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
                            | > |
                            | > | If the answer is not too arcane can someone explain to me why the
                            stack
                            | > size
                            | > | had to be set at compile/load time. Why did the c# designers not
                            grow
                            | it
                            | > | as required in virtual memory, which (so far as I know) is done by
                            most
                            | > | other language implementations , and let an "out of memory" exception
                            | > occur?
                            | > |
                            | > | In an object oriented language where most objects are allocated on the
                            | > heap,
                            | > | perhaps it is not so important. Nevertheless, some problems are most
                            | > easily
                            | > | coded as a recursive function and, if even a few value types are
                            used, a
                            | > 1MB
                            | > | stack does not seem like much.
                            | > |
                            | > | E.G. (not too realistic, maybe, given that a double would not hold the
                            | > | answer anyway):
                            | > | public static double fact(double x)
                            | > |
                            | > | {
                            | > |
                            | > | if (x <= 0)
                            | > |
                            | > | return 1;
                            | > |
                            | > | return x *fact(x-1);
                            | > |
                            | > | }
                            | > |
                            | > | overflows when fact(35000) is called. ("An unhandled exception of
                            type
                            | > | 'System.StackOv erflowException ' occurred in Foo.exe"). So less than
                            | 35000
                            | > | recursive calls can occur in a function that uses type double as a
                            | > | parameter?
                            | > |
                            | > | "Ben R. Bolton" <xbridgehd@hotm ail.com> wrote in message
                            | > | news:OG7GWB4jDH A.1884@TK2MSFTN GP09.phx.gbl...
                            | > | > Thanks Mattias,
                            | > | >
                            | > | > I had hoped there was a compiler option, but at least there is a
                            way.
                            | > | >
                            | > | > Ben
                            | > | >
                            | > | >
                            | > | > "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
                            | > | > news:%23pF8ftaj DHA.2616@TK2MSF TNGP11.phx.gbl. ..
                            | > | > > Ben,
                            | > | > >
                            | > | > > >Is it possible to change the StackSize in .NET? If so how?
                            | > | > >
                            | > | > > Editbin.exe /stack
                            | > | > >
                            | > | > > should work, I think.
                            | > | > >
                            | > | > >
                            | > | > >
                            | > | > > Mattias
                            | > | > >
                            | > | > > --
                            | > | > > Mattias Sjögren [MVP] mattias @ mvps.org
                            | > | > > http://www.msjogren.net/dotnet/
                            | > | > > Please reply only to the newsgroup.
                            | > | >
                            | > | >
                            | > |
                            | > |
                            | > |
                            | >
                            |
                            |
                            |

                            Comment

                            Working...