OSDN Git Service

Add easy way to ensure the next allocation does GC.
authorMathieu Chartier <mathieuc@google.com>
Wed, 9 Jul 2014 00:46:19 +0000 (17:46 -0700)
committerMathieu Chartier <mathieuc@google.com>
Wed, 9 Jul 2014 22:03:54 +0000 (15:03 -0700)
Added a class called ScopedHeapFill which changes the bytes allocated
counter to be equal to the growth limit. This causes the next
allocation to do a GC and possibly generate an OOM error. This is
useful for tests which need GC to happen at specific point.

Change-Id: Ibd8f3d5928b58534c5165ba7c296980002aa2c28

runtime/gc/heap.h

index 6d70a38..511e9f8 100644 (file)
@@ -981,6 +981,7 @@ class Heap {
   friend class VerifyReferenceCardVisitor;
   friend class VerifyReferenceVisitor;
   friend class VerifyObjectVisitor;
+  friend class ScopedHeapFill;
   friend class ScopedHeapLock;
   friend class space::SpaceTest;
 
@@ -997,6 +998,25 @@ class Heap {
   DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
 };
 
+// ScopedHeapFill changes the bytes allocated counter to be equal to the growth limit. This
+// causes the next allocation to perform a GC and possibly an OOM. It can be used to ensure that a
+// GC happens in specific methods such as ThrowIllegalMonitorStateExceptionF in Monitor::Wait.
+class ScopedHeapFill {
+ public:
+  explicit ScopedHeapFill(Heap* heap)
+      : heap_(heap),
+        delta_(heap_->GetMaxMemory() - heap_->GetBytesAllocated()) {
+    heap_->num_bytes_allocated_.FetchAndAddSequentiallyConsistent(delta_);
+  }
+  ~ScopedHeapFill() {
+    heap_->num_bytes_allocated_.FetchAndSubSequentiallyConsistent(delta_);
+  }
+
+ private:
+  Heap* const heap_;
+  const int64_t delta_;
+};
+
 }  // namespace gc
 }  // namespace art