0

I am facing challenging memory issues in my own application. I want to tackle a memory leakage problem, so instead of creating too many objects and arrays, I want to reuse the last allocated memory (using a pool of objects and arrays).

In one of my scenarios, I want to shift cells of an allocated array to the right for the specific length. For this, I implement the following simple solution:

private void shiftRight(int length) { for (int index = size + length - 1; index >= length; index--) { bytes[index] = bytes[index - length]; } } 

As I google for this problem, I found that I can use System.arraycopy instead of my simple solution.

System.arraycopy(bytes, 0, bytes, length, size); 

But I am worry about the performance of System.arraycopy. As mentioned in the documentation of this method:

If the src and dest arguments refer to the same array object, then the copying is performed as if the components at positions srcPos through srcPos+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destPos through destPos+length-1 of the destination array.

This method uses a temporary array to copy from src to dest. I think this method causes a new performance issue by creating too many arrays in high transaction systems.

Could you please discuss about these two solutions?

Thanks in advance.

4
  • 3
    Could you just try it and see if the performance is acceptable? Commented Jun 19, 2019 at 12:16
  • Implement and test the solution under high load has time cost for me. so, based on documentation, it seems to have performance issue. Commented Jun 19, 2019 at 12:19
  • 1
    Is this any help to you? stackoverflow.com/questions/18638743/… Commented Jun 19, 2019 at 12:23
  • 2
    "I want to tackle a memory leakage problem ..." - If you really mean memory leak, then (IMO) you are going about solving the problem the wrong way. Use a memory profile to track down the source of the leak. It is much less work than rewriting your code to minimize object allocation. Commented Jun 19, 2019 at 13:18

1 Answer 1

3

The key words in the sentence there are "as if". It doesn't actually use a temporary array. The description is simply saying that it works as if there was one (in other words, it doesn't overwrite things it should not).

You might like to compare performance of System.arraycopy vs the for loop as I believe I saw somewhere that the for loop can be quicker for short arrays.

See Is Java's System.arraycopy() efficient for small arrays?

Sign up to request clarification or add additional context in comments.

10 Comments

We are not 100% sure that it doesn't create a temporary array. All we know is that it's a native method, but there's nothing to stop the native implementation from creating a temporary array.
If we examine the source code of the native method (not hard!), we can be 100% sure. And we can be 99.9% sure without checking, because an implementation that did use a temporary array would have been optimized / fixed > 20 years ago. (Unless you are talking about an experimental JVM ... written by someone who didn't care much about performance.)
@maaartinus normally intrinsic == native.
@rghome AFAIK this is no more true. "native" means calling some non-java code and losing some tens of instructions on it, while "intrinsic" means special handling doing whatever the JIT sees fit, possibly without any call overhead. A good example for this is Long.rotateRight which takes a single instruction on current CPUs, even in Java. This can't be optimized using a JNI call as any call is much more expensive than the non-optimized shift-shift-or implementation.
@maaartinus - My understanding is that native and 'intrinsic' are orthogonal. But it depends on whether you mean "native" to mean implemented in native code or declared with the native keyword.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.