Questions tagged [memory-management]

Process of dynamically allocating and freeing portions of physical memory in order to respond to program requests with, if possible, fairness and no starvation among the requesters.

Memory management is the act of managing computer memory. The essential requirement of memory management is to provide ways to dynamically allocate portions of memory to programs at their request, and freeing it for reuse when no longer needed.

Memory management usually occurs at two distinct levels:

  1. Virtual memory management
    The operating system kernel manages the pages of physical memory and maps them into the virtual address spaces of processes at their request.

    The following methods/algorithms fall into the category of virtual memory management:

    • Paging: The act of writing the contents of unused pages to disc in order to reuse the RAM page for other purposes (page out). When the data is needed again, the hardware issues an interrupt due to an unmapped memory address, which the kernel resolves by reading the page back from disc. Even though paging is entirely transparent to user space processes, it can severely slow a system down.

    • CoW (Copy on Write): Good implementations of the fork() system call will not copy the entire data of the process. Instead, only the page tables are copied, and all memory pages are marked as read-only. Whenever an access violation occurs because one of the forked processes tries to write to a memory page, the kernel just copies the contents of that page.

    • Memory mapped files: Memory pages do not need to be backed by the swap area, the may also be backed by files. Such memory mapped files may usually be paged out without writing anything to disc, because memory mapping is usually used as a means of input rather than output. Code sections from executables and libraries, for instance, are usually memory mapped, which avoids duplication of data in memory when several processes use the same executable/libraries. Memory mapping is also available to user space processes via the mmap() system call.

  2. Dynamic memory management

    This second level of memory management is local to every process, and is usually implemented by the standard C library (malloc(), realloc(), and free()). As a second level of memory management, malloc() relies on the brk() and mmap() system calls to request memory pages from the kernel. Large allocations are usually directly satisfied by an mmap() call. For small allocations, malloc() implementations keep track of mapped, unused memory, subdividing the unused memory as necessary to match the requests from the user code, and requesting new memory pages from the kernel whenever no suitable free memory block is found.

    Common goals for dynamic memory allocators:

    • Thread safety

    • No unnecessary memory fragmentation

    • Low amount of memory used for bookkeeping purposes

    • Low allocation/deallocation latencies

      This includes optimizations of the search for suitable free blocks, and methods to reduce the amount of system calls.

    Another method of memory management within a process is the function call stack. This is usually managed by manipulating a single, dedicated register in the CPU. On entry, every function changes the value of this stack pointer register to allocate stack memory for its local variables, and resets the stack pointer before it returns to its caller. This method of memory allocation is extremely fast, however, the lifetime of the objects on the stack is limited to the function that allocates them.

    Stack memory may either be provided as a block of fixed size, or may be dynamically increased by the kernel whenever it notices an access violation just below the stack memory region.

    Even though the kernel is not responsible to do dynamic memory management for the processes, it still needs to do so for itself, in LINUX, this is done by the kmalloc() function. Also, kernel threads need their own stacks. In contrast to the user space dynamic memory managers, the kernel facilities cannot rely on the virtual memory manager.

Common to virtual memory management and dynamic memory management is the necessity to know when a block of memory may be deallocated. There are three approaches to handle this:

  • Explicit management: The user has to take care that every allocation call is matched by a deallocation call.

  • Reference counting: Some entity keeps track of how many times every memory block is referenced. The reference count may reside in the memory block itself, or somewhere outside of it. An important implementation of this is the std::shared_ptr<> in C++.

  • Garbage collection: Used in high level scripting languages and in Java. Memory is only allocated, never explicitly deallocated, deallocation is the job of the garbage collector which scans all used memory for references to memory block and automatically deallocates blocks that are not referenced anymore.

Several methods have been devised that increase the effectiveness of the different memory management facilities, as the quality of the involved memory managers can have an extensive effect on overall system performance.

25779 questions
8542
votes
28 answers

What and where are the stack and heap?

Programming language books explain that value types are created on the stack, and reference types are created on the heap, without explaining what these two things are. I haven't read a clear explanation of this. I understand what a stack is. But,…
1181
votes
6 answers

What are the -Xms and -Xmx parameters when starting JVM?

Please explain the use of Xms and Xmx parameters in JVMs. What are the default values for them?
Pankaj
  • 12,220
  • 3
  • 15
  • 22
1087
votes
21 answers

Can a local variable's memory be accessed outside its scope?

I have the following code. #include int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *p; } And the code is just running with no runtime exceptions! The…
Avi Shukron
  • 5,923
  • 8
  • 45
  • 82
921
votes
19 answers

Why should C++ programmers minimize use of 'new'?

I stumbled upon Stack Overflow question Memory leak with std::string when using std::list, and one of the comments says this: Stop using new so much. I can't see any reason you used new anywhere you did. You can create objects by…
bitgarden
  • 9,749
  • 3
  • 15
  • 24
813
votes
9 answers

How do I discover memory usage of my application in Android?

How can I find the memory used on my Android application, programmatically? I hope there is a way to do it. Plus, how do I get the free memory of the phone too?
Andrea Baccega
  • 25,523
  • 12
  • 41
  • 44
806
votes
13 answers

How do I determine the size of an object in Python?

I want to know how to get size of objects like a string, integer, etc. in Python. Related question: How many bytes per element are there in a Python list (tuple)? I am using an XML file which contains size fields that specify the size of value. I…
user46646
  • 133,483
  • 43
  • 73
  • 82
774
votes
2 answers

Why is my program slow when looping over exactly 8192 elements?

Here is the extract from the program in question. The matrix img[][] has the size SIZE×SIZE, and is initialized at: img[j][i] = 2 * j + i Then, you make a matrix res[][], and each field in here is made to be the average of the 9 fields around it in…
anon
694
votes
8 answers

Which Python memory profiler is recommended?

I want to know the memory usage of my Python application and specifically want to know what code blocks/portions or objects are consuming most memory. Google search shows a commercial one is Python Memory Validator (Windows only). And open source…
Anurag Uniyal
  • 77,208
  • 39
  • 164
  • 212
528
votes
19 answers

In what cases do I use malloc and/or new?

I see in C++ there are multiple ways to allocate and free data and I understand that when you call malloc you should call free and when you use the new operator you should pair with delete and it is a mistake to mix the two (e.g. Calling free() on…
Ralph Burgess
518
votes
4 answers

What is private bytes, virtual bytes, working set?

I am trying to use the perfmon windows utility to debug memory leaks in a process. This is how perfmon explains the terms: Working Set is the current size, in bytes, of the Working Set of this process. The Working Set is the set of memory pages…
pankajt
  • 7,024
  • 11
  • 33
  • 59
513
votes
28 answers

Tricks to manage the available memory in an R session

What tricks do people use to manage the available memory of an interactive R session? I use the functions below [based on postings by Petr Pikal and David Hinds to the r-help list in 2004] to list (and/or sort) the largest objects and to…
Dirk Eddelbuettel
  • 331,520
  • 51
  • 596
  • 675
470
votes
10 answers

How can I explicitly free memory in Python?

I wrote a Python program that acts on a large input file to create a few million objects representing triangles. The algorithm is: read an input file process the file and create a list of triangles, represented by their vertices output the…
Nathan Fellman
  • 108,984
  • 95
  • 246
  • 308
447
votes
25 answers

What uses are there for "placement new"?

Has anyone here ever used C++'s "placement new"? If so, what for? It looks to me like it would only be useful on memory-mapped hardware.
Head Geek
  • 33,955
  • 20
  • 72
  • 83
443
votes
17 answers

How to allocate aligned memory only using the standard library?

I just finished a test as part of a job interview, and one question stumped me, even using Google for reference. I'd like to see what the StackOverflow crew can do with it: The memset_16aligned function requires a 16-byte aligned pointer passed to…
JimDaniel
  • 11,993
  • 6
  • 57
  • 66
417
votes
20 answers

Peak memory usage of a linux/unix process

Is there a tool that will run a command-line and report the peak RAM usage total? I'm imagining something analogous to /usr/bin/time
jes5199
  • 16,375
  • 11
  • 35
  • 40
1
2 3
99 100