OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 TRACE_EVENT_SET_SAMPLING_STATE("v8", "V8MajorGC"); | 375 TRACE_EVENT_SET_SAMPLING_STATE("v8", "V8MajorGC"); |
376 } else { | 376 } else { |
377 MajorGCWrapperVisitor visitor(isolate, constructRetainedObjectInfos); | 377 MajorGCWrapperVisitor visitor(isolate, constructRetainedObjectInfos); |
378 v8::V8::VisitHandlesWithClassIds(isolate, &visitor); | 378 v8::V8::VisitHandlesWithClassIds(isolate, &visitor); |
379 visitor.notifyFinished(); | 379 visitor.notifyFinished(); |
380 } | 380 } |
381 } | 381 } |
382 | 382 |
383 void V8GCController::gcEpilogue(v8::GCType type, v8::GCCallbackFlags flags) | 383 void V8GCController::gcEpilogue(v8::GCType type, v8::GCCallbackFlags flags) |
384 { | 384 { |
385 // v8::kGCCallbackFlagForced forces a Blink heap garbage collection | |
386 // when a garbage collection was forced from V8. This is either used | |
387 // for tests that force GCs from/ JavaScript to verify that objects die | |
388 // when expected, or when handling memory pressure notifications. | |
389 bool forceGC = flags & v8::kGCCallbackFlagForced; | |
390 | |
391 // FIXME: It would be nice if the GC callbacks passed the Isolate directly..
.. | 385 // FIXME: It would be nice if the GC callbacks passed the Isolate directly..
.. |
392 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 386 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
393 if (type == v8::kGCTypeScavenge) { | 387 if (type == v8::kGCTypeScavenge) { |
394 minorGCEpilogue(isolate); | 388 minorGCEpilogue(isolate); |
395 } else if (type == v8::kGCTypeMarkSweepCompact) { | 389 } else if (type == v8::kGCTypeMarkSweepCompact) { |
396 majorGCEpilogue(isolate); | 390 majorGCEpilogue(isolate); |
397 ThreadState::current()->didV8MajorGC(forceGC); | 391 ThreadState::current()->didV8GC(); |
| 392 } |
| 393 |
| 394 // Forces a Blink heap garbage collection when a garbage collection |
| 395 // was forced from V8. This is used for tests that force GCs from |
| 396 // JavaScript to verify that objects die when expected. |
| 397 if (flags & v8::kGCCallbackFlagForced) { |
| 398 // This single GC is not enough for two reasons: |
| 399 // (1) The GC is not precise because the GC scans on-stack pointers co
nservatively. |
| 400 // (2) One GC is not enough to break a chain of persistent handles. It
's possible that |
| 401 // some heap allocated objects own objects that contain persistent
handles |
| 402 // pointing to other heap allocated objects. To break the chain, w
e need multiple GCs. |
| 403 // |
| 404 // Regarding (1), we force a precise GC at the end of the current event
loop. So if you want |
| 405 // to collect all garbage, you need to wait until the next event loop. |
| 406 // Regarding (2), it would be OK in practice to trigger only one GC per
gcEpilogue, because |
| 407 // GCController.collectAll() forces 7 V8's GC. |
| 408 Heap::collectGarbage(ThreadState::HeapPointersOnStack, ThreadState::GCWi
thSweep, Heap::ForcedGC); |
| 409 |
| 410 // Forces a precise GC at the end of the current event loop. |
| 411 ThreadState::current()->setGCState(ThreadState::FullGCScheduled); |
398 } | 412 } |
399 | 413 |
400 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "Update
Counters", TRACE_EVENT_SCOPE_THREAD, "data", InspectorUpdateCountersEvent::data(
)); | 414 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("devtools.timeline"), "Update
Counters", TRACE_EVENT_SCOPE_THREAD, "data", InspectorUpdateCountersEvent::data(
)); |
401 } | 415 } |
402 | 416 |
403 void V8GCController::minorGCEpilogue(v8::Isolate* isolate) | 417 void V8GCController::minorGCEpilogue(v8::Isolate* isolate) |
404 { | 418 { |
405 TRACE_EVENT_END1("devtools.timeline,v8", "MinorGC", "usedHeapSizeAfter", use
dHeapSize(isolate)); | 419 TRACE_EVENT_END1("devtools.timeline,v8", "MinorGC", "usedHeapSizeAfter", use
dHeapSize(isolate)); |
406 if (isMainThread()) { | 420 if (isMainThread()) { |
407 TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(V8PerIsolateData::from(isolate)-
>previousSamplingState()); | 421 TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(V8PerIsolateData::from(isolate)-
>previousSamplingState()); |
408 ScriptForbiddenScope::exit(); | 422 ScriptForbiddenScope::exit(); |
409 } | 423 } |
410 } | 424 } |
411 | 425 |
412 void V8GCController::majorGCEpilogue(v8::Isolate* isolate) | 426 void V8GCController::majorGCEpilogue(v8::Isolate* isolate) |
413 { | 427 { |
414 TRACE_EVENT_END1("devtools.timeline,v8", "MajorGC", "usedHeapSizeAfter", use
dHeapSize(isolate)); | 428 TRACE_EVENT_END1("devtools.timeline,v8", "MajorGC", "usedHeapSizeAfter", use
dHeapSize(isolate)); |
415 if (isMainThread()) { | 429 if (isMainThread()) { |
416 TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(V8PerIsolateData::from(isolate)-
>previousSamplingState()); | 430 TRACE_EVENT_SET_NONCONST_SAMPLING_STATE(V8PerIsolateData::from(isolate)-
>previousSamplingState()); |
417 ScriptForbiddenScope::exit(); | 431 ScriptForbiddenScope::exit(); |
| 432 |
| 433 // Schedule an Oilpan GC to avoid the following scenario: |
| 434 // (1) A DOM object X holds a v8::Persistent to a V8 object. |
| 435 // Assume that X is small but the V8 object is huge. |
| 436 // The v8::Persistent is released when X is destructed. |
| 437 // (2) X's DOM wrapper is created. |
| 438 // (3) The DOM wrapper becomes unreachable. |
| 439 // (4) V8 triggers a GC. The V8's GC collects the DOM wrapper. |
| 440 // However, X is not collected until a next Oilpan's GC is |
| 441 // triggered. |
| 442 // (5) If a lot of such DOM objects are created, we end up with |
| 443 // a situation where V8's GC collects the DOM wrappers but |
| 444 // the DOM objects are not collected forever. (Note that |
| 445 // Oilpan's GC is not triggered unless Oilpan's heap gets full.) |
| 446 // (6) V8 hits OOM. |
| 447 #if ENABLE(IDLE_GC) |
| 448 ThreadState::current()->scheduleIdleGC(); |
| 449 #else |
| 450 ThreadState::current()->schedulePreciseGC(); |
| 451 #endif |
418 } | 452 } |
419 } | 453 } |
420 | 454 |
421 void V8GCController::collectGarbage(v8::Isolate* isolate) | 455 void V8GCController::collectGarbage(v8::Isolate* isolate) |
422 { | 456 { |
423 v8::HandleScope handleScope(isolate); | 457 v8::HandleScope handleScope(isolate); |
424 RefPtr<ScriptState> scriptState = ScriptState::create(v8::Context::New(isola
te), DOMWrapperWorld::create(isolate)); | 458 RefPtr<ScriptState> scriptState = ScriptState::create(v8::Context::New(isola
te), DOMWrapperWorld::create(isolate)); |
425 ScriptState::Scope scope(scriptState.get()); | 459 ScriptState::Scope scope(scriptState.get()); |
426 V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, "if (gc) gc();
"), isolate); | 460 V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, "if (gc) gc();
"), isolate); |
427 scriptState->disposePerContextData(); | 461 scriptState->disposePerContextData(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 Visitor* m_visitor; | 497 Visitor* m_visitor; |
464 }; | 498 }; |
465 | 499 |
466 void V8GCController::traceDOMWrappers(v8::Isolate* isolate, Visitor* visitor) | 500 void V8GCController::traceDOMWrappers(v8::Isolate* isolate, Visitor* visitor) |
467 { | 501 { |
468 DOMWrapperTracer tracer(visitor); | 502 DOMWrapperTracer tracer(visitor); |
469 v8::V8::VisitHandlesWithClassIds(isolate, &tracer); | 503 v8::V8::VisitHandlesWithClassIds(isolate, &tracer); |
470 } | 504 } |
471 | 505 |
472 } // namespace blink | 506 } // namespace blink |
OLD | NEW |