In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "Java buffer type or native array which garbage collection is faster". In daily operation, I believe many people have doubts about which garbage collection is faster between Java buffer type and native array. Xiaobian consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java buffer type and native array which garbage collection is faster." Next, please follow the editor to study!
When developing in C language, memory must be allocated and released manually, which is an error-prone process. In contrast, later languages like Java usually manage memory automatically. Java relies on garbage collection. In fact, memory is allocated as needed, and then Java discovers which data is no longer accessed and reclaims the corresponding memory. The garbage collection process is fast and safe, but it is not free: despite decades of optimization, it can still cause some trouble for developers.
Java has native arrays (such as the int [] type), which are usually allocated on the Java heap. That is, they are allocated and managed by Java as dynamic data and garbage collected.
Java also has Buffer types, such as IntBuffer, which is a high-level abstraction that can be implemented either by native Java arrays or by other data sources, including Java out-of-heap data. Therefore, you can use the Buffer type to avoid relying too much on the Java heap.
Compared with native arrays, Buffer has some performance loss. I'm not saying Buffer is slow. In fact, if you choose between Buffer and DataInputStream, you should prefer the Buffer type. However, in my experience, Buffer is inferior to native arrays in terms of performance.
I can use new int [50000] or IntBuffer.allocate (50000) to create an array of 50000 integers. The latter should essentially create an array (on the Java heap) and then wrap it with the IntBuffer interface.
One possible intuition is that wrapping an array with an advanced interface should be free. Although it is true that high-level abstraction does not bring performance loss (and sometimes even performance improvement), it is an empirical question whether it will lead to performance improvement. You should never just assume that your abstraction is free.
Because I'm making a statement of experience, I can prove it with the simplest tests, such as adding 1 to each element in the array and IntBuffer.
For (int k = 0; k < s.array.contains; KBG +) {s.array [k] + = 1;} for (int k = 0; k < s.buffer.limit (); KBG +) {s.buffer.put (k, s.buffer.get (k) + 1);}
I got the following results on the desktop (OpenJDK 14core 4.2 GHz Intel processor):
In other words, in this test, the array is more than four times faster than IntBuffers.
If you are interested, you can also run the following benchmarks yourself.
Https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/tree/master/2020/11/30
In my opinion, many of Java's optimizations for arrays do not apply to Buffer types.
Of course, we don't know what happened when Buffer was mapped from outside the Java heap, and in my experience, it could be bad.
The Buffer type does not make native arrays obsolete, at least in terms of performance.
At this point, the study on "which garbage collection is faster, Java buffer type or native array" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.