All Topics  
Dynamic memory allocation

 

   Email Print
   Bookmark   Link






 

Dynamic memory allocation



 
 
In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, dynamic memory allocation is the allocation of memory
Computer storage

Computer data storage, often called storage or memory, refers to computer components, devices, and recording medium that retain digital data used for computing for some interval of time....
 storage for use in a computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
 during the runtime
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
 of that program. It can be seen also as a way of distributing ownership of limited memory resources among many pieces of data and code.

Dynamically allocated memory exists until it is released either explicitly by the programmer, exiting a block
Structured programming

Structured programming can be seen as a subset or subdiscipline of procedural programming, one of the major programming paradigms. It is most famous for removing or reducing reliance on the GOTO Statement ....
, or by the garbage collector
Garbage collection (computer science)

In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage , or memory used by Object that will never be accessed or mutated again by the Application software....
. This is in contrast to static memory allocation
Static memory allocation

Static memory allocation refers to the process of allocating memory at Compile time before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at Runtime....
, which has a fixed duration.






Discussion
Ask a question about 'Dynamic memory allocation'
Start a new discussion about 'Dynamic memory allocation'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In computer science
Computer science

Computer science is the study of the theoretical foundations of information and computation, and of practical techniques for their implementation and application in computer systems....
, dynamic memory allocation is the allocation of memory
Computer storage

Computer data storage, often called storage or memory, refers to computer components, devices, and recording medium that retain digital data used for computing for some interval of time....
 storage for use in a computer program
Computer program

Computer programs are Instruction for a computer. A computer requires programs to function. Moreover, a computer program does not run unless its instructions are executed by a Central processing unit; however, a program may communicate an Algorithm#Formalization of algorithms to people without running....
 during the runtime
Runtime

In computer science, runtime or run time describes the operation of a computer program, the duration of its execution, from beginning to termination ....
 of that program. It can be seen also as a way of distributing ownership of limited memory resources among many pieces of data and code.

Dynamically allocated memory exists until it is released either explicitly by the programmer, exiting a block
Structured programming

Structured programming can be seen as a subset or subdiscipline of procedural programming, one of the major programming paradigms. It is most famous for removing or reducing reliance on the GOTO Statement ....
, or by the garbage collector
Garbage collection (computer science)

In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage , or memory used by Object that will never be accessed or mutated again by the Application software....
. This is in contrast to static memory allocation
Static memory allocation

Static memory allocation refers to the process of allocating memory at Compile time before the associated program is executed, unlike dynamic memory allocation or automatic memory allocation where memory is allocated as required at Runtime....
, which has a fixed duration. It is said that an object so allocated has a dynamic lifetime.

Details

  • The task of fulfilling allocation request
    • Finding a block of unused memory of sufficient size


  • Problems during fulfilling allocation request
    • Internal and external fragmentation
      Fragmentation (computer)

      In computer storage, fragmentation is a phenomenon in which storage space is used inefficiently, reducing storage capacity. The term is also used to denote the wasted space itself....
      .
      • Reduction needs special care, thus making implementation more complex (see algorithm efficiency
        Algorithmic efficiency

        In computer science, efficiency is used to describe properties of an algorithm relating to how much of various types of resources it consumes. The two most frequently encountered are...
        ).
    • Allocator's metadata can inflate the size of (individually) small allocations;
      • Chunking
        Chunking (computing)

        In computer programming, chunking has multiple meanings....
         attempts to reduce this effect.


Usually, memory is allocated from a large pool of unused memory area called the heap (also called the free store). Since the precise location of the allocation is not known in advance, the memory is accessed indirectly, usually via a reference
Reference (computer science)

In computer science, a reference is an object containing information about how to locate and access the particular data item, as opposed to containing the data itself....
. The precise algorithm used to organize the memory area and allocate and deallocate chunks is hidden behind an abstract interface and may use any of the methods described below.

Implementations


Fixed-size-blocks allocation


Fixed-size-blocks allocation, also called memory pool allocation, uses a free list
Free list

A free list is a data structure used in a scheme for dynamic memory allocation. It operates by connecting unallocated regions of memory together in a linked list, using the first word of each unallocated region as a pointer to the next....
 of fixed-size blocks of memory (often all of the same size). This works well for simple embedded system
Embedded system

An embedded system is a special-purpose computer system designed to perform one or a few dedicated functions, often with real-time computing constraints....
s.

Buddy blocks

In this system, memory is allocated from a large block in memory that is a power of two
Power of two

In mathematics, a power of two is any of the integer exponentiation of the number 2 ; in other words, two multiplication by itself a certain number of times....
 in size. If the block is more than twice as large as desired, it is broken in two. One of the halves is selected, and the process repeats (checking the size again and splitting if needed) until the block is just large enough.

All the blocks of a particular size are kept in a sorted linked list
Linked list

In computer science, a linked list is one of the fundamental data structures, and can be used to implement other data structures. It consists of a sequence of node s, each containing arbitrary data Field s and one or two reference s pointing to the next and/or previous nodes....
 or tree. When a block is freed, it is compared to its buddy. If they are both free, they are combined and placed in the next-largest size buddy-block list. (When a block is allocated, the allocator will start with the smallest sufficiently large block avoiding needlessly breaking blocks)

See also

  • Automatic memory allocation
    Automatic memory allocation

    In computer programming, an automatic variable is a scope variable which is allocated and de-allocated automatically when program flow enters and leaves the variable's scope ....
  • Hazard pointer
    Hazard pointer

    In a Thread computer science environment, a hazard pointer is an element used by a method that allows the memory allocated to the node of lock dynamic shared object to be reclaimed....
  • Heap overflow
    Heap overflow

    A heap overflow is a type of buffer overflow that occurs in the Heap data area....
  • Hoard memory allocator
    Hoard memory allocator

    The Hoard memory allocator, or Hoard, is a memory allocation for Linux, Solaris , Microsoft Windows and other operating systems. Hoard can improve the performance of multithreaded applications by providing fast, scalable memory management functions ....
  • malloc
    Malloc

    In computing, malloc is a subroutine provided in the C and C++'s standard library for performing dynamic memory allocation....
  • Memory pool
    Memory pool

    Memory pools, also called Memory_allocation#Fixed-size-blocks_allocation , allow dynamic memory allocation comparable to malloc or C++'s new . As those implementations suffer from fragmentation because of variable block sizes, it can be impossible to use them in a Real-time computing due to performance....
  • mmap
    Mmap

    In computing, mmap is a POSIX-compliant Unix system call that maps files or devices into memory. It is a method of memory-mapped file I/O....
  • obstack
    Obstack

    An obstack is a Stack of Object which is grown dynamically.Obstack code typically provides C Macro which take care of memory allocation and management for you....
  • Slab allocation
    Slab allocation

    Slab allocation is a memory management mechanism intended for more efficient memory allocation and to eliminate memory Fragmentation to a large extent....
  • Stack-based memory allocation
    Stack-based memory allocation

    Stack s in computing architectures are regions of memory where data is added or removed in a LIFO manner.In most modern computer systems, each Thread has a reserved region of memory referred to as its stack....
  • new (C++)
  • Java Virtual Machine heap
    Java Virtual Machine heap

    The Java Virtual Machine heap is the area of memory used by the Java Virtual Machine for dynamic memory allocation.The heap is split up into "generations":...


External links

  • [https://users.cs.jmu.edu/bernstdh/web/common/lectures/slides_cpp_dynamic-memory.php Slides for knowing about Dynamic memory allocation]