Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Typescript (Angular2): Passing objects to functions -> possible memory leaks?

I have read that when passing objects as parameters in javascript (also inherited in Typescript) it is passed by reference.

Could that possibly cause memory leaks?

Let's assume a scenario where:

  1. The following component, executes the saveCustomerInfo function
  2. The function then pushes the object in an array.
  3. The component gets destroyed.

@Component({
    selector: "customer-details",
    templateUrl: "../html/customerDetails.template.html"
})
export class CustomerDetailsComponent {
  customerData: Customer;

  constructor(private customerService: CustomerService): void {}
 
  saveCustomerInfo(): void {
    this.customerService.saveCustomer(this.customerData);
  }
}

Wouldn't that cause a memory leak, as the service now has a saved reference of a Component's attribute not allowing the Component to be garbage collected?

In any case, what is the best practice on such calls? Should we only pass temporary objects declared inside the functions via "let" or pass clones created by Object.create()?

Thank you very much in advance,

Dimitris

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

@Component({
    selector: "customer-details",
    templateUrl: "../html/customerDetails.template.html"
})
export class CustomerDetailsComponent {
  customerData: Customer;

  constructor(private customerService: CustomerService): void {}

  saveCustomerInfo(): void {
    this.customerService.saveCustomer(this.customerData);
  }
}

In your example code, based on what you described in the rest of your question, I assume that the customerService.saveCustomer(customerData) call ends up putting its parameter (customerData) into an internal array.

This correctly means that the customerData object won't be garbage collected because now your customerService object has a reference to it. However, although the customerData object can't be garbage collected, the rest of the CustomerDetailsComponent can be garbage collected without an issue - the fact that someone else has a reference to one of its internal properties means that specific property won't be marked for garbage collection, but the rest of the object is still ready for garbage collection (assuming no one else is holding a reference to the component itself).

over 4 years ago · Santiago Trujillo Report

0

"When Angular sees a component is no longer needed in the DOM, it will destroy the component."

Basically, when the component isn't needed, ngDestroy() is called.

In this isntance, you can actually test to see if the component gets destroyed by implementing the lifecycle hook on the component, making the call to as you do above and then moving to a new view where the component isn't in the DOM.

The component should be destroyed just fine.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!