简体   繁体   中英

Array vs Matrix in terms of memory in Java

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.

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.

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.

Edit: The array and matrix both are made of int primitives.

The array is array[25] and the matrix is matrix[5][5].

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).

I am talking about 16GB of RAM usage, and 25 million nodes.

I made this method, to clone arrays:

public int[] cloneArray(int[] array){
    int i = 0;
    int[] clone = new int[array.length];
    while (i < array.length){
        clone[i] = array[i];
        i++;
    }
    return clone;
}

Similar methods were made, to clone matrixes, and the objects themselves.

Edit: After finding out about the existence of a profiler, I made a check. Here is a screenshot of the results: Java Profiler统计信息

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).

So, in the profiler, we can see almost 20m states, which are the generated nodes. Each state contains 1 array and 1 matrix.

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.

Am I making sense? Is this interpretation accurate?

Here is a dropbox link, so you can check the full resolution image: https://www.dropbox.com/s/7wxz8vch1wnrsyr/Untitled.png?dl=0

Your question may suggest hidden problem in your code rather then "out of memory problem". the heap memory is not finish so fast , you need your code to be extremely heavy in order to get there.

still, I'll dare to say that changing 2 dimensional matrix into an array wouldn't change the memory usage much. speaking on which - the 2 most common ways to implement higher-dimensions arrays (2 and above) are 1) slice it to one dimension array, then use the formula :

arr[a][b].. = arr[a+b+..]

2) use pointers to pointers , then you get an array of pointers , which points to another array of pointers and so on until the final level which are real objects

this said , (again , with dare) , Java may already slice the matrix into one dimension array behind the scenes.

any way , I highly suspect you have memory leak in your code , or not-ending-recursion, or a combination of the above . try to see you're not there before trying to implement what you suggested.

Here are a couple of examples:

int[] array = new int[25];

int[][] matrix = new int[5][5];

The space occupied by the array is:

  • 25 x 4 byte ints (the array contents)
  • 12 bytes of object header for the array
  • total 112 bytes

A 2D int matrix in Java is actually an array of arrays, so the space occupied by the matrix is

  • (5 x 4 byte ints + 12 bytes of array header) x 5.
  • 5 x 4 byte references + 12 bytes of array header
  • total 192 bytes

(The above assumes a 32 bit JVM, and typical array header sizes. Those are platform specific assumptions, but for any JVM platform you should be able to tie them down with specificity. And for Oracle HotSpot / OpenJDK JVMs since Java 6, the source code is available for anyone to see.)

Note of course that as the arrays / matrices get larger, the relative saving for an int[N^2] versus an int[N][N] becomes smaller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM