Java Buffer Types vs Native Arrays: Which Is Faster?
The article compares Java native arrays and Buffer types such as IntBuffer, showing through benchmarks that native arrays are typically over four times faster than Buffers, and discusses the memory management, performance trade‑offs, and practical implications of using each abstraction.
When developing in C you must manually allocate and free memory, a process prone to errors, whereas languages like Java manage memory automatically through garbage collection; memory is allocated as needed and reclaimed when no longer referenced, offering speed and safety at a cost.
Java’s native arrays (e.g., int[] ) are allocated on the Java heap and are managed and garbage‑collected by the JVM.
Java also provides Buffer types such as IntBuffer , which are higher‑level abstractions that can be backed by native arrays or other data sources, including off‑heap memory, allowing developers to reduce reliance on the Java heap.
Compared with native arrays, Buffers incur some performance loss; while they are preferable to streams like DataInputStream , in the author’s experience they are slower than raw arrays.
For example, one can create an array of 50,000 integers with new int[50000] or an IntBuffer with IntBuffer.allocate(50000) , the latter essentially wrapping a heap‑allocated array.
Although high‑level abstractions can sometimes be free of performance penalties, this is an empirical question and should not be assumed.
The author performed a simple test adding 1 to each element of both structures.
for (int k = 0; k < s.array.length; k++) { s.array[k] += 1; }
for (int k = 0; k < s.buffer.limit(); k++) { s.buffer.put(k, s.buffer.get(k) + 1); }
Running the benchmark on a desktop (OpenJDK 14, 4.2 GHz Intel CPU) produced the results shown below:
The test demonstrates that native arrays are more than four times faster than IntBuffer s.
If interested, readers can run the benchmark themselves using the code available at https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/tree/master/2020/11/30 .
The author’s view is that many Java optimizations for arrays do not apply to Buffer types, and the behavior of Buffers when mapped off‑heap can be problematic.
Buffers do not make native arrays obsolete, at least not in terms of performance.
Original English article with comments: https://lemire.me/blog/2020/11/30/java-buffer-types-versus-native-arrays-which-is-faster/
Reference reading includes additional articles on microservice resilience, Java JIT, .NET async/await, Redis performance, and cloud‑native architecture.
This translation was provided by 高可用架构; original technical content and architectural practice articles are welcome for submission via the public account menu “Contact Us”.
High Availability Architecture
Official account for High Availability Architecture.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.