Difference between revisions of "Dynamic vs Static"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Memory Leakage)
(Memory Leakage)
Line 29: Line 29:
 
==Heap==
 
==Heap==
 
==Memory Leakage==
 
==Memory Leakage==
Memory leaks occur when the program assigns memory for a dynamic variable but doesn't release it back to the heap after it has finished. The program then runs again, using a new memory location without releasing. This repeats, and eventually the heap runs out of memory the program crashes.
+
Memory leaks occur when the program assigns memory for a dynamic variable but doesn't release it back to the heap after it has finished. The program then runs again, using a new memory location without releasing. This repeats, and eventually the heap runs out of memory the program crashes. <br>
Most programs have a function to release the memory, in C# it is Dispose. Many high level languages have automated features, such as garbage collectors, to prevent memory leaks.
+
Most languages have a function to release the memory, in C# it is Dispose. Many high level languages have automated features, such as garbage collectors, to prevent memory leaks.
  
 
==Garbage Collectors==
 
==Garbage Collectors==

Revision as of 07:58, 18 May 2017

This section needs expansion.
You can help by adding to it.


Static Data Structure

With a static data structure, the size of the structure is fixed. Static data structures are very good for storing a well-defined number of data items. For example a programmer might be coding an 'Undo' function where the last 10 user actions are kept in case they want to undo their actions. In this case the maximum allowed is 10 steps and so he decides to form a 10 item data structure.

Advantage

  • The memory allocation is fixed and so there will be no problem with adding and removing data items.
  • Easier to program as there is no need to check on data structure size at any point.

Disadvantage

  • Can be very inefficient as the memory for the data structure has been set aside regardless of whether it is needed or not whilst the program is executing.

Dynamic Data Structure

There are many situations where the number of items to be stored is not known before hand. Memory is allocated to the data structure dynamically i.e. as the program executes.

In this case the programmer will consider using a dynamic data structure. This means the data structure is allowed to grow and shrink as the demand for storage arises. The programmer should also set a maximum size to help avoid memory collisions.

For example a programmer coding a print spooler will have to maintain a data structure to store print jobs, but he cannot know before hand how many jobs there will be.

Advantage

  • Makes the most efficient use of memory as the data structure only uses as much memory as it needs

Disadvantage

  • Because the memory allocation is dynamic, it is possible for the structure to 'overflow' should it exceed its allowed limit. It can also 'underflow' should it become empty.
  • Harder to program as the software needs to keep track of its size and data item locations at all times

Heap

Memory Leakage

Memory leaks occur when the program assigns memory for a dynamic variable but doesn't release it back to the heap after it has finished. The program then runs again, using a new memory location without releasing. This repeats, and eventually the heap runs out of memory the program crashes.
Most languages have a function to release the memory, in C# it is Dispose. Many high level languages have automated features, such as garbage collectors, to prevent memory leaks.

Garbage Collectors