Preloaders - Advantages and Disadvantages

Preloaders - advantages and disadvantages


Using a preloader is a lot like using percussive maintainance to repair a device:
If you know what you're doing you'll get a lot of astounished people asking "How did you repair the thing by hitting it with a sledge hammer??"
And anyone who doesn't know what they're doing wonders why everything broke apart on being hit.


Preloaders aren't miracle workers - and in a lot of cases they break or slow down your games. Not because they are buggy, but because you don't understand their limitations and use them in the wrong way.



This tutorial is intended to explain the basics behind the preloaders, so that you know how to use them and how NOT to use them.
Unfortunately that means that parts will be rather dry reading to explain the basics - but if you consider using a preloader you should read this before applying a sledgehammer to your game.
A hammer rarely speeds something up, it usually breaks it instead.



The most precious resource of the computer is the RAM. Early programmers spent month on applying every trick impossible to reduce their program's RAM usage - that was at a time when 64kb RAM was priced in the hundreds of dollars.
When the RAM became cheaper that was unfortunately abandoned and modern programmers often don't care about memory management - which makes the RAM expensive again, just at higher numbers.
Because of that no computer ever will have "enough" RAM, they can always use more to speed things up.

A side effect of that missing memory management is what is usually called a "memory leak". Some of you might remember from the early MV versions that there were many bug reports about "memory leaks", which is basically memory being filled without ever being re-used.
The memory leak is basically the program loading data into RAM to be used, without a care whether the computer has enough RAM or giving it back to other programs to be used, until the program lags and crashes from missing RAM.

And now you're probably beginning to understand why I have sometimes called preloader plugins to be "memory leaks by design", because that is exactly what they are programmed to do: load files into RAM without a care for the availability of said RAM.
And with the same risk of crashing if the data is not released when the RAM is needed for something else.


Now, if most programmers have abandoned memory management (especially hobbyists writing plugins that never heard the term), why don't the computers crash all the time with memory errors?
The reason for that is the garbage collector, a very limited form of automated memory management.
The garbace collector check the memory regularly (and every time it gets too full) for the oldest files (longest in memory) and releases them to free up the RAM again.


And that is where the first case of wrong use of preloaders happens:
How many of you have thought "Preloading speeds things up, so let's preload everything!!"
That can't be a problem because the preloader magically creates RAM for everything to fit, right?

No, not really. What really happens if you try to preload files three times of what fits into RAM is that the first files are kicked out by the garbage collector before the last file in the list even started loading, and all but the last few files will be gone by the time the player presses "new game".
You only slowed down your game start with loading files that are quickly removed again for absolutely no advantage.

"Preload on boot" only works for a very limited number of files that are constantly needed from the very beginning. Pictures of HUD elements on the main screen are good candidates for preloads, or pictures from an introduction and similar.
Everything else would long be removed from memory when it is finally needed.

And please never forget that storage/filesize is NOT RAM-Size, most pictures are stored with a compression rate of 50 or 100 or more, meaning that their size in RAM is fifty or a hundred ties of their size on harddrive.


Now you should also understand that the only way for a preloader to work correctly is if it is given the command to load something at a short time before the file is used, but not directly before that use.
If you give the command too early the garbage collector will have it removed and forces you to reload it on use.
If you give the command too late then the loading is not yet complete and the computer has to wait for loading on use again. Which is a lag that you want to prevent by preloading after all.




I haven't checked the code myself, but from what I heard the latest maker versions have a half-way decent preloader on the event interpreter.
Upon loading an event for execution, it checks the event for the show picture commands and preloads all pictures used in them, before starting processing the event with its first command.
Depending on what kind of event it is and how many pictures it contains that principle may still be lacking (for example if it is cycling though many large pictures for an animation), but it is at least trying to follow the best principles as to when to preload which resources and as such will speed up most events.


And that is what you need to do if you want to get improvements by any preloader:

1) identify resources that are too large to be loaded fast - you don't have to use a preloader for small icons that are loaded within microseconds. Parallax maps, large background pictures or videofiles are something else

2) look at when that resource needs to be available for use. It does not help to load it hours before that, the garbage collector would remove it long before it is used in that case.
And neither helps a preload command on the line before the resource is needed, because that preload command would get you nothing compared to directly loading the resource.

3) find a place in the events that is shortly before that moment, based on the size of the resource. If the computer would need three seconds to load the file that place is different than it would be if the file can be loaded in one second - or in ten seconds

4) place the command to preload there.



Any other form of preloader use - especially excessive use of preloading to the point of overloading your memory - will only cause negative effects.
And there is nothing that can change that.

For example another point is "Virtual memory" - computers can increase their physical RAM by adding "virtual memory" - but do you know how that works?
It means placing part of the memory on the harddrive, which is why the virtual memory is so much slower. It needs to be loaded back again from the harddrive to be accessed.
That is the opposite of the preload function, which means that if you use the preload to a limit that forces windows to create more virtual memory to compensate the demand, then you're working against the preloader advantage.

So another problem of using preloaders is to know how much RAM is available to keep the preload limits within that limit.
And since you don't know how much RAM all your players will have, you cannot just base everything on the performance of your own computer (which is often above average for the interested developer).



I hope that these background informations will make people think before adding preloader plugins to their games, and especially before configuring them.
A preloader can help and speed things up, but only if you as the developer know what it is doing, and not automatically.
The garbace collector check the memory regularly (and every time it gets too full) for the oldest files (longest in memory) and releases them to free up the RAM again.
...
No, not really. What really happens if you try to preload files three times of what fits into RAM is that the first files are kicked out by the garbage collector before the last file in the list even started loading, and all but the last few files will be gone by the time the player presses "new game".

Sorry to be blunt, but that's not how it works at all. The Garbage Collector does not arbitrarily release memory based on the age of the objects. It only releases objects that have no active references (when no part of your code can find or use the object anymore). This means that objects in your game's cache will absolutely *not* be freed by the Garbage Collector, because they are currently being referenced by the engine's cache object.

I think that what might have you confused is that MV's default cache management system will automatically start un-caching things once the cache reaches a designated size. If you preload a ton of stuff to the default cache object, then yes, you are correct that older images will probably start getting replaced by newer ones (not because of the Garbage Collector, but because of the way MV's default cache system is coded)... however, I would imagine that a decent preloader plugin would probably implement a separate cache to allow devs to bypass the default cache system. I've never actually used such a plugin, so maybe they don't, but if I were to code one, that would certainly be an option that I would include.

It's also worth noting that MZ's cache system works completely differently from MV's, and it doesn't monitor the size of the cache at all. It simply has one cache that is cleared between map transfers, and another cache that is permanent throughout the game session.

For the record, I'm not advocating that people should preload their entire games at boot, or anything like that. I think that your advice is good--it's just that some of your information is wrong.
having coded a preloader, I can say that it's not the load, it's how you handle it.
I had to rework how I structure tilesets, so that one sheet is common, and the other four are progressively floaty-er down the line.
progression should be structured like a tree, not a grid.... that's why Bethesda is loading things every two steps: one, they design like a grid, and two, they jump around that grid.
it's just that some of your information is wrong.
regardless of implementation, nothing can create physical RAM or increase it.
that means that the cache size is ALWAYS limited, because you can't cache outside the RAM.

And if the implementation of the garbage collector does not remove oldest files from memory even if they are still referenced, then we get exactly the memory leaks I also wrote about, the ones that crash the computer when no more memory is available to be added to the cache.

which is part of the problem, as I also mentioned.

So if it is really iimplemented as you claim (as said I haven't checked the code itself), then both the garbage collector AND the preloader are truly garbage that only hope the games are small enough to not crash the computer due to memory overload.

Because that is the true definition of a memory leak: memory that is assigned but never released independent on whether it is still needed or not.
Because that is the true definition of a memory leak: memory that is assigned but never released independent on whether it is still needed or not.
No, it can only be defined as a memory leak if the memory is no longer needed. If you're intentionally keeping it in memory, with the intention of using it again, then it cannot be defined as a memory leak.

And if the implementation of the garbage collector does not remove oldest files from memory even if they are still referenced, then we get exactly the memory leaks I also wrote about, the ones that crash the computer when no more memory is available to be added to the cache.
If a Garbage Collector went around arbitrarily freeing memory that has active references, that would likely crash your application, because then you'd be trying to access values in memory that no longer exist. Garbage Collectors only touch things that it knows for sure are no longer going to be used (hence the reason for the word "garbage" in the name). There is literally no Garbage Collector that acts the way that you're claiming it does.

So if it is really iimplemented as you claim (as said I haven't checked the code itself), then both the garbage collector AND the preloader are truly garbage that only hope the games are small enough to not crash the computer due to memory overload.
To be frank, you probably *should* check such things before you go around writing posts about them. But regardless, a Garbage Collector isn't "garbage"... you're just misunderstanding what Garbage Collectors are supposed to do. In older languages, developers had to manually free an object from memory when they no longer intended to use the object in their code. In newer languages, a Garbage Collector was invented in order to remove that burden from the developers. That's all--it's job is simply to clean things that no longer serve any purpose out of memory.

If you're saying that a preloader that implements a permanent cache is garbage, then I disagree. Not all things should be cached permanently, but there are some things where it makes sense. For example, certain UI elements might be visible throughout the entire game. Does it make sense to have the relevant images keep dropping out of cache, and having to reload the files? They're going to be in RAM pretty much 100% of the time regardless, but then you're just wasting system resources by periodically deleting and then reloading them again for no reason. The same thing could possibly apply to your MC's walking sprite, shadow graphics, etc.

If I were to create a preloader plugin, I'd give the dev the option to choose whether they want to load something to temporary cache, or to permanent cache, and I'd leave the decision for when to use one or the other up to them. The dev should be the one responsible for worrying about how much memory their game is using.
@Arthran
The problem with your way are the files that were neither needed nor released by the programmer.
Resources that he keeps referenced despite never using them.

Because with a good memory management, there is absolutely no need for a garbage collector at all.

Early programming languages enforced correct memory management - Pascal for example was especially strict there. You had to plan your memory usage and program it correctly with all declarations and assignments of memory, or the Pascal compilers didn't even process your code.

Many modern languages have released the forced memory management procedures in order to speed up programming workflow, but that had the price of creating these memory problems.

And no, it is not the game developer who should have cared about this, it is the programmer that wrote the plugin or code used by the game developer.
The programmer is the one who decides he needs more variables or references and requests them from the computer, and he should tell the computer when those resources are no longer needed and can be released. The game developer often has no idea what vaiables are even used on the code level.
Unfortunately most of today's programmer never plan their memory usage. They assign and create resources and then ignore them, hoping that the garbage collector correctly learns which resources have been abandoned and removes them - which is what rarely works as good as they hope.

and preloaders mess that up even further by adding resources to the cache and references to the computer. Which is the reason why they can cause more problems if someone without knowledge just adds to the cache without care.
The problem with your way are the files that were neither needed nor released by the programmer.
Resources that he keeps referenced despite never using them.
That's a fault of the programmer--not of his tools. Similarly, if a game dev preloads a bunch of stuff into permanent cache unnecessarily, then that's the fault of the game dev, not of the plugin.

Because with a good memory management, there is absolutely no need for a garbage collector at all.

Early programming languages enforced correct memory management - Pascal for example was especially strict there. You had to plan your memory usage and program it correctly with all declarations and assignments of memory, or the Pascal compilers didn't even process your code.

Many modern languages have released the forced memory management procedures in order to speed up programming workflow, but that had the price of creating these memory problems.

And no, it is not the game developer who should have cared about this, it is the programmer that wrote the plugin or code used by the game developer.
The programmer is the one who decides he needs more variables or references and requests them from the computer, and he should tell the computer when those resources are no longer needed and can be released. The game developer often has no idea what vaiables are even used on the code level.
Unfortunately most of today's programmer never plan their memory usage. They assign and create resources and then ignore them, hoping that the garbage collector correctly learns which resources have been abandoned and removes them - which is what rarely works as good as they hope.
The risk of memory leaks is actually thought to be higher in those older languages. Garbage collectors were created with the intention of reducing the occurrence of memory leaks. There's probably not enough empirical data to prove one way or the other, but regardless, I feel like this is getting very tangential to the original topic.

My point was simply that the Garbage Collector doesn't go around deleting images out of your game's cache, as you claimed in your original post.

Original thread: https://forums.rpgmakerweb.com/threads/preloaders-advantages-and-disadvantages.162804/