2

I am working on a piece of code that has a substancial memory leak, I am posting an example function but my entire 11,000 line code has similar functions repeated all over. I am experiencing an average of 5Mb memory leak when I click the Refresh Button. The logic behind the refresh button is to call the empty function of the object. I have researched all over for cyclic references and closures but I am not really sure if the way I set everything to an empty array is correct or should I set everything to null ? Any help would be great. I have to use IE as the app is on IE. Sadly no chrome tools for me :(

/**
 * Represents the tasks for the currently loaded patients.
 */
var foo = {
    loaded: false,
    overdueTaskCounts: [],
    unscheduledTaskCounts: [],
    currentTaskCounts: [],
    scheduled: null,
    patientTasks: {},
    tasks: {},
    taskNumber: 0,

    /**
     * Unpacks the JSON received from the CareCompass service for the CareCompass task counts.
     * @param reply - The JSON representing the data returned from the CareCompass service.
     */
    unpack: function unpackTasks(reply) {
        var taskCounts = reply.data; * * //This function populates the variables declared above**//
        this.scheduled = taskCounts.scheduled;
    },
    /**
     * Removes all the task information related to the loaded patients.
     * @param none
     */
    empty: function emptyTasks() {
        this.loaded = false;
        this.overdueTaskCounts = [];
        this.unscheduledTaskCounts = [];
        this.currentTaskCounts = [];
        this.scheduled = null;
        this.patientTasks = {};
    }
}
11
  • 5
    Common practice is not to use the Array constructor, not least because its constructor semantics are inconsistent. Commented May 20, 2013 at 19:47
  • 2
    I would recommend not to use antipattern @DavidStarkey http://w3fools.com/ and stick with literals. Commented May 20, 2013 at 19:50
  • 1
    that I what I have been told in school and usually to use literals instead of new Array(); Commented May 20, 2013 at 19:54
  • 3
    Stop arguing about how to declare a variable. It's [] and that's it, and it has nothing to do with the question. There isn't enough code to go by to determine if this is even where the memory leak occurs. Commented May 20, 2013 at 20:08
  • 1
    Most of the memories leaks I ever had while programming javascript were due to dynamically creating HTML elements, adding them to the DOM and forgetting to remove them. It's pretty easy to happen when you are adding elements with position:absolute as they will stack on top of each other perfectly. Also check your libraries, I have had problems with dojo causing memory leaks when updating its charts. Commented May 20, 2013 at 20:29

1 Answer 1

3

This video from Google I/O will show you how memory leaks happen and how to debug your app. http://www.youtube.com/watch?v=x9Jlu_h_Lyw

Grab some coffee and get ready for a great video.

Sign up to request clarification or add additional context in comments.

3 Comments

sorry, I should have mentioned earlier I cannot use Chrome my app is on IE.
So? You can still debug the memory leak in Chrome.
I did debug it using this. It really helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.