Is it possible to mark objects so as not to collect garbage?

I don’t know much about the JavaScript garbage collector, I’m just trying to manage links so objects without links can be deleted from memory periodically. I was thinking of something that, in my opinion, could increase productivity if it were possible for the performers of the language.

It will be like this. In the file, add the line:

"no gc"; 

This is similar to the use strict setting. It will mark everything that is defined in the file, not for garbage collection. I think this will be used in libraries like jQuery and underscore. All helper methods will be marked and stored in a separate memory area that is not controlled by the GC.

While I know that this can lead to the fact that it will store things that are never used; he would at least isolate him from the periodic GC process. Therefore, although we may gobble up additional memory, we at least reduce the processing load of the GC.

I apologize for the naivety of this proposal, as I have never implemented a GC. I'm just wondering if this idea is possible or if JavaScript somehow does it already.

+6
source share
4 answers

if you want to save them as a cache, then you have a global scope.

In a browser, the global scope is a window,

therefore, consider whether you want object X to never receive garbage, but you can simply write

 window.nogc = X; 

since the window, which is a global region, will never be garbage collected, so its child links will also not be garbage marked until we explicitly do this.

+3
source

Garbage collection only starts when the stream is free. Nothing will be saved, because the GC only happens when the system is not busy.

No, It is Immpossible.

+2
source

You can guarantee that the object will not be assembled by referencing it from the root of the GC, but not by the fact that it is not processed by the GC.

The reason is that GC in JS VMs is usually implemented through a Mark-and-Sweep or method that is functionally equivalent. The basic premise is that the GC goes through a cycle that looks like this:

  • GC marks all objects on the heap as "potentially available for release." This is usually done using the flag switch, which changes the interpretation of existing labels on objects from the value "must be saved" for "safe exit". Thus, at this stage, there is no real iteration over the objects. Telling GC not to “mark” certain objects will actually require additional operations, not less.

  • The GC starts with the roots of the GC and goes through the link tree, changing the sign of the objects from "safe until release" to "need to save." This is the Mark phase. GC roots can be global objects, the current executable stack, waiting for callbacks in the event loop, etc. The tree trip itself can be done in different ways, with DFS being the easiest.

  • The GC passes all objects on the heap and deletes everything that is still marked as release safe. This is the Sweep phase. There are many optimizations in this phase, which allows the GC to free the memory used by a group of objects in a single operation. However, this requires at least some level of iteration over groups of objects, if not over the objects themselves.

Now the problem is with the non-GC arena setup: suppose the object in your non-GC arena was supposed to refer to a regular object. During the Mark phase, if this item is not checked, it will be released. This means that all your “non-collectable” objects must be GC-roots in order to “hold” the regular objects to which they belong. And, being a GC root, it has no advantages over direct reference to the GC root. Both must be equally involved in the Mark phase.

The only alternative for your non-collectible item is to not be able to fully reference collectibles. This can be achieved using your own binary heap, rather than using your own JS objects. Binary data will not be interpreted as links, and a heap object will complete its marking phase in one operation. This is what asm.js does: It predetermines a large array that will act as an internal heap. For a VM, the entire array is considered one large object, and therefore no garbage collection is performed on any data structures that are encoded inside it. Using this method has the disadvantage that you must encode all of your objects and data structures in binary heap format and decode them when you want to use them. When working with asm.js, this is handled by the compiler.

+1
source

in JavaScript, if you want to kill an object, you need to remove all links. let's say we have an object like this:

 var myobj={ obj : {} }; 

and we want a GC obj object, we have to do this:

 myobj["obj"] = null; delete myobj["obj"]; 

but if you define another variable related to this object, it will not be killed with this line. for example, if you do:

 var obj = myobj.obj; 

if you really want to delete the object, you do the same for this variable:

 obj = null; delete obj; 

then if there is no link, it will be killed. in other words, if you want your object not to get into the GC, create a link somewhere and save it privately.

0
source

All Articles