LoadVars RAM Saver Trick

To those of you that think memory management is old hat, more power to you. However, there are those of us who have never had the opportunity to worry about such things in the past. Who knows what is really happening behind the scenes, but I was trying to load 26 XML files. The process goes:

– get xml filename
– attach to existing LoadVars variable (not local)
– do a POST to PHP script
– PHP script uses the passed in filename, loads the gZipped XML file, and returns to the LoadVars callback as a variable “theXML”
– LoadVars’ callback then throws that string to the xml object via owner.my_xml.parseXML(theXML)

Now, each XML file, around 300k, was making the memory jump 20 megs every load. So, the 9th load would usually crash Flash. I thought it was the XML object, but turns out, it is the LoadVars object. Even though his “theXML” variable is reset each time, the string data in memory isn’t.

To solve it, I added a “delete this.theXML;” after I passed the string data to the XML object to parse. This doesn’t entirely solve the problem, though. Even though I can see the RAM drop about 20 megs, she still climbs to a whopping 80megs when finished. I really don’t think my nested array/objects are what is causing the significant increase, and there aren’t many components on stage nor in use. At any rate, at least it doesn’t crash now!

8 Replies to “LoadVars RAM Saver Trick”

  1. Good to know. If you (or anyone else) figures out how to keep the ram down, please let me know. I’ll be running into the same problem on this project soon. Time to add some delete statements…

  2. I’m sure if you used as a local variable instead, garbage collection would just “know” to delete it since the activation object isn’t hanging around after your onLoad executes… or does it? Who knows. I don’t think it would hurt putting a delete this inside of your onLoad. In my case, I had to keep him around, but you could just do:

    var my_lv = new LoadVars(); if it’s inside a function.

  3. Actually, the only other I know, but untested, is to have a seperate level do it. Load it into like _level1, get the data, and then do an unloadMovieNum(1). I’ve seen RAM go away real quick in the past, but I’m not sure if it takes into account the scope of _level1 in relation to the LoadVars who exists there. One would think.

  4. Wow 9 300k xml files! I’d suggest finding a way to optimize your data.

    Do you have 9 different views or is each XML file parsed and then compiled into a single data source? If so you may want to consider offloading the parsing to the server.

    What are you using to monitor your mem usage? Perfmon?

  5. External datasource, as usual, is out of my control.

    http://www.swgcraft.com/resourceexport.php

    26 different 300k files vs. one 7meg one

    Each XML file is loaded individually, parsed into a tiny object, and the process continues until all 26 are done.

    I could off-load to the server, but then… it’s already abstracted to simple XML that I can easily parse and then save locally to a local shared object. I only need to do this once a week for the user.

  6. As far as monitoring the RAM usage, I just look at Task Manager. I’ve heard the “requested memory” excuse from one of the chattyfig lists, but I don’t buy that for a minute.

  7. Jesse,

    I possible, offload the parsing of the xml to your server. If you have it aggregating it on a cron each week, don’t just mirror the xml to your server, but parse it as well. You could parse the xml into a php version of same data type that you are currently deserializing it to on the client side. You could save this data as serialized info on your server. Then when a client requests the info you could have a PHP script read in the data and send it to the client via Flash remoting. You would take one hit each week for the processing, and a lot less intensive hit each time someone made a request. You aren’t serving flat xml files anymore, but from the clientside you don’t have the same issues.

  8. I came accross this issue by having to reload an XML object over and over during a loop in a ‘screensaver’, crashing my PC eventually.
    I worked around it by setting isLoaded flag

    Anyway, here’s some example code of how to repro this bug

    function init() {
    adXML = new XML();
    adXMLfile = “xmlfile.xml”;
    loadXml();
    }
    function loadXml() {
    adXML.load(adXMLfile);
    adXML.onLoad = function(sucess) {
    if (sucess) {
    init();
    pass++;
    output.text = pass;
    }
    };
    }
    init();

Comments are closed.