- What does alloc do?
It allocates a chunk of memory to hold the object and returns the pointer.
alloc 給對象分配了一塊內存,讓對象不釋放,并且把地址返回給指針。
光有alloc分配了一塊地址是不夠的,對象還沒有真正被創建。 - What does init do?
init才真正地把對象初始化了,把我們想要得到的對象存放到前面alloc的內存里。
- init {
if (self = [super init]) { //初始化父類,比如實例變量等等
self = ...;
}
return self;
}
- About alloc.
調用完alloc后,內存是直接映射到堆,還是只分配給了虛擬內存?
申請的這塊內存又是多大呢? - About memory.
iOS里的內存被分為:clean memory and dirty memory
Clean memory:可以被操作系統回收,在閃存中有備份,可以再次讀取,主要包括 system framework,binary executable of your app, memory mapped files,code(代碼段)
Dirty memory:不可以被操作系統回收,所有非clean memory,包括 heap allocation(包括被分配了的堆空間), cashes, decompressed images,image cash - 內存之間的關系
物理內存:物理內存是真實插在板子上的內存,是多大就是多大,看機器配置的時候,看的就是這個內存。所謂升級就是可以外擴內存條。
虛擬內存層面:virtual memory = clean memory dirty memory.
物理內存層面:resident memory = dirty memory clean memory that loaded in physical memory.
總結:virtual memory == (clean memory dirty memory) > resident memory > dirty memory。
我們的程序很多時候會因為系統的內存不足而被殺死.