Linux数据管理

what the application is seeing is a carefully controlled illusion(幻觉)

 在memory3实验中,实际分配到716MB内存空间,然后进程被杀死;
显然在初期,linux内核调用空闲的物理内存给用户程序。一旦物理内存都被占用,内

核会使用swap space。内核移动数据和程序在物理内存和swap space之间,这保证了每

次你读或写内存时,数据总是出现在物理内存中。在你访问之前,该位置已经被分配

好了。
        linux实现了一个基于需求页式虚拟内存系统(a demand paged virtual memory system)
所有的内存在用户程序看来都是虚拟的,也就是说在实际物理内存空间中不存在用户

程序所访问的地址。
 linux把所有的内存划分为页,通常4096 bytes为一页。当一个程序试图访问内存

时,会先把虚拟地址转换到物理地址,当访问到的内存不是物理驻留空间,此时一张

page fault和控制信息被传递到内核。
 The Linux kernel checks the address being accessed and, if it’s a legal(合法的)address

for that program, determines which page of physical memory to make available

 最终,when the application exhausts(废弃) both the physical memory and the swap

space, or  when the maximum stack size is exceeded(溢出), the kernel finally refuses the request

for further memory and may  preemptively terminate the program.
 
 However, you must remember that allocating two blocks of memory won’t result in a single

continuously addressable block of memory
 通常,你调用malloc失败的原因可能不是由于内存不足,而是由于内存结构被

破坏而造成。


*. 滥用内存
memory4 显示“段错误”,为什么呢?这是Linux系统的保护机制
    Each running program on a Linux system sees its own memory map, which is different from every

other program’s. Only the operating system knows how physical memory is arranged and not only

manages it for user programs, but also protects user programs from each other.


*.空指针
 试图从NULL指针中写入数据,是非法的;
 试图从NULL指针中读取数据,返回()


*.释放内存
 linux内存管理系统会在程序结束后自动回收内存,当然在程序运行期间可能会

要求回收内存,请调用free()。
 当一个运行中的程序调用free()函数释放内存,其被释放的内存任然待在该进程

中,不过如果其未被利用的话,linux内存管理器将能够从物理内存中swap到交换空间

中.
 #include <stdlib.h>
 void free(void *ptr_to memory);

*.calloc和realloc
 realloc函数用来改变内存块的大小。realloc函数可能不得不移动数据来实现分配
,最重要的是确保一旦内存被realloc后,你最好用新分配的指针,千万不要试图用以前

的指针访问内存。

posted @ 2010-12-09 20:31  hungryMan  阅读(226)  评论(0编辑  收藏  举报