Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Performance/Memory Consumption: Class vs. Array

Out of interest: Recently, I encountered a situation in one of my Java projects where I could store some data either in a two-dimensional array or make a dedicated class for it whose instances I would put into a one-dimensional array. So I wonder whether there exist some canonical design advice on this topic in terms of performance (runtime, memory consumption)?

Without regard of design patterns (extremely simplified situation), let's say I could store data like

class MyContainer {
  public double a;
  public double b;
  ...
}

and then

MyContainer[] myArray = new MyContainer[10000];
for(int i = myArray.length; (--i) >= 0;) {
  myArray[i] = new MyContainer();
}
...

versus

double[][] myData = new double[10000][2];  
...

I somehow think that the array-based approach should be more compact (memory) and faster (access). Then again, maybe it is not, arrays are objects too and array access needs to check indexes while object member access does not.(?) The allocation of the object array would probably(?) take longer, as I need to iteratively create the instances and my code would be bigger due to the additional class.

Thus, I wonder whether the designs of the common JVMs provide advantages for one approach over the other, in terms of access speed and memory consumption?

Many thanks.

like image 272
Thomas Weise Avatar asked Jan 07 '23 10:01

Thomas Weise


1 Answers

Then again, maybe it is not, arrays are objects too

That's right. So I think this approach will not buy you anything.

If you want to go down that route, you could flatten this out into a one-dimensional array (each of your "objects" then takes two slots). That would give you immediate access to all fields in all objects, without having to follow pointers, and the whole thing is just one big memory allocation: since your component type is primitive, there is just one object as far as memory allocation is concerned (the container array itself).

This is one of the motivations for people wanting to have structs and value types in Java, and similar considerations drive the development of specialized high-performance data structure libraries (that get rid of unneccessary object wrappers).

I would not worry about it, until you really have a huge datastructure, though. Only then will the overhead of the object-oriented way matter.

like image 185
Thilo Avatar answered Jan 10 '23 01:01

Thilo