Using linked list to implement Nodejs memory object management

Source: Internet
Author: User

Although JavaScript has garbage collection, the garbage collection mechanism does not automatically release persistent objects, such as websocks connections.

In order to be able to abort some connections in certain situations (such as out-of-memory), it is clear that the Global object manager should be established for management.

It is clear that the doubly linked list is the most consistent data structure for managing such objects.

This implements the method of adding objects, deleting an object, and releasing all objects.

varmanager=module.exports=function(){         This. head={"Next":NULL};  This. last= This. Head;} Manager.prototype.add=function(obj) {//Adding nodes        varprev= This. Last; varnewlast={"Prev":p rev, "next":NULL, "obj": obj}; Prev.next=Newlast;  This. last=Newlast; returnNewlast;//Object removalthe handle used when}manager.prototype.remove=function(node) {//remove node by abort procedure Call of Object        varprev=Node.prev; varnext=Node.next; Prev.next=Next; if(next!=NULL) {next.prev=prev;} Node.prev=NULL; Node.obj=NULL; Node.next=NULL;} Manager.prototype.free=function(){//release all, if a counter can be implemented to release a specified number of nodes, you can try        varcur;  while((cur= This. head.next)! =NULL) {cur.obj.end ();//Abort procedure for executing objects        }}//link List Implementation Add, remove, erase

Using linked list to implement Nodejs memory object management

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.