0

I have an angular service having some properties and methods. This is injected in different controllers and all working fine as expected. Now the issue is with the memory conception.

The signature of the class looks like the following:

export class CustomService implements ICustomService {
    // Having some variables here
    customArray: Array<NameSpace.CustomClass>
    constructor() // few injectors here
    {
        // Some code
    }
    // Some methods Here
}

One of the property inside the service is an Array of custom object(that is populated using a service call). The number of items in the objects may vary depends on certain parameters sometimes the list may have less than 10 items sometimes it may greater than 100.

Questions:

  • Consider that the specific list is now having 100 items, from another controller it is re-assigned with 3 items, will it free the memory for those 97 items(that are previously in that array)?
  • Is it possible/needed to free the property inside a service using delete
  • Is there any other reasons for memory leaks when using services
pegla
  • 1,866
  • 4
  • 17
  • 20
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • 2
    JavaScript has a garbage collector. If an object is not referenced by anything anymore (or not reachable anymore through a chain of references), it's garbage collected. So, if the array is the only place where the 100 objects are referenced, and the array is replaced by another one, both the original array and the objects it referenced will be GCed. – JB Nizet Sep 27 '17 at 15:02
  • @JBNizet: But in my case the service is always injected to any of the controller, so it will be there in memory as long as service exists. right?That's fine. But Why I'm getting memory increased in each navigation? It should show the difference for 3 objects and 100 objects right? – sujith karivelil Sep 27 '17 at 15:13
  • It's impossible, from your description, to know *if* you have a memory leak, and *what* causes the memory leak. What is sure is that using services, and not calling delete before replacing an array by another one, is not what causes memory leaks. – JB Nizet Sep 27 '17 at 15:22
  • If it is an array that is being used, this isn't memory leak, this is memory use. If you're encountering memory leaks, consider providing http://stackoverflow.com/help/mcve . Otherwise the question is off-topic, it doesn't contain anything that could help to provide a quality answer. – Estus Flask Sep 27 '17 at 15:28
  • Memory Leaks in the sense, after few navigations the memory used by chrome became `1.5GB`. This is what I want to control – sujith karivelil Sep 27 '17 at 15:29

0 Answers0