小结:

1、垃圾回收的本质:找到并回收不再被使用的内存空间;

2、标记清除方式和复制收集方式的对比;

3、复制收集方式的局部性优点;

 

 

https://en.wikipedia.org/wiki/C_(programming_language)#Memory_management

Memory management

One of the most important functions of a programming language is to provide facilities for managing memory and the objects that are stored in memory. C provides three distinct ways to allocate memory for objects:[27]

  • Static memory allocation: space for the object is provided in the binary at compile-time; these objects have an extent (or lifetime) as long as the binary which contains them is loaded into memory.
  • Automatic memory allocation: temporary objects can be stored on the stack, and this space is automatically freed and reusable after the block in which they are declared is exited.
  • Dynamic memory allocation: blocks of memory of arbitrary size can be requested at run-time using library functions such as malloc from a region of memory called the heap; these blocks persist until subsequently freed for reuse by calling the library function realloc or free

These three approaches are appropriate in different situations and have various trade-offs. For example, static memory allocation has little allocation overhead, automatic allocation may involve slightly more overhead, and dynamic memory allocation can potentially have a great deal of overhead for both allocation and deallocation. The persistent nature of static objects is useful for maintaining state information across function calls, automatic allocation is easy to use but stack space is typically much more limited and transient than either static memory or heap space, and dynamic memory allocation allows convenient allocation of objects whose size is known only at run-time. Most C programs make extensive use of all three.

Where possible, automatic or static allocation is usually simplest because the storage is managed by the compiler, freeing the programmer of the potentially error-prone chore of manually allocating and releasing storage. However, many data structures can change in size at runtime, and since static allocations (and automatic allocations before C99) must have a fixed size at compile-time, there are many situations in which dynamic allocation is necessary.loader, before the program can even begin execution.)

Unless otherwise specified, static objects contain zero or null pointer values upon program startup. Automatically and dynamically allocated objects are initialized only if an initial value is explicitly specified; otherwise they initially have indeterminate values (typically, whatever bit pattern happens to be present in the storage, which might not even represent a valid value for that type). If the program attempts to access an uninitialized value, the results are undefined. Many modern compilers try to detect and warn about this problem, but both false positives and false negatives can occur.

Another issue is that heap memory allocation has to be synchronized with its actual usage in any program in order for it to be reused as much as possible. For example, if the only pointer to a heap memory allocation goes out of scope or has its value overwritten before free() is called, then that memory cannot be recovered for later reuse and is essentially lost to the program, a phenomenon known as a memory leak. Conversely, it is possible for memory to be freed but continue to be referenced, leading to unpredictable results. Typically, the symptoms will appear in a portion of the program far removed from the actual error, making it difficult to track down the problem. (Such issues are ameliorated in languages with automatic garbage collection.) 

 

https://zh.wikipedia.org/wiki/C动态内存分配

C动态内存分配是在C语言中为了实现动态内存分配而进行的手动内存管理。这种管理是通过C标准库中的 mallocrealloccallocfree 等函数进行的。[2]

C++ 为了兼容 C 语言也提供这些函数,但是更推荐使用 newdelete 操作符来完成类似的操作。[3]

malloc 所实际使用的内存分配机制有很多不同的实现,执行时间和内存消耗各有不同。

目录

相关文章:

  • 2021-10-16
  • 2021-10-08
  • 2022-02-23
  • 2022-02-07
  • 2022-01-28
猜你喜欢
  • 2021-06-23
  • 2021-12-28
  • 2022-01-13
  • 2022-01-15
  • 2022-02-11
相关资源
相似解决方案