After one day reworking the whole project and trying to find out how to deal with all this, I eventually made it. As I have seen several developers hitting memory problems at times, jere is the whole answer.
As an introduction to this solution, I want to point the fact that I wasn't exactly doing "bad things", that is, doing infinite loops or any kind of heavy scripts. For a long time, I absolutely had no problem with what I was doing, which was mostly like what everyone here does; the only difference with some people here being that my scripts were Javascript and not in C#.
My usage was very simple, but after some time with everything running smoothly, Unity Editor was beginning to hit the memory limit (around 2.4 GB for me) and to crash very often.
Here is what I was doing, which created the problem:
I imported my PNG images as Texture2D assets.
My assets were often NPOT (Non Power of Two) high resolution images,.
These assets were declared as member variables of my objects (to be able to drag&drop them into the object from the library, inside the Editor).
These variables were affected by default (ie. the picture was dragged over it inside the Editor)
There were around 30 such assets. At one time I decided to create an array of 18 of these assets. When I was dragging picture from the Assets library to the field, I noticed some slowdown, nothing more. This is exactly when Unity jumped from 0.4 GB memory to about 2.2 GB memory, without me noticing it.
As long as my Texture2D array was filled with the eighteen 1920 hi-res pictures, Unity was memory-mad.
- Since Unity Editor was already eating up so much memory, everytime I tried to import a new picture Asset inside my Library, this was likely to make the Editor crash.
This sounds like a bug with Texture2D arrays in javascript, but I cannot be sure, I am still young to Unity.
Here is what I tried, which didn't exactly work:
I removed the settings of these variables (leaving them empty again). As soon as the Texture2D array was emptied, Unity came back to 0.4 GB memory consumed.
I was then able to add my other pictures inside my Asset library. Unity was able to insert them without going much above its regular 0.4 GB memory consumption.
Now that I was again able to use the Editor, I modified programs to use Resources.Load instead of setting my pics inside tons of Texture2D objects.
Again this failed: the Editor memory was fine, but when building, it tries to insert all these Assets and Unity fails again and crashes. Even with only one Asset used and only one line of Resources.Load(), Unity was crashing on the"Building level 0" step.
Maybe Unity was broken at this time. I don't know.
It seemed that I was still bothered by memory problems because of the picture assets inside my Library.
I of course have tried to delete / reimport them, try other texture settings, to no result.
Here is the solution THAT worked:
Of course, if you never ran into this problems, you can continue the way you are doing things. Using Resources.Load() with picture assets inside your library is the right way to do things and avoid heavy memory usage, just as Mike underscored.
But it couldn't get me past my problem; it was the library that was memory-exploding in my issue. Even with my Texture2D as empty objects and only one Resources.Load() at the project start, project was broken and unbuildable.
I don't know how it came to do this; this may also be a PNG format problem for instance; I know that Photoshop CS4 did crazy things with my PNG files and broke them everytime, Corel Paintshop was used to fix the mess CS4 did.
So, I don't know this happened; I only know how I eventually got it fixed.
So, if you see the Editor using too much memory, experience Editor crashes, or are unable to build and run your program, here is what you maybe should check:
1. Don't load too many high resolution picture assets inside your library.
Use Resources.Load() with picture assets inside your library as long as it seems fine.
You may use dynamic resource loading, targeting local files, with the WWW object and file:// urls.
Note for MAC users: it took me some time to find out that to have the same thing as...
file://c:/...
You have to write things like:
file:///Users/Desktop/myUserName/...
Notice the three slashes.
There was no C drive on my iMac, I googled for a solution, but eventually found out by opening a local file with Safari.
Don't create Javascript Texture2D arrays as preset member variables, with heavy hi-res pictures in their rows. This seems to break up memory usage - at least it did with me.
So I worked things out by removing almost all picture assets from my Library, by placing them inside another folder out of the Assets folder, which relieved Unity Editor and Unity Builder.
I now access these resources with WWW-loading. I have about four Texture2D objects that get dynamically switched thanks to this.
This completely works.
I just don't know how things got broken, as of course, I suppose you are fine by using Resources.Load() yourself.
But hey, I am only doing many GUI screens and switching them, and if it happened to me, it may surely happen to someone else; so here at least you have a solution that could also work for you.