Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:39:12+00:00 2026-06-15T18:39:12+00:00

As we know Java always initialises arrays upon creation. I.e. new int[1000000] always returns

  • 0

As we know Java always initialises arrays upon creation. I.e. new int[1000000] always returns an array with all elements = 0. I understand that it’s a must for Object arrays, but for primitive arrays (except may be Boolean) in most cases we don’t care about the initial values.

Does anybody know a way to avoid this intialization?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:39:13+00:00Added an answer on June 15, 2026 at 6:39 pm

    I’ve done some investigation. There is no legal way to create uninitialized array in Java. Even JNI NewXxxArray creates initialized arrays. So it is impossible to know exactly the cost of array zeroing. Nevertheless I’ve done some measurements:

    1) 1000 byte arrays creation with different array size

            long t0 = System.currentTimeMillis();
            for(int i = 0; i < 1000; i++) {
    //          byte[] a1 = new byte[1];
                byte[] a1 = new byte[1000000];
            }
            System.out.println(System.currentTimeMillis() - t0);
    

    on my PC it gives < 1ms for byte[1] and ~500 ms for byte[1000000]. Sounds impressive to me.

    2) We don’t have a fast (native) method in JDK for filling arrays, Arrays.fill is too slow, so let’s see at least how much 1000 copying of 1,000,000 size array takes with native System.arraycopy

        byte[] a1 = new byte[1000000];
        byte[] a2 = new byte[1000000];
        for(int i = 0; i < 1000; i++) {
            System.arraycopy(a1, 0, a2, 0, 1000000);
        }
    

    It is 700 ms.

    It gives me reasons to believe that a) creating long arrays is expensive b) it seems to be expensive because of useless initialization.

    3) Let’s take sun.misc.Unsafe http://www.javasourcecode.org/html/open-source/jdk/jdk-6u23/sun/misc/Unsafe.html. It is protected from external usage but not too much

        Field f = Unsafe.class.getDeclaredField("theUnsafe");
        f.setAccessible(true);
        Unsafe unsafe = (Unsafe)f.get(null);
    

    Here is the cost of memory allocation test

        for(int i = 0; i < 1000; i++) {
            long m = u.allocateMemory(1000000);
        }
    

    It takes < 1 ms, if you remember, for new byte[1000000] it took 500ms.

    4) Unsafe has no direct methods to work with arrays. It needs to know class fields, but reflection shows no fields in an array. There is not much info about arrays internals, I guess it is JVM / platform specific. Nevertheless, it is, like any other Java Object, header + fields. On my PC/JVM it looks like

    header - 8 bytes
    int length - 4 bytes
    long bufferAddress - 8 bytes
    

    Now, using Unsafe, I will create byte[10], allocate a 10 byte memory buffer and use it as my array’s elements:

        byte[] a = new byte[10];
        System.out.println(Arrays.toString(a));
        long mem = unsafe.allocateMemory(10);
        unsafe.putLong(a, 12, mem);
        System.out.println(Arrays.toString(a));
    

    it prints

    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    [8, 15, -114, 24, 0, 0, 0, 0, 0, 0]
    

    You can see thay array’s data are not initialized.

    Now I’ll change our array length (though it still points to 10 bytes memory)

        unsafe.putInt(a, 8, 1000000);
        System.out.println(a.length);
    

    it shows 1000000. It was just to prove that the idea works.

    Now performance test. I will create an empty byte array a1, allocate a buffer of 1000000 bytes, assign this buffer to a1 an set a1.length = 10000000

        long t0 = System.currentTimeMillis();
        for(int i = 0; i < 1000; i++) {
            byte[] a1 = new byte[0];
            long mem1 = unsafe.allocateMemory(1000000);
            unsafe.putLong(a1, 12, mem);
            unsafe.putInt(a1, 8, 1000000);
        }
        System.out.println(System.currentTimeMillis() - t0);
    

    it takes 10ms.

    5) There are malloc and alloc in C++, malloc just allocates memory block , calloc also initializes it with zeroes.

    cpp

    ...
    JNIEXPORT void JNICALL Java_Test_malloc(JNIEnv *env, jobject obj, jint n) {
         malloc(n);
    } 
    

    java

    private native static void malloc(int n);
    
    for (int i = 0; i < 500; i++) {
        malloc(1000000);
    }
    

    results malloc – 78 ms; calloc – 468 ms

    Conclusions

    1. It seems that Java array creation is slow because of useless element zeroing.
    2. We cannot change it, but Oracle can. No need to change anything in JLS, just add native methods to java.lang.reflect.Array like

      public static native xxx[] newUninitialziedXxxArray(int size);

    for all primitive numeric types (byte – double) and char type. It could be used all over the JDK, like in java.util.Arrays

        public static int[] copyOf(int[] original, int newLength) {
            int[] copy = Array.newUninitializedIntArray(newLength);
            System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
            ...
    

    or java.lang.String

       public String concat(String str) {
            ...   
            char[] buf = Array.newUninitializedCharArray(count + otherLen);
            getChars(0, count, buf, 0);
            ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As we all know java allows us to use byte array as a buffer
I always come across articles which claim that Java is interpreted. I know that
I have always liked the documentation on Java APIs, generally speaking, but I know
As we all know Java program will start executing from the public static void
I'm new to Scala and don't know Java. I want to create a jar
Possible Duplicate: Java random always returns the same number when I set the seed?
as I know, Java always invoke a method by pass-by-value. but I see the
As I'm sure you all know Java enforces a very strict pattern for error
In Java, we can always use an array to store object reference. Then we
I would like to know how to express OR in Java . I always

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.