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.

27281 questions
31 answers

What and where are the stack and heap?

What are the stack and heap? Where are they located physically in a computer's memory? To what extent are they controlled by the OS or language run-time? What is their scope? What determines their sizes? What makes one faster?
5 answers

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

Please explain the use of the Xms and Xmx parameters in JVMs. What are the default values for them?
  • 14,638
  • 3
  • 17
  • 23
20 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
  • 6,088
  • 8
  • 50
  • 84
16 answers

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

How do I get the size occupied in memory by an object in Python?
  • 153,461
  • 44
  • 78
  • 84
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 value…
  • 10,461
  • 3
  • 18
  • 25
10 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
  • 27,211
  • 13
  • 45
  • 46
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…
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
  • 85,954
  • 40
  • 175
  • 219
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
  • 122,701
  • 101
  • 260
  • 319
20 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
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…
  • 7,642
  • 12
  • 39
  • 60
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
  • 360,940
  • 56
  • 644
  • 725
18 answers

How do I check if PyTorch is using the GPU?

How do I check if PyTorch is using the GPU? The nvidia-smi command can detect GPU activity, but I want to check it directly from inside a Python script.
  • 25,404
  • 19
  • 49
  • 81
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
  • 38,128
  • 22
  • 77
  • 87
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
  • 18,324
  • 12
  • 36
  • 40
2 3
99 100