简体   繁体   中英

how many v8 isolates can I create?

I need to have thousand of isolates within V8. But I have encountered weird problem with memory, when I have some amount of free RAM, but v8 throws OOM error messages.

For example,

在此输入图像描述

In this particular case, you can see we still have some RAM when V8 throws this error. Number 2047 - iterator number. So basically code looks like:

for(int i = 0; i < 3000; i++) {
   std::cout << i << std::endl;
   new Isolate(params);
}

Maybe I need to increase stack size limit. I have set it already to 16Mb. And still have this problem. htop shows just 4 threads for this process. So I don't think that the problem with threads.

Inside v8 class I do

  this->_maxRAMAvailable = 8; // 8Gb in my case
  int maxOldSpaceMb = this->_maxRAMAvailable * 1024;
  int maxSemiSpaceMb = 512;
  int maxExecutableSizeMb = 512;

  v8::V8::InitializeICUDefaultLocation(argv[0]);
  v8::V8::InitializeExternalStartupData(argv[0]);
  this->_platform = v8::platform::CreateDefaultPlatform();
  this->_create_params.array_buffer_allocator =
  v8::ArrayBuffer::Allocator::NewDefaultAllocator();

  const uint64_t physical_memory = this->_maxRAMAvailable * 1024 * 1024 * 1024;
  const uint64_t virtual_memory_limit = 0;
  this->_create_params.constraints.ConfigureDefaults(physical_memory, virtual_memory_limit);

  this->_create_params.constraints.set_max_old_space_size(maxOldSpaceMb);
  this->_create_params.constraints.set_max_semi_space_size(maxSemiSpaceMb);

this->_create_params.constraints.set_max_executable_size(maxExecutableSizeMb);

  v8::V8::InitializePlatform(this->_platform);
  v8::V8::Initialize();

Many thanks!

Information is limited here, but let's remember that each Isolate requires its own thread, (and presumably another thread for GC?)

So 1723 Isolates is going to mean ~3400 threads...

Each thread will require (a normal default of) 1MB of stack.

So that's 3.4GB of memory allocated right there.

echo 600000 > /proc/sys/vm/max_map_count

works for me.

Each V8 isolate reserves a large block of address space, so once you've created some number of isolates, further attempts may fail due to address space exhaustion rather than actual memory usage.

In a 32-bit process you can only create 50-100 isolates before most OS's start failing V8's reservation requests. 64-bit V8 obviously has much more address space to work with, but it reserves much larger blocks too.

Also, it doesn't make sense to create thousands of isolates unless you intend to run thousands of scripts simultaneously, which would require thousands of threads. I suggest using a robust thread pool to schedule your scripts, and creating per-thread V8 isolates on demand.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM