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.…