title: Array vs Matrix in terms of memory in Java
body: <p>I am working on a project that involves heuristics, and I built it in Java (Should have probably made it in C).
  I am running into problems with memory.</p>
  
  <p>My tree is built up with object nodes, and each object contains an array, a matrix, and three integers.
  I already cut down many other values, in order to try and save more memory space, however, it still isn't enough.</p>
  
  <p>So, I was thinking that I could also cut down the matrix, and transform it into an array.
  However, my whole project is built on coordinates, to reach a certain point in the matrix.
  So before I make any change, I would like to know how much (or not so much) this would affect memory usage.</p>
  
  <p>Edit: The array and matrix both are made of int primitives.</p>
  
  <p>The array is array[25] and the matrix is matrix[5][5].</p>
  
  <p>The matrix represents the board of the game, with information of whether the field is empty, or has a certain type of piece inside it (all int).</p>
  
  <p>I am talking about 16GB of RAM usage, and 25 million nodes.</p>
  
  <p>I made this method, to clone arrays:</p>
  
  <pre><code>public int[] cloneArray(int[] array){
      int i = 0;
      int[] clone = new int[array.length];
      while (i &lt; array.length){
          clone[i] = array[i];
          i++;
      }
      return clone;
  }
  </code></pre>
  
  <p>Similar methods were made, to clone matrixes, and the objects themselves.</p>
  
  <p>Edit:
  After finding out about the existence of a profiler, I made a check.
  Here is a screenshot of the results:
  <img src="https://i.stack.imgur.com/nD216.png" alt="Java profiler stats"></p>
  
  <p>I think these numbers make sense, because in the console, you can see nearly as many nodes that were counted, as you can see in the profiler, the states (in the console, "estados" is the pointer of the state that is currently being expanded).</p>
  
  <p>So, in the profiler, we can see almost 20m states, which are the generated nodes.
  Each state contains 1 array and 1 matrix.</p>
  
  <p>We can see 138m arrays, which divided by 6 equals 23m.
  And since a matrix is 5x5, then 5x23m of the arrays are contained in the matrix, and the other 23m are the arrays.</p>
  
  <p>Am I making sense? Is this interpretation accurate?</p>
  
  <p>Here is a dropbox link, so you can check the full resolution image:
  <a href="https://www.dropbox.com/s/7wxz8vch1wnrsyr/Untitled.png?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/7wxz8vch1wnrsyr/Untitled.png?dl=0</a></p>