1

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

  • thanks to @komal-bansal this question has been answered already in this link . so flagged as duplicate : https://stackoverflow.com/a/49841518/2611020 – hamid reza jafari Jan 03 '19 at 10:49

3 Answers3

3

Your delete() function should look like this:

 w.delete(a.aa);
Ben
  • 2,441
  • 1
  • 12
  • 16
  • 1
    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 : https://stackoverflow.com/a/43579723/2611020 – hamid reza jafari Jan 03 '19 at 09: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

Komal Bansal
  • 789
  • 2
  • 7
  • 20
  • 1
    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 : https://stackoverflow.com/a/43579723/2611020 – hamid reza jafari Jan 03 '19 at 09:58
  • 2
    I think you can get , what you are looking for from here https://stackoverflow.com/questions/49841096/javascriptes6-weakmap-garbage-collection-when-set-an-object-to-null – Komal Bansal Jan 03 '19 at 10:30
  • oh my god . yes that's the answer . thank you so very much – hamid reza jafari Jan 03 '19 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!

Community
  • 1
  • 1
Basheer Kharoti
  • 4,202
  • 5
  • 24
  • 50
  • 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 : https://stackoverflow.com/a/43579723/2611020 – hamid reza jafari Jan 03 '19 at 10:02