• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

System.arrayCopy

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hello all -

I need hep in understanding 2 things -

1)I was looking at the source code of the Arrays.copyOf and seems like it uses System.arrayCopy.

but this is what is declared in System.arrayCopy -



What is happening here? I do understand that Java is relying on the native calls to copy teh array but jsut interested in details.

2 ) the other question is there are 2 ways of deleting the file - example if you want to remove a file, you do it by calling file.delete() or Runtime.getRuntime().exec("rm filename"); performance wise which is better? I was told that using file io is better but I dont understand how.

Thanks.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About 2: Even if there is a very small performance gain I would never use your second method. You're depended on the OS, need to read the streams of the process and I can probably think of another 200 reasons why this is a bad idea.
 
Sheriff
Posts: 27456
88
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two ways of getting to the other side of the street. You could walk across the street. Or you could build a springboard on one side and install a net on the other side and hire a few people to make sure that when you launched yourself from the springboard, the net would actually catch you.
 
Hari priya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for responses folks.

We will never use direct System calls in Java but for understanding sake...as I was asked a question something similar to this in performance aspects.

BTW - is it true that there will be better performance using File I/O and NOT with using System calls? I never understood how...
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hari priya wrote:What is happening here? I do understand that Java is relying on the native calls to copy teh array but jsut interested in details.


I can't be sure without looking into the JVM source code, but my guess is that the JVM is using some form of low-level memory copying that you can't do in Java. This will make the call faster.

Hari priya wrote:BTW - is it true that there will be better performance using File I/O and NOT with using System calls? I never understood how...


A pure system call (as in a native method) will most likely be the most efficient. However, that's probably what File.delete() already uses. Using a system call by using Runtime.exec() first needs to create a new process, then read the results from its streams (or the process may hang). Both are quite inefficient compared to calling a single method that already uses native calls.

For your information, I checked the source code of File.delete() (available in file src.zip inside your JDK root folder). It calls fs.delete(this), with fs being an instance of a sub class of non-public class FileSystem. For Windows the implementation of this method uses a private native method. So my assumption that File.delete() uses a native call is correct, at least on Windows.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hari priya wrote:...example if you want to remove a file, you do it by calling file.delete() or Runtime.getRuntime().exec("rm filename"); performance wise which is better?


To be honest, I wouldn't spend too much time worrying about performance; at least at this stage. Far better is to think about clarity; and what would you prefer to see:
file.delete();
or
Runtime.getRuntime().exec("rm " + file.getAbsolutePath());
(which is in fact what you would need)?

What are the chances of making a mistake with the first and the second?
Which is easier for the poor soul that has to debug your program after you're gone?

Read the quote at the bottom of Wouter's post; it's very good. You might also look at mine. And if you're still not convinced, have a look at this. I don't always agree with Brian Goetz, but he's an industry guru for a reason; and in this case, he's spot on.

Winston
 
The only thing that kept the leeches off of me was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic