top

Memory optimization for Smart Tv Apps

Published 2014-10-27 | (Compatible with SDK 3.5,4.5,5.0,5.1 and 2012,2013,2014 models)

Tips on application optimization

If your application has many big sized images, which are created by the new constructor, it can lead to memory leakage, as this operation is not finished automatically.

For efficient memory optimization, we recommend to use several methods. All of them are illustrated with good and bad usage examples.

new Image() constructor

The new constructor is not finalized automatically, if not explicitly using delete or set null. So the runtime memory of Javascript Engine will be ever increasing. As devices having limited memory, it is better to use a different method.

Bad example

function test() {
    var a = new Image(); // local variable
    document.getElementById("foo").appendChild(a);
}

Good example

function test() {
    var a = document.createElement("img"); // use createElement() instead of new Image()
    document.getElementById("foo").appendChild(a);
}

removeChild function

Because the return value of removeChild is Node, if there is no left-hand side of removeChild expression then it is never finalized. For that reason, if the returned value of removeChild is not assigned to any variable, the runtime memory of Javascript Engine will be still increasing.

Bad example

element.removeChild(element.firstChild);

Good example

var a = element.removeChild(element.firstChild);

Samsung native API

In order to avoid memory leakage, the Samsung App Engine has a new implemented API - deleteChild. It works exactly as standard removeChild function except that there is no return value.

if (element.deleteChild) {
    element.deleteChild(element.firstChild);
} else {
    element.removeChild(element.firstChild);
}

XHR.destroy() function

XMLHttpRequest uses a lot of memory while parsing the responseXML document. When the XHR object is no longer needed, using the destroy() method may free some memory space.

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        ...
        if (xhr.destroy) {
            xhr.destroy();
        }
    }
}