Menu

I Threw Away 786 MB of Scratch Buffers
📰

I Threw Away 786 MB of Scratch Buffers

DEV Community: dotnet·Sukhpinder Singh·8 days ago
#u0SeczH4
#devtodotnet#devto#dev#buffersize#scratch
Reading 0:00
15s threshold

I had a message processor that unpacked each incoming record into a scratch buffer, read one value out, and moved on. The buffer was pure throwaway: fill it, peek at a byte, forget it. I wrote the obvious thing, new byte[4096] at the top of the loop, and shipped it. It worked. It was also quietly handing the garbage collector a short-lived 4 KB array every single iteration, and I didn't notice until I was staring at an allocation profile for something else and saw this loop sitting near the top.

Here's the loop, more or less:

for ( int i = 0 ; i < messages ; i ++) { byte [] scratch = new byte [ bufferSize ]; // 4096 bytes, every iteration Fill ( scratch , i ); sink += scratch [ i % bufferSize ]; }

Enter fullscreen mode

Exit fullscreen mode

Each scratch lives for a few microseconds and then becomes garbage.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.