Resource Loading Percentage Formula [SOLVED]

Hello JGO, since my last help topic (Where I couldn’t remember how to divide properly…) i’ve continued to work on a “Loader” for my project :slight_smile:

The issue I’m having is once my ImageIcon[] array’s size hits a certain cap my percentage formula fails.
(Percentage formula meaning what percentage out of 100 is increased per resource loaded)

Here is how I’m “Test Loading” a ImageIcon[] array:


		imgs1 = new ImageIcon[2000000]; // if array[] size is > 2000000 + (another zero) formula fails.
		int objects2Load = imgs1.length;
		double increment_formula = totalPercent / objects2Load;
		boolean accurate_formula = Boolean.valueOf((increment_formula * objects2Load) == totalPercent);
		print("At this rate (Loading " + objects2Load + " objects).");
		print("Each object will increment the % by " + increment_formula);
		if (!accurate_formula) {
			System.err.println("Formula is not accurate");
			System.exit(0);
		}
		print("Formula's accurate!");
		sleep(1000);

		/* Loading process */
		while (percent < totalPercent) {
			for (int i = 0; i < imgs1.length; i++) {
				imgs1[i] = loadSprite("tiles/floor/tile_" + MathUtils.random(5) + ".png",
						increment_formula, i);
			}
		}

The above code (Loading 2000000 ImageIcons) works properly, however if I add 1 more 0 “Zero” to the ImageIcon[]'s size my percentage formula does not function :frowning:

Here is the method LoadSprite(String, int, int) for those who were wondering:


	private ImageIcon loadSprite(String path, double percent_increment, int imgCount) {
		percent += percent_increment;
		progressBar.setValue((int) percent);
		System.out.println("Image "+imgCount+" loaded"); // for debugging.
		return new ImageIcon("res/sprite/" + path);
	}

I believe this has to do with the data type I am using although I am not 100% sure / confused.

If somebody could help me that would be great :>
Or at least push me in the right direction.

Solved, apparently I can’t allocate that many ImageIcons on startup T_T (For testing purposes)

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

So the reason it was saying my formula was inaccurate was because it wasn’t printing out the exception.
(Thus the ImageIcon[] array was never initialized)

I think there is a serious problem if you are loading 20,000,000 image icons into memory. Why do you need more than 5?

To get the percentage:

for (int i = 0; i < imgs1.length; i++) {
    float p = i / (float)imgs1.length * 100f;
}

That would be a serious issue if I were truly doing that in my project yes lol.
However in this case 20,000,000 was just a number I popped in during Testing :>