blob: 4d8d176683d1753214eb2c85b20a25da3b0bf226 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "heap.h"
Carl Shapiro58551df2011-07-24 03:09:51 -07004
5#include <vector>
6
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include "UniquePtr.h"
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07008#include "image.h"
Carl Shapiro58551df2011-07-24 03:09:51 -07009#include "mark_sweep.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "object.h"
11#include "space.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070012#include "stl_util.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070013#include "thread_list.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070014
15namespace art {
16
Carl Shapiro58551df2011-07-24 03:09:51 -070017std::vector<Space*> Heap::spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
Brian Carlstroma663ea52011-08-19 23:33:41 -070019Space* Heap::boot_space_ = NULL;
20
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070021Space* Heap::alloc_space_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -070022
23size_t Heap::maximum_size_ = 0;
24
Carl Shapiro58551df2011-07-24 03:09:51 -070025size_t Heap::num_bytes_allocated_ = 0;
26
27size_t Heap::num_objects_allocated_ = 0;
28
Carl Shapiro69759ea2011-07-21 18:13:35 -070029bool Heap::is_gc_running_ = false;
30
31HeapBitmap* Heap::mark_bitmap_ = NULL;
32
33HeapBitmap* Heap::live_bitmap_ = NULL;
34
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070035MemberOffset Heap::reference_referent_offset_ = MemberOffset(0);
36MemberOffset Heap::reference_queue_offset_ = MemberOffset(0);
37MemberOffset Heap::reference_queueNext_offset_ = MemberOffset(0);
38MemberOffset Heap::reference_pendingNext_offset_ = MemberOffset(0);
39MemberOffset Heap::finalizer_reference_zombie_offset_ = MemberOffset(0);
Brian Carlstrom1f870082011-08-23 16:02:11 -070040
Elliott Hughes92b3b562011-09-08 16:32:26 -070041Mutex* Heap::lock_ = NULL;
42
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070043bool Heap::verify_objects_ = false;
44
Elliott Hughes92b3b562011-09-08 16:32:26 -070045class ScopedHeapLock {
46 public:
47 ScopedHeapLock() {
48 Heap::Lock();
49 }
50
51 ~ScopedHeapLock() {
52 Heap::Unlock();
53 }
54};
55
Elliott Hughesbe759c62011-09-08 19:38:21 -070056void Heap::Init(size_t initial_size, size_t maximum_size,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070057 const char* boot_image_file_name,
58 std::vector<const char*>& image_file_names) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070059 Space* boot_space;
60 byte* requested_base;
61 if (boot_image_file_name == NULL) {
62 boot_space = NULL;
63 requested_base = NULL;
64 } else {
65 boot_space = Space::Create(boot_image_file_name);
66 if (boot_space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070067 LOG(FATAL) << "Failed to create space from " << boot_image_file_name;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070068 }
69 spaces_.push_back(boot_space);
70 requested_base = boot_space->GetBase() + RoundUp(boot_space->Size(), kPageSize);
71 }
72
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070073 std::vector<Space*> image_spaces;
74 for (size_t i = 0; i < image_file_names.size(); i++) {
75 Space* space = Space::Create(image_file_names[i]);
76 if (space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070077 LOG(FATAL) << "Failed to create space from " << image_file_names[i];
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070078 }
79 image_spaces.push_back(space);
80 spaces_.push_back(space);
81 requested_base = space->GetBase() + RoundUp(space->Size(), kPageSize);
82 }
83
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070084 Space* space = Space::Create(initial_size, maximum_size, requested_base);
Carl Shapiro58551df2011-07-24 03:09:51 -070085 if (space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070086 LOG(FATAL) << "Failed to create alloc space";
Carl Shapiro69759ea2011-07-21 18:13:35 -070087 }
88
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070089 if (boot_space == NULL) {
90 boot_space = space;
91 }
92 byte* base = std::min(boot_space->GetBase(), space->GetBase());
93 byte* limit = std::max(boot_space->GetLimit(), space->GetLimit());
94 DCHECK_LT(base, limit);
95 size_t num_bytes = limit - base;
Carl Shapiro69759ea2011-07-21 18:13:35 -070096
97 // Allocate the initial live bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -070098 UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes));
99 if (live_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700100 LOG(FATAL) << "Failed to create live bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700101 }
102
103 // Allocate the initial mark bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -0700104 UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes));
105 if (mark_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700106 LOG(FATAL) << "Failed to create mark bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700107 }
108
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700109 alloc_space_ = space;
Carl Shapiro58551df2011-07-24 03:09:51 -0700110 spaces_.push_back(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700111 maximum_size_ = maximum_size;
112 live_bitmap_ = live_bitmap.release();
113 mark_bitmap_ = mark_bitmap.release();
114
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700115 num_bytes_allocated_ = 0;
116 num_objects_allocated_ = 0;
117
Carl Shapiro69759ea2011-07-21 18:13:35 -0700118 // TODO: allocate the card table
119
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700120 // Make objects in boot_space live (after live_bitmap_ is set)
121 if (boot_image_file_name != NULL) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700122 boot_space_ = boot_space;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700123 RecordImageAllocations(boot_space);
124 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700125 for (size_t i = 0; i < image_spaces.size(); i++) {
126 RecordImageAllocations(image_spaces[i]);
127 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700128
Elliott Hughes85d15452011-09-16 17:33:01 -0700129 Heap::EnableObjectValidation();
130
Elliott Hughes92b3b562011-09-08 16:32:26 -0700131 // It's still to early to take a lock because there are no threads yet,
132 // but we can create the heap lock now. We don't create it earlier to
133 // make it clear that you can't use locks during heap initialization.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700134 lock_ = new Mutex("Heap lock");
Carl Shapiro69759ea2011-07-21 18:13:35 -0700135}
136
137void Heap::Destroy() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700138 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700139 STLDeleteElements(&spaces_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700140 if (mark_bitmap_ != NULL) {
141 delete mark_bitmap_;
142 mark_bitmap_ = NULL;
143 }
144 if (live_bitmap_ != NULL) {
145 delete live_bitmap_;
146 }
147 live_bitmap_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700148}
149
Carl Shapiro58551df2011-07-24 03:09:51 -0700150Object* Heap::AllocObject(Class* klass, size_t num_bytes) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700151 ScopedHeapLock lock;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700152 DCHECK(klass == NULL
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700153 || klass->GetDescriptor() == NULL
Brian Carlstrom4873d462011-08-21 15:23:39 -0700154 || (klass->IsClassClass() && num_bytes >= sizeof(Class))
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 || (klass->IsVariableSize() || klass->GetObjectSize() == num_bytes));
156 DCHECK(num_bytes >= sizeof(Object));
Elliott Hughes92b3b562011-09-08 16:32:26 -0700157 Object* obj = AllocateLocked(num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700158 if (obj != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700159 obj->SetClass(klass);
Carl Shapiro58551df2011-07-24 03:09:51 -0700160 }
161 return obj;
162}
163
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700164bool Heap::IsHeapAddress(const Object* obj) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700165 // Note: we deliberately don't take the lock here, and mustn't test anything that would
166 // require taking the lock.
Elliott Hughesa2501992011-08-26 19:39:54 -0700167 if (!IsAligned(obj, kObjectAlignment)) {
168 return false;
169 }
170 // TODO
171 return true;
172}
173
Elliott Hughes3e465b12011-09-02 18:26:12 -0700174#if VERIFY_OBJECT_ENABLED
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700175void Heap::VerifyObject(const Object* obj) {
Elliott Hughes85d15452011-09-16 17:33:01 -0700176 if (!verify_objects_) {
177 return;
178 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700179 ScopedHeapLock lock;
180 Heap::VerifyObjectLocked(obj);
181}
182#endif
183
184void Heap::VerifyObjectLocked(const Object* obj) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700185 lock_->AssertHeld();
Elliott Hughes85d15452011-09-16 17:33:01 -0700186 if (obj != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 if (!IsAligned(obj, kObjectAlignment)) {
188 LOG(FATAL) << "Object isn't aligned: " << obj;
189 } else if (!live_bitmap_->Test(obj)) {
190 // TODO: we don't hold a lock here as it is assumed the live bit map
191 // isn't changing if the mutator is running.
192 LOG(FATAL) << "Object is dead: " << obj;
193 }
194 // Ignore early dawn of the universe verifications
Brian Carlstromdbc05252011-09-09 01:59:59 -0700195 if (num_objects_allocated_ > 10) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700196 const byte* raw_addr = reinterpret_cast<const byte*>(obj) +
197 Object::ClassOffset().Int32Value();
198 const Class* c = *reinterpret_cast<Class* const *>(raw_addr);
199 if (c == NULL) {
200 LOG(FATAL) << "Null class" << " in object: " << obj;
201 } else if (!IsAligned(c, kObjectAlignment)) {
202 LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj;
203 } else if (!live_bitmap_->Test(c)) {
204 LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj;
205 }
206 // Check obj.getClass().getClass() == obj.getClass().getClass().getClass()
207 // NB we don't use the accessors here as they have internal sanity checks
208 // that we don't want to run
209 raw_addr = reinterpret_cast<const byte*>(c) +
210 Object::ClassOffset().Int32Value();
211 const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr);
212 raw_addr = reinterpret_cast<const byte*>(c_c) +
213 Object::ClassOffset().Int32Value();
214 const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr);
215 CHECK_EQ(c_c, c_c_c);
216 }
217 }
218}
219
Brian Carlstrom78128a62011-09-15 17:21:19 -0700220void Heap::VerificationCallback(Object* obj, void* arg) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700221 DCHECK(obj != NULL);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700222 Heap::VerifyObjectLocked(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223}
224
225void Heap::VerifyHeap() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700226 ScopedHeapLock lock;
227 live_bitmap_->Walk(Heap::VerificationCallback, NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700228}
229
Elliott Hughes92b3b562011-09-08 16:32:26 -0700230void Heap::RecordAllocationLocked(Space* space, const Object* obj) {
231#ifndef NDEBUG
232 if (Runtime::Current()->IsStarted()) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700233 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700234 }
235#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700236 size_t size = space->AllocationSize(obj);
237 DCHECK_NE(size, 0u);
238 num_bytes_allocated_ += size;
239 num_objects_allocated_ += 1;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700240
241 if (Runtime::Current()->HasStatsEnabled()) {
242 RuntimeStats* global_stats = Runtime::Current()->GetStats();
243 RuntimeStats* thread_stats = Thread::Current()->GetStats();
244 ++global_stats->allocated_objects;
245 ++thread_stats->allocated_objects;
246 global_stats->allocated_bytes += size;
247 thread_stats->allocated_bytes += size;
248 }
249
Carl Shapiro58551df2011-07-24 03:09:51 -0700250 live_bitmap_->Set(obj);
251}
252
Elliott Hughes92b3b562011-09-08 16:32:26 -0700253void Heap::RecordFreeLocked(Space* space, const Object* obj) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700254 lock_->AssertHeld();
Carl Shapiro58551df2011-07-24 03:09:51 -0700255 size_t size = space->AllocationSize(obj);
256 DCHECK_NE(size, 0u);
257 if (size < num_bytes_allocated_) {
258 num_bytes_allocated_ -= size;
259 } else {
260 num_bytes_allocated_ = 0;
261 }
262 live_bitmap_->Clear(obj);
263 if (num_objects_allocated_ > 0) {
264 num_objects_allocated_ -= 1;
265 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700266
267 if (Runtime::Current()->HasStatsEnabled()) {
268 RuntimeStats* global_stats = Runtime::Current()->GetStats();
269 RuntimeStats* thread_stats = Thread::Current()->GetStats();
270 ++global_stats->freed_objects;
271 ++thread_stats->freed_objects;
272 global_stats->freed_bytes += size;
273 thread_stats->freed_bytes += size;
274 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700275}
276
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700277void Heap::RecordImageAllocations(Space* space) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700278 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700279 CHECK(space != NULL);
280 CHECK(live_bitmap_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700281 byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700282 while (current < space->GetLimit()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 DCHECK(IsAligned(current, kObjectAlignment));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700284 const Object* obj = reinterpret_cast<const Object*>(current);
285 live_bitmap_->Set(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700286 current += RoundUp(obj->SizeOf(), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700287 }
288}
289
Elliott Hughes92b3b562011-09-08 16:32:26 -0700290Object* Heap::AllocateLocked(size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700291 lock_->AssertHeld();
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700292 DCHECK(alloc_space_ != NULL);
293 Space* space = alloc_space_;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700294 Object* obj = AllocateLocked(space, size);
Carl Shapiro58551df2011-07-24 03:09:51 -0700295 if (obj != NULL) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700296 RecordAllocationLocked(space, obj);
Carl Shapiro58551df2011-07-24 03:09:51 -0700297 }
298 return obj;
299}
300
Elliott Hughes92b3b562011-09-08 16:32:26 -0700301Object* Heap::AllocateLocked(Space* space, size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700302 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700303
Carl Shapiro69759ea2011-07-21 18:13:35 -0700304 // Fail impossible allocations. TODO: collect soft references.
305 if (size > maximum_size_) {
306 return NULL;
307 }
308
Carl Shapiro58551df2011-07-24 03:09:51 -0700309 Object* ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700310 if (ptr != NULL) {
311 return ptr;
312 }
313
314 // The allocation failed. If the GC is running, block until it
315 // completes and retry.
316 if (is_gc_running_) {
317 // The GC is concurrently tracing the heap. Release the heap
318 // lock, wait for the GC to complete, and retrying allocating.
319 WaitForConcurrentGcToComplete();
Carl Shapiro58551df2011-07-24 03:09:51 -0700320 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700321 if (ptr != NULL) {
322 return ptr;
323 }
324 }
325
326 // Another failure. Our thread was starved or there may be too many
327 // live objects. Try a foreground GC. This will have no effect if
328 // the concurrent GC is already running.
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700329 if (Runtime::Current()->HasStatsEnabled()) {
330 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
331 ++Thread::Current()->GetStats()->gc_for_alloc_count;
332 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700333 CollectGarbageInternal();
334 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700335 if (ptr != NULL) {
336 return ptr;
337 }
338
339 // Even that didn't work; this is an exceptional state.
340 // Try harder, growing the heap if necessary.
Carl Shapiro58551df2011-07-24 03:09:51 -0700341 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700342 if (ptr != NULL) {
343 //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
Carl Shapiro58551df2011-07-24 03:09:51 -0700344 size_t new_footprint = space->MaxAllowedFootprint();
345 // TODO: may want to grow a little bit more so that the amount of
346 // free space is equal to the old free space + the
347 // utilization slop for the new allocation.
348 LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB
Carl Shapiro69759ea2011-07-21 18:13:35 -0700349 << "for " << size << "-byte allocation";
350 return ptr;
351 }
352
353 // Most allocations should have succeeded by now, so the heap is
354 // really full, really fragmented, or the requested size is really
355 // big. Do another GC, collecting SoftReferences this time. The VM
356 // spec requires that all SoftReferences have been collected and
357 // cleared before throwing an OOME.
358
Carl Shapiro58551df2011-07-24 03:09:51 -0700359 // TODO: wait for the finalizers from the previous GC to finish
Carl Shapiro69759ea2011-07-21 18:13:35 -0700360 LOG(INFO) << "Forcing collection of SoftReferences for "
361 << size << "-byte allocation";
Carl Shapiro58551df2011-07-24 03:09:51 -0700362 CollectGarbageInternal();
363 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700364 if (ptr != NULL) {
365 return ptr;
366 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700367
Carl Shapiro69759ea2011-07-21 18:13:35 -0700368 LOG(ERROR) << "Out of memory on a " << size << " byte allocation";
369
Carl Shapiro58551df2011-07-24 03:09:51 -0700370 // TODO: tell the HeapSource to dump its state
371 // TODO: dump stack traces for all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700372
Carl Shapiro69759ea2011-07-21 18:13:35 -0700373 return NULL;
374}
375
Elliott Hughesbf86d042011-08-31 17:53:14 -0700376int64_t Heap::GetMaxMemory() {
377 UNIMPLEMENTED(WARNING);
378 return 0;
379}
380
381int64_t Heap::GetTotalMemory() {
382 UNIMPLEMENTED(WARNING);
383 return 0;
384}
385
386int64_t Heap::GetFreeMemory() {
387 UNIMPLEMENTED(WARNING);
388 return 0;
389}
390
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700391class InstanceCounter {
392 public:
393 InstanceCounter(Class* c, bool count_assignable)
394 : class_(c), count_assignable_(count_assignable), count_(0) {
395 }
396
397 size_t GetCount() {
398 return count_;
399 }
400
401 static void Callback(Object* o, void* arg) {
402 reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o);
403 }
404
405 private:
406 void VisitInstance(Object* o) {
407 Class* instance_class = o->GetClass();
408 if (count_assignable_) {
409 if (instance_class == class_) {
410 ++count_;
411 }
412 } else {
413 if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) {
414 ++count_;
415 }
416 }
417 }
418
419 Class* class_;
420 bool count_assignable_;
421 size_t count_;
422};
423
424int64_t Heap::CountInstances(Class* c, bool count_assignable) {
425 ScopedHeapLock lock;
426 InstanceCounter counter(c, count_assignable);
427 live_bitmap_->Walk(InstanceCounter::Callback, &counter);
428 return counter.GetCount();
429}
430
Carl Shapiro69759ea2011-07-21 18:13:35 -0700431void Heap::CollectGarbage() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700432 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700433 CollectGarbageInternal();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700434}
435
436void Heap::CollectGarbageInternal() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700437 lock_->AssertHeld();
Carl Shapiro58551df2011-07-24 03:09:51 -0700438
Elliott Hughes8d768a92011-09-14 16:35:25 -0700439 ThreadList* thread_list = Runtime::Current()->GetThreadList();
440 thread_list->SuspendAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700441 {
442 MarkSweep mark_sweep;
443
444 mark_sweep.Init();
445
446 mark_sweep.MarkRoots();
447
448 // Push marked roots onto the mark stack
449
450 // TODO: if concurrent
451 // unlock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700452 // thread_list->ResumeAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700453
454 mark_sweep.RecursiveMark();
455
456 // TODO: if concurrent
457 // lock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700458 // thread_list->SuspendAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700459 // re-mark root set
460 // scan dirty objects
461
462 mark_sweep.ProcessReferences(false);
463
464 // TODO: swap bitmaps
465
466 mark_sweep.Sweep();
467 }
468
469 GrowForUtilization();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700470 thread_list->ResumeAll();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700471}
472
473void Heap::WaitForConcurrentGcToComplete() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700474 lock_->AssertHeld();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700475}
476
477// Given the current contents of the active heap, increase the allowed
478// heap footprint to match the target utilization ratio. This should
479// only be called immediately after a full garbage collection.
480void Heap::GrowForUtilization() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700481 lock_->AssertHeld();
Elliott Hughes53b61312011-08-12 18:28:20 -0700482 UNIMPLEMENTED(ERROR);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700483}
484
Elliott Hughes92b3b562011-09-08 16:32:26 -0700485void Heap::Lock() {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700486 // TODO: grab the lock, but put ourselves into Thread::kVmWait if it looks like
Elliott Hughes92b3b562011-09-08 16:32:26 -0700487 // we're going to have to wait on the mutex.
488 lock_->Lock();
489}
490
491void Heap::Unlock() {
492 lock_->Unlock();
493}
494
Carl Shapiro69759ea2011-07-21 18:13:35 -0700495} // namespace art