Search code examples
javamultidimensional-arrayperformancememory-efficient

Java Array Efficiency


I am not 100% sure of the mechanism in action so I decided to post here for further clarifications.

I am doing a project that should handle large amounts of data in Java (it has to be Java). I would like it to be as efficient as possible. By efficient I mean that memory and speed calculations should come in first and readability should come in second.

Now I have two ways to store my data: create one array of MyObject

1) MyObject[][] V = new MyObject[m][n]

Or create two arrays of int:

2) int[][] V = new int[m][n]

3) int[][] P = new int[m][n]

Clearly MyObject contains at least two fields and some methods. Now I notice that while looping over the MyObject array to assign values I have to call new or else I get a null pointer exception. This means that the new in line 1 didn't suffice. Is this a more expensive operation than, for sake of argument, P[i][j]=n, considering that arrays are also objects in Java?


Solution

  • Is this a more expensive operation than, for sake of argument, P[i][j]=n, considering that arrays are also objects in Java?

    In the first case you create an array object which is to store other objects of type array. Both the array object and the objects that are to be stored in the array need to be instantiated meaning that you will need m * n + 1 object instantiations and also (m * n + 1) * objectSize memory consumption.

    In the second case you only have to instantiate the array object; int primitives are not objects so this should me more faster and also more memory efficient since and Object memory size is several times larger than that of an int. Here you basically have 1 object instantiation and (m * n) * intSize + objectSize memory consumption.

    Another reason for using primitives is the fact that when used as local variables they are kept on the stack; you will probably use intermediate local variables inside a method before storing the computed value in the array and the allocation/deallocation time for the memory of these variables is several times higher than that of an object which lives on the heap.