Buy real YouTube subscribers. Best price and warranty.
Get Free YouTube Subscribers, Views and Likes

The Cost of Memory Fragmentation

Follow
Hussein Nasser

Fragmentation is a very interesting topic to me, especially when it comes to memory.

While virtually memory does solve external fragmentation (you can still allocate logically contiguous memory in noncontiguous physical memory) it does however introduce performance delays as we jump all over the physical memory to read what appears to us for example as contiguous array in virtual memory.

You see, DDR RAM consists of banks, rows and columns. Each row has around 1024 columns and each column has 64 bits which makes a row around 8kib. The cost of accessing the RAM is the cost of “opening” a row and all its columns (around 50100 ns) once the row is opened all the columns are opened and the 8 kib is cached in the row buffer in the RAM.

The CPU can ask for an address and transfer 64 bytes at a time (called bursts) so if the CPU (or the MMU to be exact) asks for the next 64 bytes next to it, it comes at no cost because the entire row is cached in the RAM. However if the CPU sends a different address in a different row the old row must be closed and a new row should be opened taking an additional 50 ns hit. So spatial access of bytes ensures efficiency,

So fragmentation does hurt performance if the data you are accessing are not contiguous in physical memory (of course it doesn’t matter if it is contiguous in virtual memory). This kind of remind me of the old days of HDD and how the disk needle physically travels across the disk to read one file which prompted the need of “defragmentation” , although RAM access (and SSD NAND for that matter) isn’t as bad.

Moreover, virtual memory introduces internal fragmentation because of the use of fixedsize blocks (called pages and often 4kib in size), and those are mapped to frames in physical memory.

So if you want to allocate a 32bit integer (4 bytes) you get a 4 kib worth of memory, leaving a whopping 4092 allocated for the process but unused, which cannot be used by the OS. These little pockets of memory can add up as many processes. Another reason developers should take care when allocating memory for efficiency.

0:00 Intro
2:40 Memory Allocation
4:10 External fragmentation
9:00 Internal Fragmentation
11:30 Virtual Memory & Swap
15:20 Page Size
18:00 The genius of memcached
19:50 How CPU reads data from RAM
25:00 The cost of fragmentation in memory
29:00 MySQL 8.x regression
34:00 Summary


Discovering Backend Bottlenecks: Unlocking Peak Performance
https://performance.husseinnasser.com

Resources
https://bugs.mysql.com/bug.php?id=93734
  / 1730573712313741545  
https://pclt.sites.yale.edu/memoryan...

Fundamentals of Backend Engineering Design patterns udemy course (link redirects to udemy with coupon)
https://backend.husseinnasser.com'>https://backend.husseinnasser.com

Fundamentals of Networking for Effective Backends udemy course (link redirects to udemy with coupon)
https://network.husseinnasser.com

Fundamentals of Database Engineering udemy course (link redirects to udemy with coupon)
https://database.husseinnasser.com

Follow me on Medium
  / membership  


Introduction to NGINX (link redirects to udemy with coupon)
https://nginx.husseinnasser.com

Python on the Backend (link redirects to udemy with coupon)
https://python.husseinnasser.com

Become a Member on YouTube
   / @hnasr  

Buy me a coffee if you liked this
https://www.buymeacoffee.com/hnasr

Arabic Software Engineering Channel
   / @husseinnasser  

Members Only Content
   • Membersonly videos  



Backend Engineering Videos in Order
https://backend.husseinnasser.com'>https://backend.husseinnasser.com

Database Engineering Videos
   • Database Engineering  

Listen to the Backend Engineering Podcast
https://husseinnasser.com/podcast

Gears and tools used on the Channel (affiliates)

Slides and Thumbnail Design
Canva
https://partner.canva.com/c/2766475/6...


Stay Awesome,
Hussein

posted by armuguetz0v