Open In App

What is the Stack Size Estimation

Last Updated : 04 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

What is stack size estimation?

  • Stack size estimation is the process of forecasting the size of the stack that an operating system might use for internal use. The stack is a section of memory used by a program in order to store its own variables, temporaries, and so on. 
  • Stack size estimation uses the stack usage pattern and the operational characteristics of a given OS to predict the most probable capacity for storing those elements.
  • According to the algorithm, a stack is a discrete memory region of fixed size. Programmers allocate space in the stack to temporary and reference variables. Each time they are called they require a memory allocation. 
  • The stack is also used to temporarily store control structures like loops, function arguments, and function return addresses. 
  • A program’s local variables are local (to the function) because they are only available when the corresponding functions are executing. Furthermore, there are no global variables that can be accessed by any function because these global variables would require much more memory since it would be necessary for each computer at all times to compute the address of all globals and store it in its own variable(s).
  • Stack size estimation is a process of predicting the size of the stack memory required for the execution of a given program, to avoid over-filling the stack with data.

First-order models for stack usage:

A stack usage pattern is a measure of how the stack space is used by an application. 

This pattern can be classified according to two types: bounded and unbounded, from which the unbounded pattern has more to offer in terms of estimation accuracy over bounded ones. Although most operating systems have some boundary stack sizes, operating systems usually have an unstable boundary in the stack sizes due to their design and features that affect stack usage.

Stack usage pattern has a large number of dimensions, but an application’s stack pattern can be identified in four dimensions namely: 

  • Depth and height – The depth is a measure that shows the number of local variables declared within the function of that application. The height is a measure that shows the number of calls from one level to another level
  • Variable scope – Variable scope is used to find how many temporaries are allocated in a call, and this is significantly different from language because languages like C/C++ have no limitation on temporaries whereas others like Pascal do.
  • Function scope, and 
  • Language

Different types of stacks:

There are two types of stacks:

  • User Stack
  • System Stack

User Stack:

A user stack is a sequence of instructions that must be completed for a program to execute. The user stack is a part of the memory allocated for the running program, and it’s what enables programs to operate on pieces of data. A user stack is always allocated at least 1 memory page size and 32k of memory. A process needs one page of data for every instruction it executes. If a process runs longer than its allocated user stack, the program eventually loses control over where it has been directed by the operating system, and usually halts.

User stack size estimation method:

  • User stack size estimation method is a technique that is used in software engineering and computer applications development, which estimates the required stack size for program execution.
  • The stack size estimation method is a technique for estimating the number of bytes of memory that will be needed to execute a given program on a particular machine. It uses timing data from the last three stages of the execution process: loading, running, and unloading.
  • The time to load a program into memory is a good indication of the actual code size, since the loader reads in all of the instructions and data. The time to run the instructions is an indication of Stack Size. Finally, the time it takes to unload newly unused memory is an indication of how much memory that needs to be saved.
  • This process can be repeated for every method and class in your program. To compare them with each other you can use one of the most popular methods called Cyclomatic Complexity.

System Stack:

A system stack can be thought of as “the space in which commands are being executed”. The system stack’s section name is represented by “SI”. The system stack is part of the physical memory space, and it temporarily stores information associated with each new process that is created or switched to. The system has only one system stack and it (system stack) is used by the kernel.

System stack size estimation method:

  • System stack size estimation method is a technique that is used to estimate the size of a program’s stack. 
  • System stack size estimation method can be used as a tool for “program optimization” and it will typically take into account the number of input and output parameters, plus the local variables. 
  • The estimation technique is especially useful if one wants to know how much memory is required by a particular program under certain conditions. 
  • The analysis technique can also be used when programming new software to estimate how much memory will be required. The system size is specified below-

System Format: 

system{
    size= <System stack size (size)>;
    priority= <Maximum task priority (priority)>;
    IPLsystem=<kernel interrupt mask level (IPLsystem)>;
    messagepriority= <Maximum message priority (messagepriority)>;
      deno= <Denominator of base clock interval time (deno)>;
     nume=<Numerator of base clock interval time (nume)>;
     context=<Task context register (context)>;
 };

Conclusion: The amount of space used by variables in stack can be estimated on the data structure, memory footprint, and resource consumption. The space usage pattern and the operational characteristics of a given operating system are taken into account while making these estimates. The algorithm starts with an investigation of how much stack is enough to manipulate or store parameters and variables in common programming languages (for example, C/C++). Then it recommends a parameter to set at system initialization because some operating systems have a higher overhead in setting down data structures than others. Finally, the algorithm determines the size of the stack space based on the type of application. The algorithm is a good tool for programming language programmers to predict stack usage by common applications.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads