javascript weakmap keep refrence to deleted object [duplicate]












1
















This question already has an answer here:




  • JavaScript(ES6) WeakMap garbage collection when set an object to null

    1 answer




when delete the object , weakmap keeps refrence to it.

but the normal behaviour is : when oyu delete the object it will removed from weakmap automatically and weakmap cannot cause memory leak.

is it something wrong with weakmap or delete ?



let a =  { aa : { aa : 123 } };
const w = new WeakMap();
w.set(a.aa,"hello");
delete a.aa
console.log(w);// shows that '{aa:123}' is still there in weakmap


i've closed and open the devtool and {aa:123} is still there.

expect weakmap to be empty










share|improve this question















marked as duplicate by Community Jan 3 at 11:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • thanks to @komal-bansal this question has been answered already in this link . so flagged as duplicate : stackoverflow.com/a/49841518/2611020

    – hamidkardorost
    Jan 3 at 10:49
















1
















This question already has an answer here:




  • JavaScript(ES6) WeakMap garbage collection when set an object to null

    1 answer




when delete the object , weakmap keeps refrence to it.

but the normal behaviour is : when oyu delete the object it will removed from weakmap automatically and weakmap cannot cause memory leak.

is it something wrong with weakmap or delete ?



let a =  { aa : { aa : 123 } };
const w = new WeakMap();
w.set(a.aa,"hello");
delete a.aa
console.log(w);// shows that '{aa:123}' is still there in weakmap


i've closed and open the devtool and {aa:123} is still there.

expect weakmap to be empty










share|improve this question















marked as duplicate by Community Jan 3 at 11:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • thanks to @komal-bansal this question has been answered already in this link . so flagged as duplicate : stackoverflow.com/a/49841518/2611020

    – hamidkardorost
    Jan 3 at 10:49














1












1








1









This question already has an answer here:




  • JavaScript(ES6) WeakMap garbage collection when set an object to null

    1 answer




when delete the object , weakmap keeps refrence to it.

but the normal behaviour is : when oyu delete the object it will removed from weakmap automatically and weakmap cannot cause memory leak.

is it something wrong with weakmap or delete ?



let a =  { aa : { aa : 123 } };
const w = new WeakMap();
w.set(a.aa,"hello");
delete a.aa
console.log(w);// shows that '{aa:123}' is still there in weakmap


i've closed and open the devtool and {aa:123} is still there.

expect weakmap to be empty










share|improve this question

















This question already has an answer here:




  • JavaScript(ES6) WeakMap garbage collection when set an object to null

    1 answer




when delete the object , weakmap keeps refrence to it.

but the normal behaviour is : when oyu delete the object it will removed from weakmap automatically and weakmap cannot cause memory leak.

is it something wrong with weakmap or delete ?



let a =  { aa : { aa : 123 } };
const w = new WeakMap();
w.set(a.aa,"hello");
delete a.aa
console.log(w);// shows that '{aa:123}' is still there in weakmap


i've closed and open the devtool and {aa:123} is still there.

expect weakmap to be empty





This question already has an answer here:




  • JavaScript(ES6) WeakMap garbage collection when set an object to null

    1 answer








javascript memory-leaks garbage-collection weakmap






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 10:06







hamidkardorost

















asked Jan 3 at 6:51









hamidkardorosthamidkardorost

3311




3311




marked as duplicate by Community Jan 3 at 11:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Community Jan 3 at 11:12


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • thanks to @komal-bansal this question has been answered already in this link . so flagged as duplicate : stackoverflow.com/a/49841518/2611020

    – hamidkardorost
    Jan 3 at 10:49



















  • thanks to @komal-bansal this question has been answered already in this link . so flagged as duplicate : stackoverflow.com/a/49841518/2611020

    – hamidkardorost
    Jan 3 at 10:49

















thanks to @komal-bansal this question has been answered already in this link . so flagged as duplicate : stackoverflow.com/a/49841518/2611020

– hamidkardorost
Jan 3 at 10:49





thanks to @komal-bansal this question has been answered already in this link . so flagged as duplicate : stackoverflow.com/a/49841518/2611020

– hamidkardorost
Jan 3 at 10:49












3 Answers
3






active

oldest

votes


















2














Your delete() function should look like this:



 w.delete(a.aa);





share|improve this answer
























  • you're right , but my problem is not deleting something from weakmap . weakmap are known as something that has weak reference . this means that when you remove the object that has a reference in weakmap , the weak cannot prevent the object from gc . so weakmap does not cause memory leak . but in my code even though i'm deleting the object , weakmap prevent gorbage collector from removing the object . my question is why weakmap do such a thing ? look and try this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:54





















0














You are using delete operator instead of delete property of weak map.



var a =  { aa : { aa : 123 } };
var w = new WeakMap();
w.set(a.aa,"hello");
console.log(a.aa)
w.delete(a.aa); // try this
console.log(w);


Docs link






share|improve this answer


























  • my question is not removing something from weakmap . my Q is : the point of weakmap is : when you delete object that has a reference in weakmap , weakmap remove the object inside , so that gc be able to remove the object from memory . but in my case even though i'm deleting object weakmap hold the reference to object and this cause gc to fail at removing object {aa:123} . this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:58








  • 2





    I think you can get , what you are looking for from here stackoverflow.com/questions/49841096/…

    – Komal Bansal
    Jan 3 at 10:30











  • oh my god . yes that's the answer . thank you so very much

    – hamidkardorost
    Jan 3 at 10:44



















0














However I have not worked with weakmap but the reason behind not deleting through



delete a.aa



is just because w is separate reference to the object rather pointer to the same object.




Simple values (aka scalar primitives) are always assigned/passed by value-copy: null, undefined, string, number, boolean, and ES6's symbol.



Compound values -- objects (including arrays, and all boxed object wrappers -- see Chapter 3) and functions -- always create a copy of the reference on assignment or passing.




consider the below snippet:



function foo(x) {
x.push( 4 );
x; // [1,2,3,4]

// later
x = [4,5,6];
x.push( 7 );
x; // [4,5,6,7]
}

var a = [1,2,3];

foo( a );

a; // [1,2,3,4] not [4,5,6,7]



When we pass in the argument a, it assigns a copy of the a reference to x. x and are a separate references pointing at the same [1,2,3] value. Now, inside the function, we can use that reference to mutate the value itself (push(4)). But when we make the assignment x = [4,5,6], this is in no way affecting where the initial reference a is pointing -- still points at the (now modified) [1,2,3,4] value.




Give it a read to this!



Hope it makes it clear!






share|improve this answer


























  • no weakmap is not the same as normal object . weakmap is called weak because it holds a weak reference to object and this weak reference means that when you delete the object itself > it will removed from weakmap automatically and the weak reference inside w cannot prevent gorbage collector from removing the object. look at this answer : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 10:02


















3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Your delete() function should look like this:



 w.delete(a.aa);





share|improve this answer
























  • you're right , but my problem is not deleting something from weakmap . weakmap are known as something that has weak reference . this means that when you remove the object that has a reference in weakmap , the weak cannot prevent the object from gc . so weakmap does not cause memory leak . but in my code even though i'm deleting the object , weakmap prevent gorbage collector from removing the object . my question is why weakmap do such a thing ? look and try this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:54


















2














Your delete() function should look like this:



 w.delete(a.aa);





share|improve this answer
























  • you're right , but my problem is not deleting something from weakmap . weakmap are known as something that has weak reference . this means that when you remove the object that has a reference in weakmap , the weak cannot prevent the object from gc . so weakmap does not cause memory leak . but in my code even though i'm deleting the object , weakmap prevent gorbage collector from removing the object . my question is why weakmap do such a thing ? look and try this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:54
















2












2








2







Your delete() function should look like this:



 w.delete(a.aa);





share|improve this answer













Your delete() function should look like this:



 w.delete(a.aa);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 3 at 6:55









Ben BeckBen Beck

1,9381616




1,9381616













  • you're right , but my problem is not deleting something from weakmap . weakmap are known as something that has weak reference . this means that when you remove the object that has a reference in weakmap , the weak cannot prevent the object from gc . so weakmap does not cause memory leak . but in my code even though i'm deleting the object , weakmap prevent gorbage collector from removing the object . my question is why weakmap do such a thing ? look and try this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:54





















  • you're right , but my problem is not deleting something from weakmap . weakmap are known as something that has weak reference . this means that when you remove the object that has a reference in weakmap , the weak cannot prevent the object from gc . so weakmap does not cause memory leak . but in my code even though i'm deleting the object , weakmap prevent gorbage collector from removing the object . my question is why weakmap do such a thing ? look and try this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:54



















you're right , but my problem is not deleting something from weakmap . weakmap are known as something that has weak reference . this means that when you remove the object that has a reference in weakmap , the weak cannot prevent the object from gc . so weakmap does not cause memory leak . but in my code even though i'm deleting the object , weakmap prevent gorbage collector from removing the object . my question is why weakmap do such a thing ? look and try this can help : stackoverflow.com/a/43579723/2611020

– hamidkardorost
Jan 3 at 9:54







you're right , but my problem is not deleting something from weakmap . weakmap are known as something that has weak reference . this means that when you remove the object that has a reference in weakmap , the weak cannot prevent the object from gc . so weakmap does not cause memory leak . but in my code even though i'm deleting the object , weakmap prevent gorbage collector from removing the object . my question is why weakmap do such a thing ? look and try this can help : stackoverflow.com/a/43579723/2611020

– hamidkardorost
Jan 3 at 9:54















0














You are using delete operator instead of delete property of weak map.



var a =  { aa : { aa : 123 } };
var w = new WeakMap();
w.set(a.aa,"hello");
console.log(a.aa)
w.delete(a.aa); // try this
console.log(w);


Docs link






share|improve this answer


























  • my question is not removing something from weakmap . my Q is : the point of weakmap is : when you delete object that has a reference in weakmap , weakmap remove the object inside , so that gc be able to remove the object from memory . but in my case even though i'm deleting object weakmap hold the reference to object and this cause gc to fail at removing object {aa:123} . this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:58








  • 2





    I think you can get , what you are looking for from here stackoverflow.com/questions/49841096/…

    – Komal Bansal
    Jan 3 at 10:30











  • oh my god . yes that's the answer . thank you so very much

    – hamidkardorost
    Jan 3 at 10:44
















0














You are using delete operator instead of delete property of weak map.



var a =  { aa : { aa : 123 } };
var w = new WeakMap();
w.set(a.aa,"hello");
console.log(a.aa)
w.delete(a.aa); // try this
console.log(w);


Docs link






share|improve this answer


























  • my question is not removing something from weakmap . my Q is : the point of weakmap is : when you delete object that has a reference in weakmap , weakmap remove the object inside , so that gc be able to remove the object from memory . but in my case even though i'm deleting object weakmap hold the reference to object and this cause gc to fail at removing object {aa:123} . this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:58








  • 2





    I think you can get , what you are looking for from here stackoverflow.com/questions/49841096/…

    – Komal Bansal
    Jan 3 at 10:30











  • oh my god . yes that's the answer . thank you so very much

    – hamidkardorost
    Jan 3 at 10:44














0












0








0







You are using delete operator instead of delete property of weak map.



var a =  { aa : { aa : 123 } };
var w = new WeakMap();
w.set(a.aa,"hello");
console.log(a.aa)
w.delete(a.aa); // try this
console.log(w);


Docs link






share|improve this answer















You are using delete operator instead of delete property of weak map.



var a =  { aa : { aa : 123 } };
var w = new WeakMap();
w.set(a.aa,"hello");
console.log(a.aa)
w.delete(a.aa); // try this
console.log(w);


Docs link







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 7:09

























answered Jan 3 at 7:04









Komal BansalKomal Bansal

28715




28715













  • my question is not removing something from weakmap . my Q is : the point of weakmap is : when you delete object that has a reference in weakmap , weakmap remove the object inside , so that gc be able to remove the object from memory . but in my case even though i'm deleting object weakmap hold the reference to object and this cause gc to fail at removing object {aa:123} . this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:58








  • 2





    I think you can get , what you are looking for from here stackoverflow.com/questions/49841096/…

    – Komal Bansal
    Jan 3 at 10:30











  • oh my god . yes that's the answer . thank you so very much

    – hamidkardorost
    Jan 3 at 10:44



















  • my question is not removing something from weakmap . my Q is : the point of weakmap is : when you delete object that has a reference in weakmap , weakmap remove the object inside , so that gc be able to remove the object from memory . but in my case even though i'm deleting object weakmap hold the reference to object and this cause gc to fail at removing object {aa:123} . this can help : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 9:58








  • 2





    I think you can get , what you are looking for from here stackoverflow.com/questions/49841096/…

    – Komal Bansal
    Jan 3 at 10:30











  • oh my god . yes that's the answer . thank you so very much

    – hamidkardorost
    Jan 3 at 10:44

















my question is not removing something from weakmap . my Q is : the point of weakmap is : when you delete object that has a reference in weakmap , weakmap remove the object inside , so that gc be able to remove the object from memory . but in my case even though i'm deleting object weakmap hold the reference to object and this cause gc to fail at removing object {aa:123} . this can help : stackoverflow.com/a/43579723/2611020

– hamidkardorost
Jan 3 at 9:58







my question is not removing something from weakmap . my Q is : the point of weakmap is : when you delete object that has a reference in weakmap , weakmap remove the object inside , so that gc be able to remove the object from memory . but in my case even though i'm deleting object weakmap hold the reference to object and this cause gc to fail at removing object {aa:123} . this can help : stackoverflow.com/a/43579723/2611020

– hamidkardorost
Jan 3 at 9:58






2




2





I think you can get , what you are looking for from here stackoverflow.com/questions/49841096/…

– Komal Bansal
Jan 3 at 10:30





I think you can get , what you are looking for from here stackoverflow.com/questions/49841096/…

– Komal Bansal
Jan 3 at 10:30













oh my god . yes that's the answer . thank you so very much

– hamidkardorost
Jan 3 at 10:44





oh my god . yes that's the answer . thank you so very much

– hamidkardorost
Jan 3 at 10:44











0














However I have not worked with weakmap but the reason behind not deleting through



delete a.aa



is just because w is separate reference to the object rather pointer to the same object.




Simple values (aka scalar primitives) are always assigned/passed by value-copy: null, undefined, string, number, boolean, and ES6's symbol.



Compound values -- objects (including arrays, and all boxed object wrappers -- see Chapter 3) and functions -- always create a copy of the reference on assignment or passing.




consider the below snippet:



function foo(x) {
x.push( 4 );
x; // [1,2,3,4]

// later
x = [4,5,6];
x.push( 7 );
x; // [4,5,6,7]
}

var a = [1,2,3];

foo( a );

a; // [1,2,3,4] not [4,5,6,7]



When we pass in the argument a, it assigns a copy of the a reference to x. x and are a separate references pointing at the same [1,2,3] value. Now, inside the function, we can use that reference to mutate the value itself (push(4)). But when we make the assignment x = [4,5,6], this is in no way affecting where the initial reference a is pointing -- still points at the (now modified) [1,2,3,4] value.




Give it a read to this!



Hope it makes it clear!






share|improve this answer


























  • no weakmap is not the same as normal object . weakmap is called weak because it holds a weak reference to object and this weak reference means that when you delete the object itself > it will removed from weakmap automatically and the weak reference inside w cannot prevent gorbage collector from removing the object. look at this answer : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 10:02
















0














However I have not worked with weakmap but the reason behind not deleting through



delete a.aa



is just because w is separate reference to the object rather pointer to the same object.




Simple values (aka scalar primitives) are always assigned/passed by value-copy: null, undefined, string, number, boolean, and ES6's symbol.



Compound values -- objects (including arrays, and all boxed object wrappers -- see Chapter 3) and functions -- always create a copy of the reference on assignment or passing.




consider the below snippet:



function foo(x) {
x.push( 4 );
x; // [1,2,3,4]

// later
x = [4,5,6];
x.push( 7 );
x; // [4,5,6,7]
}

var a = [1,2,3];

foo( a );

a; // [1,2,3,4] not [4,5,6,7]



When we pass in the argument a, it assigns a copy of the a reference to x. x and are a separate references pointing at the same [1,2,3] value. Now, inside the function, we can use that reference to mutate the value itself (push(4)). But when we make the assignment x = [4,5,6], this is in no way affecting where the initial reference a is pointing -- still points at the (now modified) [1,2,3,4] value.




Give it a read to this!



Hope it makes it clear!






share|improve this answer


























  • no weakmap is not the same as normal object . weakmap is called weak because it holds a weak reference to object and this weak reference means that when you delete the object itself > it will removed from weakmap automatically and the weak reference inside w cannot prevent gorbage collector from removing the object. look at this answer : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 10:02














0












0








0







However I have not worked with weakmap but the reason behind not deleting through



delete a.aa



is just because w is separate reference to the object rather pointer to the same object.




Simple values (aka scalar primitives) are always assigned/passed by value-copy: null, undefined, string, number, boolean, and ES6's symbol.



Compound values -- objects (including arrays, and all boxed object wrappers -- see Chapter 3) and functions -- always create a copy of the reference on assignment or passing.




consider the below snippet:



function foo(x) {
x.push( 4 );
x; // [1,2,3,4]

// later
x = [4,5,6];
x.push( 7 );
x; // [4,5,6,7]
}

var a = [1,2,3];

foo( a );

a; // [1,2,3,4] not [4,5,6,7]



When we pass in the argument a, it assigns a copy of the a reference to x. x and are a separate references pointing at the same [1,2,3] value. Now, inside the function, we can use that reference to mutate the value itself (push(4)). But when we make the assignment x = [4,5,6], this is in no way affecting where the initial reference a is pointing -- still points at the (now modified) [1,2,3,4] value.




Give it a read to this!



Hope it makes it clear!






share|improve this answer















However I have not worked with weakmap but the reason behind not deleting through



delete a.aa



is just because w is separate reference to the object rather pointer to the same object.




Simple values (aka scalar primitives) are always assigned/passed by value-copy: null, undefined, string, number, boolean, and ES6's symbol.



Compound values -- objects (including arrays, and all boxed object wrappers -- see Chapter 3) and functions -- always create a copy of the reference on assignment or passing.




consider the below snippet:



function foo(x) {
x.push( 4 );
x; // [1,2,3,4]

// later
x = [4,5,6];
x.push( 7 );
x; // [4,5,6,7]
}

var a = [1,2,3];

foo( a );

a; // [1,2,3,4] not [4,5,6,7]



When we pass in the argument a, it assigns a copy of the a reference to x. x and are a separate references pointing at the same [1,2,3] value. Now, inside the function, we can use that reference to mutate the value itself (push(4)). But when we make the assignment x = [4,5,6], this is in no way affecting where the initial reference a is pointing -- still points at the (now modified) [1,2,3,4] value.




Give it a read to this!



Hope it makes it clear!







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 7:35

























answered Jan 3 at 7:29









Basheer AhmedBasheer Ahmed

2,34531538




2,34531538













  • no weakmap is not the same as normal object . weakmap is called weak because it holds a weak reference to object and this weak reference means that when you delete the object itself > it will removed from weakmap automatically and the weak reference inside w cannot prevent gorbage collector from removing the object. look at this answer : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 10:02



















  • no weakmap is not the same as normal object . weakmap is called weak because it holds a weak reference to object and this weak reference means that when you delete the object itself > it will removed from weakmap automatically and the weak reference inside w cannot prevent gorbage collector from removing the object. look at this answer : stackoverflow.com/a/43579723/2611020

    – hamidkardorost
    Jan 3 at 10:02

















no weakmap is not the same as normal object . weakmap is called weak because it holds a weak reference to object and this weak reference means that when you delete the object itself > it will removed from weakmap automatically and the weak reference inside w cannot prevent gorbage collector from removing the object. look at this answer : stackoverflow.com/a/43579723/2611020

– hamidkardorost
Jan 3 at 10:02





no weakmap is not the same as normal object . weakmap is called weak because it holds a weak reference to object and this weak reference means that when you delete the object itself > it will removed from weakmap automatically and the weak reference inside w cannot prevent gorbage collector from removing the object. look at this answer : stackoverflow.com/a/43579723/2611020

– hamidkardorost
Jan 3 at 10:02



Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas