4

I was trying to implement my own ArrayList class for educational purposes and when I need to make it grow I need to copy the contents of the old small array into the new bigger one.

Doing this with a for loop is very inefficient and would require O(n) time depending on the size of the array to be copied. Luckily Java has the System.arraycopy() function which I suspect doesn't use a for loop but copies the entire array at once taking way less time.

However is it possible for myself to do these kind of memory copies or is this so deeply buried that only the java compiler can do this?

P.S there are a lot of functions that I don't know how to implement and seem to work using magic System.out.println() for example or sockets.

For clarification I just want to know how i can do these kind of omptimized memory managments things myself.

Paul Boon
  • 143
  • 1
  • 12
  • 5
    I don't think it's possible to copy an array in O(1) time. – shmosel Feb 09 '16 at 09:31
  • 1
    Copying an array would still depend on the size since bytes need to be moved around, so even `System.arraycopy()` would be O(n) - probably just faster by some factor due to the missing loop. The question would be: if it is for educational purposes does performance play that big a role? – Thomas Feb 09 '16 at 09:32
  • well okay not O(1) time but atleast more efficiently than a for loop – Paul Boon Feb 09 '16 at 09:34
  • 1
    If your educational task is to create your own ArrayList, why don't you use `System.arraycopy()` internally? IMO there's no need to do everything yourself, especially not the very low level stuff. – Thomas Feb 09 '16 at 09:35
  • Do you want to implement the method using native c functions like the real one, or recreate a similar method using normal java? – Ferrybig Feb 09 '16 at 09:36
  • i do use system.arraycopy but i just want to know if i can do these kinds of optimized memory managment things myself – Paul Boon Feb 09 '16 at 09:36
  • I would prefer doing the memory management with java if possible but if not i would also like to know how to efficiently copy an array in C – Paul Boon Feb 09 '16 at 09:40
  • There might be some highly optimized assembler code in it, which of course has way less overhead (if any). In Java, you are stuck with the tools the language gives you, unless you want to implement some native library yourself. But then your app is no longer platform independent. – Vlasec Feb 09 '16 at 10:02
  • if the answer is that it's just not possible, can someone respond to this question as an answer instead of a comment so i can accept it – Paul Boon Feb 09 '16 at 10:06

1 Answers1

4

In java.lang.System, you can see declaration, but no source code:

public static native void arraycopy(Object src,  int  srcPos,
                                    Object dest, int destPos,
                                    int length);

That is because it's a native method, implemented in a library provided by JVM. The implementation can differ between JVMs and its binaries differ between platforms as it needs to be compiled for each.

You can write your own native libraries. To achieve the optimization level you want, you need to be proficient with some lower level language like C or Assembler. With higher languages will probably hit the same barrier as in Java, since you usually don't have direct access to memory in those.

A downside of writing your own native library is that your application will no longer be platform independent, because unlike System, the compiled library you need for it to run is not included in JVM.

Vlasec
  • 5,500
  • 3
  • 27
  • 30