// File: example.js var key = {}; var indirectReference = {}; indirectReference['key'] = (function() { var m = {}; m['foo'] = 'bar'; return m; })(); var map = new WeakMap(); map.set(key, indirectReference['key']); console.log('Has key after setting value: %s', map.has(key)); delete indirectReference['key']; console.log('Has key after deleting value: %s', map.has(key)); global.gc(); console.log('Has key after performing global.gc(): %s', map.has(key));I downloaded the latest version of io.js (so that I could have a JavaScript runtime with
WeakMap
and global.gc()
and ran it as follows:
./iojs --expose-gc example.jsHere is what I see:
Has key after setting value: true Has key after deleting value: true Has key after performing global.gc(): trueDespite my best efforts, I can't seem to get
WeakMap
to give up the value that is mapped to key
. Am I doing it wrong? Obviously I'm making some assumptions here, so I'm curious where I'm off.
Ultimately, I would like to be able to use WeakMap
to write some tests to ensure certain objects get garbage collected and don't leak memory.
In the JS weakmap implementation, the keys are held by weak references, not the values? So, like, if you want to associate an Element with some Object, but you want that Object to go away if the Element goes away.
ReplyDelete(I think the API is actually specifically designed to not allow the use case you have in mind, where you're monitoring the gc, but I don't know the background reasoning behind that)
Google Ephemeron. That's what a WeakMap is.
ReplyDeleteIt's just a way of "attaching" hidden properties to the keys. When you have the map and the key you can get hold of a property, the value. When either the key or the map are GCed, the value will not be kept alive any more.
You could use ES6 symbols for the same thing, where the symbol plays the role of the WeakMap, and you use the symbol to put a property on the key:
// Solution with WeakMap. If either the map or the key are unreachable then
// the value is also unreachable (unless it's reachable some other way).
var map = new WeakMap();
map[key] = value
// Solution with Symbol. If the key becomes unreachable then the value can
// become unreachable, but if you have the key you can always get the symbol
// using getOwnPropertySymbols, so the value cannot be collected unless the
// key is collected.
var map = Symbol(); // Creates a new primitive symbol object
key[symbol] = value;
You were probably expecting WeakMap to behave like WeakReference from Java. That would mean that the JS program could detect when a GC had taken place, which would inevitably introduce compatibility issues between the JS engines. This is probably one of the reasons it hasn't happened.
Nick and Erik, yep, you're right, I was assuming WeakMap was going to behave like WeakReference from Java, so that's the source of my problems.
ReplyDeleteI suspect the only way to do what I want is to explicitly expose this functionality from V8/Node. Obviously that would introduce an incompatibility across JS engines, but my intention is to use it only for memory leak tests, so I could live with that.
The concept applies in a variety of disciplines,[5] with both the nature of the evidence or justification and the criteria for sufficiency being area-dependent. In the area of oral and written communication such as conversation, dialog. Dubai Mobile App Developer
ReplyDelete"Nice Blog!!
ReplyDeleteNorton login is one of the easiest platform that allows you to manage products download Norton antivirus and protect your device from online threats.
Norton Account
Norton Setup"
Thank you for all of your work on this web page.
ReplyDeleteufa777
ufa777
ufa777
ufa777
ufa777
ReplyDeleteufa777
ufa777
ufa777
ReplyDeleteufa777
ufa777
Interpages
ReplyDeleteGuest Blogger
Guest Blogging Site
Guest Blogging Website
Guest Posting Site
drexel
ReplyDeleteamnesty
git.rj
Cara mengecilkan perut Sugar and carbohydrates are the highest sources of saturated fat. So by reducing sugar and carbohydrates, the calories that enter the body shrink. Ngobrol Sehat It is advisable to swap sugar and carbohydrates for foods that are high in energy and low in fat. Ngobrol Sehat Having a small belly and thighs and nice as the desire of the majority of women. For those of you who have a large belly and thigh size, of course you want to have a smaller size to support your performance. Ngobrol Sehat Here's how to reduce the stomach and thighs naturally.
ReplyDeleteHidup Sehat | Hidup Sehat
The WeakMap object is a collection of value pairs in which the keys and If you want to have a list of keys. you should use a MapKinemaster Gold
ReplyDeleteThis article is really very amazing, from this post we can get more information about 6 reasons you need to unplug from technology. It was my dream to become a scientist and invent something better for our world and I am addicted to technology. It is 100% right that TV computer, smartphones become an unbreakable part of our daily life and it is the main source of our entertainment. Nobody can't compromise with these devices. The promotion of these devices depends on advertisement because it has always been the key tool to increase your product popularity. Advertising becomes a business and it has long remained unchanged. If anyone wants to zoek softwareontwikkelaar you can see more details on this. Advertising had to adjust to a new market with the internet help of software development. Do you know that the specific set of techniques and methods to boost your product through digital media?
ReplyDeleteI swear! Weakmap is taken into its literal sense. Lately; I have been busy with my studies that I don’t find time for anything else, even now I was just taking assignment writing services from https://bestassignmentwriter.co.uk/. But I felt the need to give my honest opinion about the weak map.
ReplyDeletePanther Jackets Is A Leather Jackets WareHouse Providing Jackets In USA, Canada and UK.With Free Shipping
ReplyDeleteGucci Bomber Style Black Leather Jacket
San Francisco Dons Varsity Wool Jacket
love IGI 3. The game's features that you'll love after downloading include Improved Artificial Intelligence. More improved graphics, better sound as well as SFX Music. Game Weapons are new. Game plots are engaging. There are new features in the IGI 3 Game Control Panel and missions include new objectsMetro Exodus Download Pc Game Free andc clip studio paint key and stellar data recovery standard serial key with spotify premium free pc also manycam activation code and true Korean horror films due imyfone registration code so metasploit pro activation key and avs video editor activation key as o avid media composer ultimate crackIGI 3. The game's features that you'll love after downloading include Improved Artificial Intelligence. More improved graphics, better sound as well as SFX Music. Game Weapons are new. Game plots are engaging. There are new features in the IGI 3 Game Control Panel and missions include new objects.
ReplyDeleteAwesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
ReplyDeleteHow to launch a document saver app (Digi locker), the features of a Document Saver app (Digi locker) and the cost of developing a Document Saver app (Digi locker app). Want to know more then Contact Us.
DocBook XML it was fairly easy to programmatically translate the XML into HTML For some reason I do not have a copy of the figure from Appendix B that appears in the book so I had to recreate it myself Lacking real diagramming tools at the time computer science thesis topic Students can analyze ways that deep learning and artificial intelligence can shape the future of education New programs can base education on the preferences of the learner.
ReplyDeleteDo My Homework is that they provide 24/7 customer support to their clients. Students can contact them at any time of the day, and their team of customer support agents is always available to provide assistance.
ReplyDeletetw2x pg slot ก่อนเล่นเกมสล็อต สิ่งจำเป็นเป็นจำต้องรู้เรื่องกลไก pgslot ช่องชำระเงินและก็คุณลักษณะโบนัส วิชาความรู้นี้จะช่วยทำให้ผู้เล่นสามารถปรับปรุงกลอุบายมากมายให้เลือก
ReplyDeletepg biggame1688 สล็อตเว็บไซต์ตรงไม่ผ่านเอเย่นต์ pg slot เป็นอีกหนึ่งเว็บไซต์ทำเงินชั้นหนึ่ง ที่เปิดประสบการณ์สำหรับการเข้าเล่นเกมสล็อต ที่สะดุดตาไม่เหมือนใคร เข้าเล่นง่าย
ReplyDeletekc9 สล็อต เครดิต ฟรี ในโลกของความบันเทิงและการพนันออนไลน์ Kc9 สล็อต เครดิต ฟรี โดดเด่นในฐานะโอกาสที่น่าตื่นเต้น PG SLOT สำหรับผู้เล่นที่จะชนะรางวัลใหญ่ในขณะที่เพลิดเพลินกับเกมสล็อตที่น่าหลงใหล
ReplyDeleteสล็อต แตก ง่าย ด้วยความมากมายหลากหลายของตัวเกมที่ได้มีการคัดสรรมาอย่างดีเยี่ยม pg ทำให้ไม่ยุ่งยากต่อการครอบครองรางวัลโบนัส หรือกล่าวได้ว่าเป็น ไม่มีความจำเป็นที่จะต้องมีแนวทาง
ReplyDeleteI got some valuable points through this blog. Thank you sharing this blog.Hidden Gun Storage
ReplyDeletepg slot co login สามารถทำได้ผ่านช่องทางต่างๆ ดัง1. เว็บไซต์ของ PG Slot: ท่านสามารถเข้าสู่ระบบผ่านเว็บไซต์ของ PG Slot ได้โดยตรง โดยสามารถสมัครสมาชิกได้ฟรี
ReplyDelete