Reputation: 5640
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.
Upvotes: 1
Views: 122
Reputation: 718768
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 compilation of the programs given the algorithm and the constraints.
Compilation makes no difference.
Memory size should make no difference. Or at least, it should not cause this kind of problem.
I suspect that what you are seeing is actually due to incorrect synchronization in a multi-threaded application. However, without seeing your code it is unlikely we can help. (And I suspect that your application is far too large for posting in an SO question.)
(Another theoretical possibility is that your application is "squashing" OutOfMemoryError
exceptions. But I can't imagine anyone doing something that stupid ...)
I have updated my question with examples.
They are not relevant. Please pay attention to what the Answers are saying. It is HIGHLY UNLIKELY that the problem is in the JVM or the Java runtime libraries. It is HIGHLY LIKELY that the problem is in your code. Without looking at the actual code, we can't be more specific.
Upvotes: 1
Reputation: 30088
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.
Upvotes: 3