Search code examples
javascriptmultithreadingv8javascript-engine

What's the role of "Isolate" in V8? and how can it be possible to make "Isolate" isolately?


The explanations of "Isolate" what I found so far.

exp #1: http://izs.me/v8-docs/classv8_1_1Isolate.html

"Isolate represents an isolated instance of the V8 engine. V8 isolates have completely separate states. Objects from one isolate must not be used in other isolates. When V8 is initialized a default isolate is implicitly created and entered. The embedder can create additional isolates and use them in parallel in multiple threads. An isolate can be entered by at most one thread at any given time. The Locker/Unlocker API can be used to synchronize."

exp #2: https://developers.google.com/v8/get_started

"An isolate is a VM instance with its own heap."

OK, I see. "Isolate" is an isolate thread that can operate seperately. Followings are my questions.

  1. It looks like just thread for me, except that it has its own heap. is there any difference?

  2. I think "Isolate" can be used for implementing concurrent GC. The definition above says that each "Isolate" cannot be used in other "Isolate". But concurrent GC should check(or mark) the main(or other) thread(or Isolate)'s live objects. How can it be possible?

  3. How can it be possible to protect their own objects? "Isolate" is a thread not a process. So other thread can access that thread's object if it knows the address. How could protect it? And I cannot understand the meaning of own heap. Because it can be accessed by other thread if other thread knows the address. And normal thread can have their heap in memory space. Since address space of heap is not seperated exactly but if one thread malloc a memory, how could other thread use it unless others know the address? What's the difference each thread just malloc their own heap space and "Isolate" have its own heap space?

My questions can be easily summarized that what is the role of the "Isolate" and how can it be possible to have their own heap space and why does it have to have its own heap.

It will be very helpful if someone shares some good documentations of "Isolate". Thanks for reading.

---- Make the question clear ---- The key point of my question is that Q: What makes google to implement isolate in V8? what's the benefit of isolate and what would be a good example of using isolate in V8? What are they(isolates) executing concurrently?


Solution

    1. It looks like just thread for me, except that it has its own heap. is there any difference?

    They are orthogonal, a thread can execute multiple isolates at a time while an isolate can only be executed by one thread at a time. And of course one isolate can be executed by different threads at different times but it's probably not common. An isolate is only an instance of a JavaScript VM and it only has its own JavaScript heap, the normal process heap is still shared in the process as normal.

    1. I think "Isolate" can be used for implementing concurrent GC. The definition above says that each "Isolate" cannot be used in other "Isolate". But concurrent GC should check(or mark) the main(or other) thread(or Isolate)'s live objects. How can it be possible?

    Noncompacting sweep operation in a Mark&Sweep GC can be performed concurrently from another thread. Other GC operations, like compacting sweep, scavenge, marking can only be performed while JS is not executing in an isolate.

    1. How can it be possible to protect their own objects? "Isolate" is a thread not a process. So other thread can access that thread's object if it knows the address. How could protect it? And I cannot understand the meaning of own heap. Because it can be accessed by other thread if other thread knows the address. And normal thread can have their heap in memory space. Since address space of heap is not seperated exactly but if one thread malloc a memory, how could other thread use it unless others know the address? What's the difference each thread just malloc their own heap space and "Isolate" have its own heap space?

    Well you don't know the address and it's not possible to get it sticking to V8 API. Even if you could get the address, the address is not safe to use because V8 moves stuff in its heap constantly. And malloc will not return addresses that point to some isolate's js heap because obviously that memory has been malloced by the isolate.