fwrite为什么会导致内存一直增加

转载出处:http://bbs.csdn.net/topics/260006635

(1)写操作的过程空间

用户空间(user-space) ¦  内核空间(incore-space)  ¦  buffer cache ¦ disk ¦   
1.用户空间:fwrite()内申请的缓存内存空间。因为用户可以通过setbuffer()等函数对其进行设定; 
2.内核空间:write()内申请的缓存内存空间。这个是OS为写操作申请的内存,对用户来说不可见。 
3.buffer cache: 这个东东是造成我们内存减少的主要原因。不是我们的过错,是OS的特性;
4.disk:这个就不说了。  

(2)同步问题
即使我们在fwrite()、write()后面使用了sync(),fsync()等同步操作,在内存中也会保留buffer cache的,所以内存不会减少…

几个同步函数
1.fflush():The function fflush() forces a write of all user-space buffered data for the given output or update stream
via the stream’s underlying write function. The open status of the stream is unaffected.
我的理解是该函数将fwrite()里面的缓存内容强行压入底层(内核空间?cache?disk?)。至于压倒哪里就不明白了,我猜是压入到了
write()的内核空间。
2.fsync(): fsync() transfers (“flushes”) all modified in-core data of (i.e., modified buffer cache pages for) the file
referred to by the file descriptor fd to the disk device (or other permanent storage device) where that file
resides.
我的理解是该函数将write()里面内核空间(修改的部分)压入到磁盘设备。但是这里的buffer cache pages又不明白和内核空间的关系了

猜你喜欢

转载自blog.csdn.net/lile777/article/details/80978718
今日推荐