写这篇文章是因为看一篇技术贴,分析Java和操作系统交互细节,其中提到一句话,使我产生好奇
RAM 是随机访问内存,就是给一个地址就能访问到数据,而磁盘这种存储媒介必须顺序访问
我脑海里第一反应,内存确实是能够根据内存地址直接查询相应位置存储的数据,但是磁盘不是也支持随机读写吗?只是随机读写比顺序读写的性能要差很多。
带着这个疑问,Google了一下,得到以下答案:Does a HDD use random read/write or sequential read/write?
简单来说,是要看random是怎么定义的,也就是文章标题中什么是随机读写,用更准确的词来描述内存访问应该是”Arbitrary Access”, 表示内存地址可以任意的进行访问。
而对于磁盘来说,这一点是做不到的,他即使知道数据的准确位置,他在访问相应的数据的时候,还是要寻道,倒带,才可以访问到指定存储点。
所以从物理存储的角度来说,是磁盘是顺序读写的,从程序角度来说磁盘是能够随机读写的。
以上的这种情况是针对HDD而言,因为HDD是机械磁盘,定位数据需要经过寻道和旋转,才能定位到要读写的数据块,而ssd是通过维护的mapping table直接计算即可。读取数据时HDD的速度取决于旋转速度,而SSD只需要加电压读取数据,所以SSD是具备了”Arbitrary Access”的能力。
SSDs get away with being so fast largely is down to the fact SSDs can directly address locations within their own storage. There’s no read/write heads that have to physically move there, the addressing lines just have to combine to activate the right cell and off the data goes. The three biggest “bottlenecks” at that point are the protocols used on the bus the drive is connected to (SATA vs. NVMe.), the filesystem in use (Some filesystems are better designed for flash than others.), and how quickly the CPU, memory, and the drive can navigate their way through the filesystem’s data structures to locate actual data.
另一个解释也比较经典
A random-access memory device allows data items to be read or written in almost the same amount of time irrespective of the physical location of data inside the memory. In contrast, with other direct-access data storage media such as hard disks, CD-RWs, DVD-RWs and the older magnetic tapes and drum memory, the time required to read and write data items varies significantly depending on their physical locations on the recording medium, due to mechanical limitations such as media rotation speeds and arm movement.
补充阅读
http://oserror.com/backend/ssd-principle/ SSD VS HDD
https://www.online-tech-tips.com/computer-tips/sata-3-vs-m-2-vs-nvme-overview-and-comparison/ sata vs NVMe
https://www.quora.com/Does-a-HDD-use-random-read-write-or-sequential-read-write
https://www.zhihu.com/question/357903826
https://zhuanlan.zhihu.com/p/40497397