473,782 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Nov 15 '05
13 25561
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-*****@online.mi crosoft.com> wrote in message
news:KO******** ******@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" <no************ ****@frontierne t.net>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| References: <#o************ **@TK2MSFTNGP09 .phx.gbl>
<#p************ **@TK2MSFTNGP11 .phx.gbl>
<OG************ **@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: <ho************ *****@news01.ro c.ny>
| X-Complaints-To: ab********@fron tiernet.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" <xb*******@hotm ail.com> wrote in message
| news:OG******** ******@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" <ma************ ********@mvps.o rg> wrote in message
| > news:%2******** ********@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.
| >
| >
|
|
|

Nov 15 '05 #11
"Jeffrey Tan[MSFT]" <v-*****@online.mi crosoft.com> wrote in message
news:KO******** ******@cpmsftng xa06.phx.gbl...
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

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
Nov 15 '05 #12

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:
http://register.microsoft.com/mswish/suggestion.asp
or you can mail to ms****@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" <xb*******@hotm ail.com>
| From: "Ben R. Bolton" <xb*******@hotm ail.com>
| References: <#o************ **@TK2MSFTNGP09 .phx.gbl>
<#p************ **@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: <OG************ **@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" <ma************ ********@mvps.o rg> wrote in message
| news:%2******** ********@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.
|
|
|

Nov 15 '05 #13

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" <no************ ****@frontierne t.net>
| Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| References: <#o************ **@TK2MSFTNGP09 .phx.gbl>
<#p************ **@TK2MSFTNGP11 .phx.gbl>
<OG************ **@TK2MSFTNGP09 .phx.gbl> <ho************ *****@news01.ro c.ny>
<KO************ **@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: <Mx************ *****@news02.ro c.ny>
| X-Complaints-To: ab********@fron tiernet.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-*****@online.mi crosoft.com> wrote in message
| news:KO******** ******@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" <no************ ****@frontierne t.net>
| > | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
| > | References: <#o************ **@TK2MSFTNGP09 .phx.gbl>
| > <#p************ **@TK2MSFTNGP11 .phx.gbl>
| > <OG************ **@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: <ho************ *****@news01.ro c.ny>
| > | X-Complaints-To: ab********@fron tiernet.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" <xb*******@hotm ail.com> wrote in message
| > | news:OG******** ******@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" <ma************ ********@mvps.o rg> wrote in message
| > | > news:%2******** ********@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.
| > | >
| > | >
| > |
| > |
| > |
| >
|
|
|

Nov 15 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
30099
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
10
8348
by: Shuo Xiang | last post by:
Greetings: I know that variables declared on a stack definitely does not reside in heap space so there is only a very limited amount of stuff that you can store in the stack of a function. But what about the global space? Is that the same as the heap space or is that still a form of special stack? Because once I've seen someone declare a 1 megabyte char array in global space like this: char s;
2
1882
by: Peter Oliphant | last post by:
Hey, is there a limit to the stack size? I realize of course there must be, but how is this determined/set? Or does it self adjust up to, say, available memory (and possibly virtual memory from hard drives)? My current project can be set to be from very-stack-intensive to not-so-stack-intensive. The more stack-intensive it is the faster the code will run, but it can conceivable want to put huge amounts of info on the stack (there are...
9
7331
by: Ajay | last post by:
Hi all, Can I know what is the stack space and heap space allocated by the compiler.Can i increase it or decrease it.if yes,pleae tell me theway to do it.Thanks in advance. Cheers, Ajay
5
37162
by: sunny | last post by:
Hi All Is there any way to determine stack and heap size, during runtime. i.e can we predict stack overflow. etc
1
4223
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting errors of all kinds. I have decided to start from scratch. Any suggestions someone can give me would be greatly appreciated!! Here is the current code: #include <iostream> using std::cout; using std::cin; #include <cstring> using std::strcpy;
7
22051
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char a; (this type of code and other complex mallc/free etc is used frequently and total approx 450 files, each file is of approx/avg 1000 line,
9
8735
by: Jensen Somers | last post by:
Hi, Is it possible to change the stack size of a DLL from within the source code? When using threads you can specify the size of the stack you want the thread to use, but since my application doesn't use threads I'm looking for another solution. Source code I haven't written but which I use creates a structure containing 4 double arrays of almost 30k. I know this is big but it's required and I cannot change anything about that.
3
8327
by: mark4asp | last post by:
Stack of limited size containing unique items? Hi guys, How would I implement a stack of limited size containing unique items? For example. Suppose my stack has . I add 2 to it and it is now . Now I want to add 5 to the unique item stack so it should now be: . The item added is pulled from the stack, then pushed. The stack should also have a maximum size of 4 items. I add 4 and 7
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.