-1

Is it possible to avoid array zeroing/initialization in Java?

I need to quickly allocate fresh byte arrays of fixed length that will completely be filled with predefined data. Therefore, I don't need the JVM to automatically zero the array on instantiation and I certainly don't mind if the array contains junk. All I need is constant time array allocation, which unfortunately becomes O(n) due to the mentioned zeroing issue.

Would using unsafe help?

maxim1
  • 53
  • 5
  • 1
    You could do it like netty, and do manual pooling & reference counting http://netty.io/4.0/api/io/netty/buffer/PooledByteBufAllocator.html + http://netty.io/4.0/api/io/netty/util/ReferenceCounted.html#release() - the duplicate isn't obvious (and I don't think a good duplicate) but if you click through a bit you could have found in like http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/ the part about "Interesting use cases Avoid initialization" - edit: hmm not in there, but in http://stackoverflow.com/a/13785284/995891 – zapl Jun 11 '16 at 16:55
  • Another thing you could consider if you just need an array for temporary purposes, have a `ThreadLocal` store them. Gives you one per thread and is safe and fast as long as you use thread pools. – zapl Jun 11 '16 at 17:02

3 Answers3

1

JVM always initializes arrays. But you can reuse the same array and it will be initialized once.

Argb32
  • 1,365
  • 8
  • 10
  • Sadly I can't because these arrays are held on to for an undefined period of time by an asynchronous networking library. Reusing them results in overwriting partially unsent messages. That's why I was hoping for a solution that would quickly allocate new arrays without zeroing them. – maxim1 Jun 11 '16 at 16:40
  • If you can know when the library is done with the buffer you can use array pooling. I.e. create a pool of arrays and give/return them from the pool. – Argb32 Jun 11 '16 at 16:49
1

The class sun.misc.Unsafe is officially undocumented.

From http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/

Avoid initialization

allocateInstance method can be useful when you need to skip object initialization phase or bypass security checks in constructor or you want instance of that class but don't have any public constructor.

Some resources indicate its removal from Java 9.

mjn
  • 36,362
  • 28
  • 176
  • 378
  • How do you allocate an on-heap array with a predefined length? I haven't found a working solution. – maxim1 Jun 11 '16 at 16:48
0
  1. What kind of application do you have that array initialization became a performance bottleneck?
  2. In response to "Sadly I can't because these arrays are held on to for an undefined period of time by an asynchronous networking library. Reusing them results in overwriting partially unsent messages.": Then use a pool and reuse only arrays that are not currently in use. You will have to manage that, though. So, is array creation really that much of an issue?
Silverclaw
  • 1,316
  • 2
  • 15
  • 28