I am creating a photo mosaic in Java. The inputs to it are the Target Image and a collection of tiles. Below is my algorithm for the same:
a. Read all the tiles from the directory and process* it. [Every tile is of the same dimension.]
b. Read the target image, break it into cells [cells are of dimensions of a tile.]
c. Process* all the cells.
d. For each cell:
d.1. Create a HashMap h [where key=euclidean metric, value=corresponding tile]
d.2. For each tile:
d.2.1 Calculate Euclidean metric.
d.2.2 add it to h.
d.3 Calculate min from h.
d.4 Add the min to an outputList
e. Create the image from list of images in the outputList.
The *process method takes in an image and creates an object of a class we defined called ImageDetails. So for every tile and cell that is processed, there is an ImageDetails object created which stores details like its RGB value and dimensions. There are 2 separate lists of objects: one for tiles and other for cells.
The problem is that there are about on average 300 tiles and as much as 50,000 cells (can be more than that too!). So while my program runs, it has these many objects in memory, besides the calculations and other ip/op operations it performs.
When I run this program on a machine with low resources (less available memory) the output image created is distorted. But when I run it on a machine with more available resources, it is perfect. I think it is because it is not able to hold all the objects in memory at once when there are no resources available. So I see an image which has misplaced tiles. But when it has enough memory, I see perfect output image.
What can I do to ensure that regardless of the available memory, I can retain the order of the elements added in the outputList so that I can see an image without distortions.
Thanks.
EDIT:
Below are the 2 images that are the output of the same program. the only difference is that they are run on different machines. Please help me in understanding the difference in the output of the programs given the algorithm and the constraints.


It probably doesn’t have anything directly to do with memory – either you have enough or you don’t.
What collection class are you using for outputList? ArrayList and LinkedList should both guarantee the order of the elements.