Skip to content

Commit 7bf0091

Browse files
committed
Merge pull request #425 from NativeScript/jasssonpet/snapshot-data-memory
Keep snapshot data just after debugger is initialized
2 parents 36dd943 + 9507516 commit 7bf0091

File tree

4 files changed

+50
-12
lines changed

4 files changed

+50
-12
lines changed

src/jni/Runtime.cpp

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Runtime::Runtime(JNIEnv *env, jobject runtime, int id)
5858
{
5959
m_runtime = m_env.NewGlobalRef(runtime);
6060
m_objectManager = new ObjectManager(m_runtime);
61+
m_startupData = nullptr;
6162
s_id2RuntimeCache.insert(make_pair(id, this));
6263
}
6364

@@ -335,26 +336,38 @@ void Runtime::PassUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, jthrowab
335336
}
336337
}
337338

339+
void Runtime::ClearStartupData(JNIEnv *env, jobject obj) {
340+
if (m_startupData) {
341+
delete m_startupData->data;
342+
delete m_startupData;
343+
}
344+
}
345+
338346
Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName, jobject jsDebugger, jstring profilerOutputDir)
339347
{
340348
auto platform = v8::platform::CreateDefaultPlatform();
341349
V8::InitializePlatform(platform);
342350
V8::Initialize();
343351

344352
Isolate::CreateParams create_params;
345-
StartupData startup_data;
346353
string customScript;
347354

348355
create_params.array_buffer_allocator = &g_allocator;
349356
// prepare the snapshot blob
350357
if (Constants::V8_HEAP_SNAPSHOT)
351358
{
359+
DEBUG_WRITE_FORCE("Snapshot enabled.");
360+
361+
m_startupData = new StartupData();
362+
352363
auto snapshotPath = filesPath + "/internal/snapshot.blob";
353364
if( File::Exists(snapshotPath))
354365
{
355366
int length;
356-
startup_data.data = reinterpret_cast<char*>(File::ReadBinary(snapshotPath, length));
357-
startup_data.raw_size = length;
367+
m_startupData->data = reinterpret_cast<char*>(File::ReadBinary(snapshotPath, length));
368+
m_startupData->raw_size = length;
369+
370+
DEBUG_WRITE_FORCE("Snapshot read %s (%dB).", snapshotPath.c_str(), length);
358371
}
359372
else
360373
{
@@ -364,11 +377,25 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
364377
customScript = File::ReadText(Constants::V8_HEAP_SNAPSHOT_SCRIPT);
365378
}
366379

367-
startup_data = V8::CreateSnapshotDataBlob(customScript.c_str());
368-
File::WriteBinary(snapshotPath, startup_data.data, startup_data.raw_size);
380+
DEBUG_WRITE_FORCE("Creating heap snapshot");
381+
*m_startupData = V8::CreateSnapshotDataBlob(customScript.c_str());
382+
383+
if (m_startupData->raw_size == 0) {
384+
DEBUG_WRITE_FORCE("Failed to create heap snapshot.");
385+
} else {
386+
bool writeSuccess = File::WriteBinary(snapshotPath, m_startupData->data, m_startupData->raw_size);
387+
388+
if (!writeSuccess) {
389+
DEBUG_WRITE_FORCE("Failed to save created snapshot.");
390+
} else {
391+
DEBUG_WRITE_FORCE("Saved snapshot of %s (%dB) in %s (%dB)",
392+
Constants::V8_HEAP_SNAPSHOT_SCRIPT.c_str(), customScript.size(),
393+
snapshotPath.c_str(), m_startupData->raw_size);
394+
}
395+
}
369396
}
370397

371-
create_params.snapshot_blob = &startup_data;
398+
create_params.snapshot_blob = m_startupData;
372399
}
373400

374401
auto isolate = Isolate::New(create_params);
@@ -402,12 +429,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
402429
Local<Context> context = Context::New(isolate, nullptr, globalTemplate);
403430
PrimaryContext = new Persistent<Context>(isolate, context);
404431

405-
if (Constants::V8_HEAP_SNAPSHOT)
406-
{
407-
// we own the snapshot buffer, delete it
408-
delete[] startup_data.data;
409-
}
410-
411432
context_scope = new Context::Scope(context);
412433

413434
m_objectManager->Init(isolate);

src/jni/Runtime.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace tns
3939
jint GenerateNewObjectId(JNIEnv *env, jobject obj);
4040
void AdjustAmountOfExternalAllocatedMemoryNative(JNIEnv *env, jobject obj, jlong usedMemory);
4141
void PassUncaughtExceptionToJsNative(JNIEnv *env, jobject obj, jthrowable exception, jstring stackTrace);
42+
void ClearStartupData(JNIEnv *env, jobject obj);
4243

4344
private:
4445
Runtime(JNIEnv *env, jobject runtime, int id);
@@ -57,6 +58,8 @@ namespace tns
5758

5859
Profiler m_profiler;
5960

61+
v8::StartupData *m_startupData;
62+
6063
static void PrepareExtendFunction(v8::Isolate *isolate, jstring filesPath);
6164
v8::Isolate* PrepareV8Runtime(const std::string& filesPath, jstring packageName, jobject jsDebugger, jstring profilerOutputDir);
6265
jobject ConvertJsValueToJavaObject(JEnv& env, const v8::Local<v8::Value>& value, int classReturnType);

src/jni/com_tns_Runtime.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,14 @@ extern "C" void Java_com_tns_Runtime_passUncaughtExceptionToJsNative(JNIEnv *env
301301
nsEx.ReThrowToJava();
302302
}
303303
}
304+
305+
extern "C" void Java_com_tns_Runtime_ClearStartupData(JNIEnv *env, jobject obj, jint runtimeId)
306+
{
307+
auto runtime = TryGetRuntime(runtimeId);
308+
if (runtime == nullptr)
309+
{
310+
return;
311+
}
312+
313+
runtime->ClearStartupData(env, obj);
314+
}

src/src/com/tns/Runtime.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class Runtime
4141

4242
private native void passUncaughtExceptionToJsNative(int runtimeId, Throwable ex, String stackTrace);
4343

44+
private native void ClearStartupData(int runtimeId);
45+
4446
void passUncaughtExceptionToJs(Throwable ex, String stackTrace)
4547
{
4648
passUncaughtExceptionToJsNative(getRuntimeId(), ex, stackTrace);
@@ -199,6 +201,7 @@ private void init(Application application, ThreadScheduler threadScheduler, Logg
199201
{
200202
jsDebugger.start();
201203
}
204+
ClearStartupData(getRuntimeId()); // It's safe to delete the data after the V8 debugger is initialized
202205

203206
if (logger.isEnabled())
204207
{

0 commit comments

Comments
 (0)