操作系统将RAM分为内核空间和用户空间,内核空间下面都属于用户空间,因为用户是可以操作的,内核空间是不允许用户接触到的。
他是CPU要使用的内核存储区
Operating system kernel resides at top of the address space
一般程序员无法直接控制
在最底部
存储程序代码和静态数据
Program and static data occupies bottom of address space
n内存映射文件
Used to map shared libraries into memory
存储方法参数,返回值地址,本地变量,引用地址
The stack holds function parameters, return address, and local variables
堆存储显示分配的内存,比如C中的malloc()和java中new创建的对象
The heap holds explicitly allocated memory
内存管理主要是说堆内存管理,因为栈的内存总是自动管理的
Memory management is primarily concerned with reclaiming heap memory
目标是找到不再使用的对象,释放他们的空间以供再利用
Aim is to find objects that are no longer used, and make their space available for reuse
内存管理分为手动管理和自动管理:
手动管理:比如:C语言用free()
自动管理:比如Rust使用region-based方式,而JAVA用garbage collection方式
自动内存管理又分为几种方式:
最简单的自动内存管理方式:
Simplest automatic heap management
广泛使用在一些脚本语言,Python,ruby等
Widely used in scripting languages :Python, Ruby, etc.
也在一些小规模的系统编程中使用
Used on small scale for systems programming e.g., Objective C runtime on iOS
每个对象都会分配一个额外的空间作为引用计数,记录有多少其他对象指向他,如果这个数为0 则说明没有指向他的reference则他就会清理
优点
简单,内存在小范围内回收,容易理解内存回收和cost
缺点
循环数据相互给引用,从而不能移除造成内存泄漏
Cyclic data structures give mutual references Objects all reference each other
需要额外内存开销来存储引用
处理器时间来更新引用
garbage collection有不可预测的时间,和高内存开销
Garbage collection tends to have unpredictable timing and high memory overhead
Region-based 内存管理是这几种办法的折中
Region-based memory management aims for a middle ground
他安全可预测时间,无run-time消耗
Safe, predictable timing – no run-time cost
Limited impact on application design
一种Stack-based Memory Management,他依赖于stack非常高效,但是需要在stack上分配内存,并且把堆内存分配和栈绑定了
原理:
Giving ownership of data给所有权
变量作为参数传给另一个方法,则这个变量所有权转移。
Returning ownership of data返回所有权
一个方法中return变量就会return这个所有权给到main方法。那么这个变量声明周期延长。
Borrowing data借所有权,安全的借:使用&mut
使用borrow关键字来借变量的引用,a的所有权没有转移
Rust has 2 kinds of pointer
&T is a shared reference to an immutable object of type T ,can be referenced by one or more references
&mut T is a unique reference to a mutable object of type T, can only be referenced by one reference.
优点
缺点
避免了reference counting的复杂性,和Ownership 的追踪,但是有不可预测性
Avoid problems of reference counting and complexity of compile-time ownership tracking via tracing garbage collection
显示的最终分配的对象,记录谁用了他们丢弃无用的对象,而不是维持一个reference counts
Explicitly trace through the allocated objects, recording which are in use, rather
than continually maintaining reference counts; dispose of unused objects
许多垃圾回收的追踪算法
tracing garbage collection algorithms exist:
最简单的自动垃圾回收机制
Simplest automatic garbage collection scheme
Two phase algorithm
• Distinguish live objects from garbage (mark) 标记存活的对象
• Reclaim the garbage (sweep) 回收到garbage
problem:
回收不可用的对象,然后重新整理存在的对象,把他们移动到一起然后空出连续的内存,应该是mark-sweep的升级版
Reclaim unreachable objects, then compact live objects, moving them to leave a contiguous free space
Advantages:
• Solves fragmentation problems 解决了碎片化
• Allocation is very quick (increment pointer to next free space, return previous value) 分配空间很快
Disadvantages:
• Collection is slow, due to moving objects in memory, and time taken is unpredictable 收集速度很慢因为需要移动对象
• Collection has poor locality of reference
把内存分两半
所有存在的数据都被拷贝到另一块内存,并整理到一起
All the live data is copied into one region of memory
然后清理这一块内存的所有数据
All the remaining memory contains garbage, or has not yet been used
和标记整理很相似,但是更高效
Similar to mark-compact, but more efficient
然后使用另一块内存重启程序
The program is then restarted, using the other half of the heap as the active allocation region
每次垃圾回收触发时,就交换这两个内存的角色。
The role of the two parts of the heap (the two semispaces) reverses each time a collection is triggered
效率:
只有当一块内存满了的时候才进行垃圾回收整理,
为收集时间权衡内存:使用更多内存,但复制数据所花费的时间更少
Trade-off memory for collection time: more memory used, less fraction of time spent copying data
广泛的应用比如JVM java 虚拟机中, 很高的效率
大多数对象的寿命都很短,只有一小部分物体的寿命要长,而且存活期越久的对象,他的存活时间将更久。所以,当垃圾收集器运行时,活动对象将占少数。
Statistically, the longer an object has lived, the longer it is likely to live
在generational garbage中,堆被分为younger generation 和 older generation
In a generational garbage collector, the heap is split into regions for long-lived and young objects
在younger generation中对象被更频繁的收集,如果对象在一段时间还存活则他被移动到older generation
• Regions holding young objects are garbage collected more frequently
• Objects are moved to the region for long-lived objects if they’re still alive after several collections
垃圾回收时,使用copy collector类似的方法,把所有对象排列好放到新的堆中
通常来说,垃圾回收程序将会使用更大的内存空间,比手动内存管理的程序
In general, garbage collected programs will use significantly more memory than (correct) programs with manual memory management
E.g., many of the copying collectors must maintain two regions, and so a naïve implementation doubles memory usage