Class CombinedMemoryModel<E>

java.lang.Object
net.algart.arrays.AbstractMemoryModel
net.algart.arrays.CombinedMemoryModel<E>
All Implemented Interfaces:
MemoryModel

public final class CombinedMemoryModel<E> extends AbstractMemoryModel

The memory model allowing to create combined arrays: special kind of AlgART arrays, that store an array of Java objects with minimal amount of memory, namely in one or several another "parallel" arrays. A set of these arrays is named "the internal storage" of the combined array.

There is an essential problem with storing large arrays of small objects in Java language. For example, let a point is described by 2 integer values: x and y, and we need to store 10 million points. In C++ or Pascal language, we can create an array of 10 000 000 structures (struct or record keyword), and this array occupies ~80 MB memory (4 bytes per every integer).

Unfortunately, the only simple way to store 10 million points in Java is usage of Java array (or collection) containing instance of Java-class, such as

 class Point {
     int x;
     int y;
 }

For example,

     Point[] points = new Point[10000000];
     for (int k = 0; k < points.length; k++)
         points[k] = new Point();

Such an array occupies more than 200 MB and is created very slowly (several seconds on CPU P-IV 2 GHz, under Java 1.5). The reason is creating separate object for every point and saving a pointer to it in Java array. Only array of primitive types work efficiently in Java.

The arrays created by this memory model ("combined" arrays) allow a solution of this problem. This memory model stores an array of any Java objects in one or several "parallel" another AlgART arrays - typically, wrappers for Java-arrays of primitive types (created by SimpleMemoryModel) or for ByteBuffer objects (created by LargeMemoryModel). This array is created quickly, does not make a difficult problem for future garbage collection and occupies only necessary amount of memory.

However, in many situations, combined arrays work slower, than simple array of references as listed above. In particular, an access to stored elements of combined arrays are usually slower in several times, than usage direct references array or standard ArrayList. Usually, you need combined arrays if you should save occupied memory or quickly create large array.

The only thing that you should provide to use this class is implementing CombinedMemoryModel.Combiner interface or its inheritors CombinedMemoryModel.CombinerInPlace, CombinedMemoryModel.BufferedCombiner, which "tell" how to store (or load) one object in (from) a set of AlgART arrays. The CombinedMemoryModel.CombinerInPlace interface allows to quite eliminate usage of Java heap while working with combined array; the CombinedMemoryModel.BufferedCombiner interface allows to optimize block access to the combined array.

Below are the main features of arrays created via this memory model.

This class is immutable and thread-safe: there are no ways to modify settings of the created instance.

Author:
Daniel Alievsky