Memory leak in setProperty()

Former Member
Former Member
I believe there is a memory leak in setProperty(). I think the easiest way to provide an example is for you to edit the primates sample. Update the following functions to the following code. To stop the memory leak behavior you can just comment out the setProperty.. line.
I also had other memory leaks that I have resolved. Of note is that they did not show in the simulator, but occur on my device (920XT). I think the simulator should be made to recognize these problems.

Forgot to mention. To see the leak you must run on your device, and scroll through the pages. Each time you reenter a page the used memory will be greater.

Primates.mc
function onStart() {
var key = 0;
var value = getProperty(key);
if(null != value) {
deleteProperty(key);
value = 0;
}
setProperty(key,value);
}

PrimatesView.mc
function onUpdate(dc)
{
dc.setColor( Gfx.COLOR_TRANSPARENT, Gfx.COLOR_BLACK );
dc.clear();

dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
dc.fillRectangle(0, 0, dc.getWidth(), dc.getHeight());

dc.setColor( color, Gfx.COLOR_TRANSPARENT );
dc.drawText(5, 5, Gfx.FONT_SMALL, primates[index], Gfx.TEXT_JUSTIFY_LEFT);

dc.drawBitmap(25, 30, bitmap);

indicator.draw(dc, index);

dc.setColor( Gfx.COLOR_BLACK, Gfx.COLOR_BLACK );
var _text = "Usedmem: " + System.getSystemStats().usedMemory;
dc.fillRectangle(0,0,218,22);
dc.setColor(Gfx.COLOR_WHITE,Gfx.COLOR_BLACK);
dc.drawText(0, 0, Gfx.FONT_TINY, _text, Gfx.TEXT_JUSTIFY_LEFT);
}

PrimatesDelegate.mc // Handlers for those without touchscreen
function onKey(event) {
var key = event.getKey();

if(Ui.KEY_UP == key) {
onPreviousPage();
}
else if(Ui.KEY_DOWN == key) {
onNextPage();
}
else if(Ui.KEY_ESC == key) {
Ui.popView(Ui.SLIDE_IMMEDIATE);
}
return true;
}
  • You only call setProperty once. It looks like you are indicating that the memory use is higher with the call to setPtoperty than without.

    If that is the case, this is not a leak, it is just memory used by the application framework. Calling setProperty or getProperty will likely cause a cache to be created (used to minimize the cost of reading/writing the properties).

    If this is an actual leak, the amount of used memory would go up as the program ran. If it was a leak in setProperty, you could put the call in onUpdate and watch the memory use grow.
  • Former Member
    Former Member over 10 years ago
    Scroll through the pages. Watch the used memory increase each time you reenter a page
  • The onStart function is only called once when the application starts, not once for every view change. If the memory use goes up when switching pages, then the code in your onStart is not the source of the leak.

    I'm not saying there is not a leak in the primate example. I'm saying the code that you post above does not demonstrate a leak in setProperty. If it is a leak in setProperty, calling it a bunch of times (using a timer perhaps) would show memory usage increasing.

    I'm currently laying in bed, so I'll have to wait until morning to verify I'm not insane, but I'm pretty sure this is a red herring.
  • Former Member
    Former Member over 10 years ago
    Fair point. The reason I had attributed it to setProperty was because the behaviour only occurs after the call to setProperty. So yes, it may be an error with some other function, that only rears it's head after the setProperty call. So perhaps it is pushView, or switchView, or.....
  • Former Member
    Former Member over 10 years ago
    function sleepWell() {
    timer.start( method(:awaken), 8*60*60*1000, false);
    }
  • Okay. I've done some testing. Note that I have a 920XT and the Primates example was only written to work with a touch-enabled device, so I had to make a few tweaks...

    // Added and disabled code in MyApp.onStart
    function onStart() {
    //var key = 0;
    //var value = 0;
    //setProperty(key,value);
    }

    // Added code to PrimatesView.onUpdate
    function onUpdate(dc)
    {
    // original code here untouched

    var s = Sys.getSystemStats();
    dc.setColor(Gfx.COLOR_WHITE, Gfx.COLOR_BLACK);
    dc.drawText(0, 0, Gfx.FONT_SMALL, s.usedMemory.toString(), Gfx.TEXT_JUSTIFY_LEFT);
    }

    // Added PrimatesDelegate.onKey for 920XT
    function onKey(evt) {
    var key = evt.getKey();
    if (key == Ui.KEY_UP) {
    onPreviousPage();
    } else if (key == Ui.KEY_DOWN) {
    onNextPage();
    }
    else {
    return false;
    }

    return true;
    }


    Additionally, I added the appropriate entry to the manifest to allow the test program to be built for the 920XT.

    <iq:products>
    <iq:product id="square_watch"/>
    <iq:product id="fr920xt"/>
    </iq:products>


    Other than that I've made no changes.

    I side-loaded the app onto my 920 and cycled through the screens 20 times using only the down button, and recorded all of the values into a google spreadsheet. I then normalized each column (calculated the min in that column, and then subtracted the minimum for that column from the value in each cell and stored the result in another cell). I then enabled the setProperty() code that was added to onStart(), gathered the same data, and plotted the memory use for each against the other.

    I came up with this speadsheet. If we were seeing a leak, then I'd expect the memory usage to increase as the app is used, but that is not what I'm seeing. I see it go up and down both before and after the setProperty call is added.

    IMO, this is not a leak (at least not on a 920XT using the 3.20 firmware).
  • Former Member
    Former Member over 10 years ago
    Sadly yes, I agree. By sheer fluke, when I ran it, the values of the page I was monitoring showed slightly increased usage the first 3 times I reentered the page . When I run it again now I see it eventually rolls back. Please understand that I was getting rather tired of plugging my device in and out to track down my problem, being that my code does not exhibit a memory leak in the simulator. Because the test had matched the behavior I was expecting I didn't run it further. I had thought the primates example might be a shortcut, to prevent me having to write out a multi-page sample to reflect the behavior I am seeing with my app. I will write a sample that more accurately reflects the behavior I am seeing. Hopefully that will highlight the problem, be it my coding or other.
  • No problem. I know how difficult it can be to isolate issues given the tools that are currently available for debugging. If you'd like help finding your memory leak, let me know. I'd be happy to help.

    Travis
  • Former Member
    Former Member over 10 years ago
    Try running that same code again, with the setProperty enabled, but delete the data file first. It appears to me that problem only occurs when setProperty is writing to a new key.
  • Former Member
    Former Member over 10 years ago
    Taking it one step further, deleting an existing key and resaving it will also cause the problem. And since presently you must delete a key to save an array, there is effectively no way to save an array without the problem arising, other than on app exit.

    function onStart() {
    var key = 0;
    var value = getProperty(key);
    if(null != value) {
    deleteProperty(key);
    value = 0;
    }
    setProperty(key,value);
    }