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