当前位置:网站首页>How much disk space does a file of 1 byte actually occupy
How much disk space does a file of 1 byte actually occupy
2020-11-06 21:04:00 【Zhang Yanfei Allen】
In the foreword 《 Whether a new empty file takes up disk space ? How much does it take 》 We learned about the disk overhead of an empty file . Today, let's think about another question , If we only write 1 Bytes , So the actual disk usage of this file is also 1 Bytes ?
see 1 Byte file
As before , Let's not talk about the principle , Just do it yourself .
# mkdir tempDir
# cd tempDir
# du -h
0 .
# touch test
# du -h
0 .
After creating an empty file in a directory , adopt du
The footprint of the folder as seen by the command has not changed . This is in line with our previous understanding , Because empty files only occupy inode. good , Let's modify the file , Add a letter
echo "a" > test
# du -h
4.0K .
After saving, check the space usage of the directory again . We found that the original 0 Increased to 4K. So , No matter how small the contents of the document , Even a byte , In fact, the operating system will also assign you 4K Of . Oh , Of course, we have to calculate the above mentioned inode And file names stored in the folder data structure . therefore , Don't maintain a lot of broken files in your system . File size , It takes up a lot of disks !
Notice that my experimental environment is in ext Under the file system . If it is xfs There may be some discrepancy in performance .
Keep talking about this 4K
And then linux Source code file fs/ext2/ext2.h About inode The definition of , We find the data node defined in the structure block Array :
struct ext2_inode {
......
__le32 i_block[EXT2_N_BLOCKS]; # An array that points to blocks that store file data
......
When a file has no data to store , This array is empty . And when we write 1 A few bytes later , The file system needs to apply for block To store , After the application , The pointer is in this array . Even if the file contains only one byte , It will still be assigned a whole Block, Because this is the smallest working unit of a file system . So this block How big is it ,ext You can go through dumpe2fs
see .
#dumpe2fs -h /dev/mapper/vgroot-lvroot
......
Block size: 4096
It's on my machine , One Block yes 4KB.
What to do if the content of the document is too large
I don't know if you pay attention to ,inode As defined in block How about the array size , Only EXT2_N_BLOCKS
individual . Let's look at the definition of this constant again , Find out it's 15, The definition in the relevant kernel is as follows :
#define EXT2_NDIR_BLOCKS 12
#define EXT2_IND_BLOCK EXT2_NDIR_BLOCKS
#define EXT2_DIND_BLOCK (EXT2_IND_BLOCK + 1)
#define EXT2_TIND_BLOCK (EXT2_DIND_BLOCK + 1)
#define EXT2_N_BLOCKS (EXT2_TIND_BLOCK + 1)
Just press the 4K Of block size Look at ,15 individual block Just enough to save 15*4=60K The file of . I believe you are not satisfied with the size of this file , You save one avi Big movies have to be on G 了 . that Linux How to achieve large file storage ? Um. , In fact, the definition process of macro above has already told you , It's just 12 Arrays are stored directly block The pointer , The rest is used for indirect indexing (EXT2_IND_BLOCK), Secondary indirect index (EXT2_DIND_BLOCK) And the third level index (EXT2_TIND_BLOCK).
such , The space that a file can use expands exponentially . When the files were small , They all use direct index , disk IO Less , Good performance . When the files are big , Visit one block It might have to be done three times first IO, Performance is a little bit slow , However, there are OS Page caching at level 、 Directory item cache bonus , It's OK .
Conclusion
File systems are managed in blocks , So no matter how small your file is , Even if it's only one byte , Will consume a whole block . This block size can be passed through dumpe2fs
Wait for the order to see . What if you want to change the size of this block ? I'm sorry , You can only reformat .
Development of hard disk album of internal training :
- 1. Disk opening : Take off the hard coat of the mechanical hard disk !
- 2. Disk partitioning also implies technical skills
- 3. How can we solve the problem that mechanical hard disks are slow and easy to break down ?
- 4. Disassemble the SSD structure
- 5. How much disk space does a new empty file take ?
- 6. Only 1 How much disk space does a byte file actually take up
- 7. When there are too many documents ls Why is the command stuck ?
- 8. Understand the principle of formatting
- 9.read How much disk does a byte of file actually take place on IO?
- 10.write When to write to disk after one byte of file IO?
- 11. Mechanical hard disk random IO Slower than you think
- 12. How much faster is a server equipped with a SSD than a mechanical hard disk ?
My official account is 「 Develop internal skill and practice 」, I'm not just talking about technical theory here , It's not just about practical experience . It's about combining theory with practice , Deepen the understanding of theory with practice 、 Use theory to improve your technical practice ability . Welcome to my official account , Please also share with your friends ~~~
版权声明
本文为[Zhang Yanfei Allen]所创,转载请带上原文链接,感谢
边栏推荐
- ES6 learning notes (5): easy to understand ES6's built-in extension objects
- Zero basis to build a web search engine of its own
- What knowledge do Python automated testing learn?
- An article will take you to understand CSS3 fillet knowledge
- CloudQuery V1.2.0 版本发布
- ES6 learning notes (4): easy to understand the new grammar of ES6
- JNI-Thread中start方法的呼叫與run方法的回撥分析
- C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
- ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
- Get twice the result with half the effort: automation without cabinet
猜你喜欢
ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
An article will introduce you to HTML tables and their main attributes
【学习】接口测试用例编写和测试关注点
实用工具类函数(持续更新)
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
Summary of front-end interview questions (C, s, s) that front-end engineers need to understand (2)
Small program introduction to proficient (2): understand the four important files of small program development
ES中删除索引的mapping字段时应该考虑的点
An article will take you to understand CSS alignment
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
随机推荐
An article will take you to understand SVG gradient knowledge
The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures
Markdown tricks
ES6 learning notes (3): teach you to use js object-oriented thinking to realize the function of adding, deleting, modifying and checking tab column
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
An article taught you to download cool dog music using Python web crawler
ado.net和asp.net的关系
Vue communication and cross component listening state Vue communication
Behind the first lane level navigation in the industry
Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
What are the common problems of DTU connection
Zero basis to build a web search engine of its own
JNI-Thread中start方法的呼叫與run方法的回撥分析
C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
CloudQuery V1.2.0 版本发布
How to hide part of barcode text in barcode generation software
image operating system windows cannot be used on this platform
A small goal in 2019 to become a blog expert of CSDN
hdu3974 Assign the task線段樹 dfs序
mongo 用户权限 登录指令