'Flutter CopyWith what happens to the old object,Is it good for performance?

The point is that if we use the copyWith method, as in the example below, it creates a new object, and what happens to the old one?

If the old one is not deleted, doesn't this lead to a loss of performance?

The CopyWith method is usually used in statenotifier in this case, isn't it better to use a changenotifier instead of a statenotifier in terms of performance?

class EmailSignInModel {
EmailSignInModel({
this.email='',
this.formType=EmailSignInFormType.signIn,
this.isLoading=false,
this.password='',
this.submitted=false,
});

final String email;
final String password;
final EmailSignInFormType formType;
final bool isLoading;
final bool submitted;

EmailSignInModel copyWith({
String email,
String password,
EmailSignInFormType formType,
bool isLoading,
bool submitted,

}) {
return EmailSignInModel(
email: email ?? this.email,
password: password?? this.password,
formType: formType?? this.formType,
isLoading: isLoading?? this.isLoading,
submitted: submitted?? this.submitted

);
}
}


Solution 1:[1]

Dart has a garbage collector (GC) that knows how to take care of that. If the old object doesn't have a reference anywhere, it will be picked up by the GC to be disposed and free up the memory. The system decides when the GC runs, mostly when the memory heap reaches a certain point and more available memory is needed.

As long as you don't have a memory leak, which keeps a reference to this item and avoid it from being disposed by the GC, you probably have nothing to worry about.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dumazy