Good day,
I am facing some memory allocation problem where I have a class Car extending another class CarParent and then creating the instance of Car class somewhere else in the application. But on setting null to car instance is not deleting the instance of parent created because of super keyword.
Class car:
export class Car extends CarParent {
constructor() {
super();
}
public go(msg: string): string {
console.log(msg);
return msg;
}
}
Class CarParent
class CarParent {
constructor() {
}
getParts() {
console.log("you will get parts")
}
}
Inside application:
import { Car } from './car';
class Company {
config;
car;
constructor(config) {
this.config = config;
this.car = new Car();
}
delete() {
this.car = null;
this.config = null;
}
}
function Config(config) {
const company: Company = new Company(config);
return company;
}
export default Config;
On Index.html
<script>
var config = { place: "france" };
var box = new Renderer();
box.delete();
box = null;
</script>
Tried to debug from heap allocation:
First both Car and CarParent instance created(Screenshot):
After writing box.delete() and box = null and taking snapshot again i can see car instance is deleted:
But CarParent instance does not delete:
I am confused. Why this is happening? Is it correct, whatever happening or how can i delete memory occupied by CarParent.
Please help!
the instance of parent created because of super keyword
No, super()
does not create a separate instance.
I am confused. Why this is happening?
The CarParent
"instance" you are seeing in the heap snapshot is Car.prototype
- it's an object inheriting from CarParent.prototype
. You can see how it holds the .go
method and Car
as its .constructor
.
You do not want to delete it.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments