How to solve Stack Overflow

Source: Internet
Author: User

1. What is stack overflow?

Because the stack is generally 1-2 m, once an endless loop or a large number of recursive calls occur, the stack capacity will exceed 1 m during the continuous stack pressure process, leading to overflow.

2. solution:

Method 1: Use stacks to convert recursion into non-recursion

Generally, before calling a function, you need to do the following: a) Pass the real number of workers, return address, and other information to the called function for storage; B) allocate a storage area for the local variables of the called function; c) Transfer the control to the entry of the called function. there are three things to do before the called function returns the called function: a) Save the computing result of the called function; B) release the data zone of the called function; c) transfer the control to the call function according to the return address saved by the called function. all of these are essentially "data", both variables and addresses, and are stored in the stack allocated by the system. you can write a stack to store the necessary data to reduce the burden on the system.

 

Method 2: use static objects instead of nonstatic local objects

In recursive function design, static objects can be used to replace nonstatic local objects (stack objects). This not only reduces the overhead of generating and releasing nonstatic objects during each recursive call and return, in addition, the static object can save the intermediate state of the recursive call and can be asked by each call layer.

 

Method 3: Increase the stack size

When a thread stack is created, the system retains the address space area specified by the/stack switch of a linked program. However, when the createthread or _ beginthreadex function is called, the previously submitted memory quantity can be reloaded. Both functions have a memory number, which can be used to reload the memory size of the address space originally submitted to the stack. If the number of committed Stacks is set to 0, the system uses the committed stack size value specified by the/stack switch. Next we will assume that we use the default stack size value, that is, 1 MB of reserved area, each time a page of memory is submitted.

 

Java sets the stack size when creating a thread: thread (threadgroup group, runnable target, string name, long stacksize)
Assign a new thread object to use target as its execution object, specify the name as its name, act as a member of the thread group referenced by the group, and have the specified stack size.

How to set the heap size of the Java Virtual Machine: Command Line

Java-xms128m // JVM occupies the Minimum Memory

-Xmx512m // JVM occupies the maximum memory

-XX: permsize = 64 m // minimum heap size

-XX: maxpermsize = 128 M // maximum heap size

This article from http://blog.csdn.net/luqiang454171826, reference must indicate the source!

 

How to solve Stack Overflow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.