blob: 5e96a79aaab48acad21a18bb70a708c7ecc72207 [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
Brian Carlstrom395520e2011-09-25 19:35:00 -070041float Heap::target_utilization_ = 0.5;
42
Elliott Hughes92b3b562011-09-08 16:32:26 -070043Mutex* Heap::lock_ = NULL;
44
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070045bool Heap::verify_objects_ = false;
46
Elliott Hughes92b3b562011-09-08 16:32:26 -070047class ScopedHeapLock {
48 public:
49 ScopedHeapLock() {
50 Heap::Lock();
51 }
52
53 ~ScopedHeapLock() {
54 Heap::Unlock();
55 }
56};
57
Elliott Hughesbe759c62011-09-08 19:38:21 -070058void Heap::Init(size_t initial_size, size_t maximum_size,
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070059 const char* boot_image_file_name,
60 std::vector<const char*>& image_file_names) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070061 Space* boot_space;
62 byte* requested_base;
63 if (boot_image_file_name == NULL) {
64 boot_space = NULL;
65 requested_base = NULL;
66 } else {
67 boot_space = Space::Create(boot_image_file_name);
68 if (boot_space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070069 LOG(FATAL) << "Failed to create space from " << boot_image_file_name;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070070 }
71 spaces_.push_back(boot_space);
72 requested_base = boot_space->GetBase() + RoundUp(boot_space->Size(), kPageSize);
73 }
74
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070075 std::vector<Space*> image_spaces;
76 for (size_t i = 0; i < image_file_names.size(); i++) {
77 Space* space = Space::Create(image_file_names[i]);
78 if (space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070079 LOG(FATAL) << "Failed to create space from " << image_file_names[i];
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070080 }
81 image_spaces.push_back(space);
82 spaces_.push_back(space);
83 requested_base = space->GetBase() + RoundUp(space->Size(), kPageSize);
84 }
85
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070086 Space* space = Space::Create(initial_size, maximum_size, requested_base);
Carl Shapiro58551df2011-07-24 03:09:51 -070087 if (space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070088 LOG(FATAL) << "Failed to create alloc space";
Carl Shapiro69759ea2011-07-21 18:13:35 -070089 }
90
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070091 if (boot_space == NULL) {
92 boot_space = space;
93 }
94 byte* base = std::min(boot_space->GetBase(), space->GetBase());
95 byte* limit = std::max(boot_space->GetLimit(), space->GetLimit());
96 DCHECK_LT(base, limit);
97 size_t num_bytes = limit - base;
Carl Shapiro69759ea2011-07-21 18:13:35 -070098
99 // Allocate the initial live bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -0700100 UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes));
101 if (live_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700102 LOG(FATAL) << "Failed to create live bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700103 }
104
105 // Allocate the initial mark bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -0700106 UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes));
107 if (mark_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700108 LOG(FATAL) << "Failed to create mark bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700109 }
110
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700111 alloc_space_ = space;
Carl Shapiro58551df2011-07-24 03:09:51 -0700112 spaces_.push_back(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700113 maximum_size_ = maximum_size;
114 live_bitmap_ = live_bitmap.release();
115 mark_bitmap_ = mark_bitmap.release();
116
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117 num_bytes_allocated_ = 0;
118 num_objects_allocated_ = 0;
119
Carl Shapiro69759ea2011-07-21 18:13:35 -0700120 // TODO: allocate the card table
121
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700122 // Make objects in boot_space live (after live_bitmap_ is set)
123 if (boot_image_file_name != NULL) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700124 boot_space_ = boot_space;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700125 RecordImageAllocations(boot_space);
126 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700127 for (size_t i = 0; i < image_spaces.size(); i++) {
128 RecordImageAllocations(image_spaces[i]);
129 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700130
Elliott Hughes85d15452011-09-16 17:33:01 -0700131 Heap::EnableObjectValidation();
132
Elliott Hughes92b3b562011-09-08 16:32:26 -0700133 // It's still to early to take a lock because there are no threads yet,
134 // but we can create the heap lock now. We don't create it earlier to
135 // make it clear that you can't use locks during heap initialization.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700136 lock_ = new Mutex("Heap lock");
Carl Shapiro69759ea2011-07-21 18:13:35 -0700137}
138
139void Heap::Destroy() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700140 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700141 STLDeleteElements(&spaces_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700142 if (mark_bitmap_ != NULL) {
143 delete mark_bitmap_;
144 mark_bitmap_ = NULL;
145 }
146 if (live_bitmap_ != NULL) {
147 delete live_bitmap_;
148 }
149 live_bitmap_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700150}
151
Carl Shapiro58551df2011-07-24 03:09:51 -0700152Object* Heap::AllocObject(Class* klass, size_t num_bytes) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700153 ScopedHeapLock lock;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700154 DCHECK(klass == NULL
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700155 || klass->GetDescriptor() == NULL
Brian Carlstrom4873d462011-08-21 15:23:39 -0700156 || (klass->IsClassClass() && num_bytes >= sizeof(Class))
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700157 || (klass->IsVariableSize() || klass->GetObjectSize() == num_bytes));
158 DCHECK(num_bytes >= sizeof(Object));
Elliott Hughes92b3b562011-09-08 16:32:26 -0700159 Object* obj = AllocateLocked(num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700160 if (obj != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161 obj->SetClass(klass);
Carl Shapiro58551df2011-07-24 03:09:51 -0700162 }
163 return obj;
164}
165
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700166bool Heap::IsHeapAddress(const Object* obj) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700167 // Note: we deliberately don't take the lock here, and mustn't test anything that would
168 // require taking the lock.
Elliott Hughesa2501992011-08-26 19:39:54 -0700169 if (!IsAligned(obj, kObjectAlignment)) {
170 return false;
171 }
172 // TODO
173 return true;
174}
175
Elliott Hughes3e465b12011-09-02 18:26:12 -0700176#if VERIFY_OBJECT_ENABLED
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700177void Heap::VerifyObject(const Object* obj) {
Elliott Hughes85d15452011-09-16 17:33:01 -0700178 if (!verify_objects_) {
179 return;
180 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700181 ScopedHeapLock lock;
182 Heap::VerifyObjectLocked(obj);
183}
184#endif
185
186void Heap::VerifyObjectLocked(const Object* obj) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700187 lock_->AssertHeld();
Elliott Hughes85d15452011-09-16 17:33:01 -0700188 if (obj != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189 if (!IsAligned(obj, kObjectAlignment)) {
190 LOG(FATAL) << "Object isn't aligned: " << obj;
191 } else if (!live_bitmap_->Test(obj)) {
192 // TODO: we don't hold a lock here as it is assumed the live bit map
193 // isn't changing if the mutator is running.
194 LOG(FATAL) << "Object is dead: " << obj;
195 }
196 // Ignore early dawn of the universe verifications
Brian Carlstromdbc05252011-09-09 01:59:59 -0700197 if (num_objects_allocated_ > 10) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 const byte* raw_addr = reinterpret_cast<const byte*>(obj) +
199 Object::ClassOffset().Int32Value();
200 const Class* c = *reinterpret_cast<Class* const *>(raw_addr);
201 if (c == NULL) {
202 LOG(FATAL) << "Null class" << " in object: " << obj;
203 } else if (!IsAligned(c, kObjectAlignment)) {
204 LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj;
205 } else if (!live_bitmap_->Test(c)) {
206 LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj;
207 }
208 // Check obj.getClass().getClass() == obj.getClass().getClass().getClass()
209 // NB we don't use the accessors here as they have internal sanity checks
210 // that we don't want to run
211 raw_addr = reinterpret_cast<const byte*>(c) +
212 Object::ClassOffset().Int32Value();
213 const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr);
214 raw_addr = reinterpret_cast<const byte*>(c_c) +
215 Object::ClassOffset().Int32Value();
216 const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr);
217 CHECK_EQ(c_c, c_c_c);
218 }
219 }
220}
221
Brian Carlstrom78128a62011-09-15 17:21:19 -0700222void Heap::VerificationCallback(Object* obj, void* arg) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 DCHECK(obj != NULL);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700224 Heap::VerifyObjectLocked(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700225}
226
227void Heap::VerifyHeap() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700228 ScopedHeapLock lock;
229 live_bitmap_->Walk(Heap::VerificationCallback, NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700230}
231
Elliott Hughes92b3b562011-09-08 16:32:26 -0700232void Heap::RecordAllocationLocked(Space* space, const Object* obj) {
233#ifndef NDEBUG
234 if (Runtime::Current()->IsStarted()) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700235 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700236 }
237#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700238 size_t size = space->AllocationSize(obj);
239 DCHECK_NE(size, 0u);
240 num_bytes_allocated_ += size;
241 num_objects_allocated_ += 1;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700242
243 if (Runtime::Current()->HasStatsEnabled()) {
244 RuntimeStats* global_stats = Runtime::Current()->GetStats();
245 RuntimeStats* thread_stats = Thread::Current()->GetStats();
246 ++global_stats->allocated_objects;
247 ++thread_stats->allocated_objects;
248 global_stats->allocated_bytes += size;
249 thread_stats->allocated_bytes += size;
250 }
251
Carl Shapiro58551df2011-07-24 03:09:51 -0700252 live_bitmap_->Set(obj);
253}
254
Elliott Hughes92b3b562011-09-08 16:32:26 -0700255void Heap::RecordFreeLocked(Space* space, const Object* obj) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700256 lock_->AssertHeld();
Carl Shapiro58551df2011-07-24 03:09:51 -0700257 size_t size = space->AllocationSize(obj);
258 DCHECK_NE(size, 0u);
259 if (size < num_bytes_allocated_) {
260 num_bytes_allocated_ -= size;
261 } else {
262 num_bytes_allocated_ = 0;
263 }
264 live_bitmap_->Clear(obj);
265 if (num_objects_allocated_ > 0) {
266 num_objects_allocated_ -= 1;
267 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700268
269 if (Runtime::Current()->HasStatsEnabled()) {
270 RuntimeStats* global_stats = Runtime::Current()->GetStats();
271 RuntimeStats* thread_stats = Thread::Current()->GetStats();
272 ++global_stats->freed_objects;
273 ++thread_stats->freed_objects;
274 global_stats->freed_bytes += size;
275 thread_stats->freed_bytes += size;
276 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700277}
278
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700279void Heap::RecordImageAllocations(Space* space) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700280 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700281 CHECK(space != NULL);
282 CHECK(live_bitmap_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700283 byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700284 while (current < space->GetLimit()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700285 DCHECK(IsAligned(current, kObjectAlignment));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700286 const Object* obj = reinterpret_cast<const Object*>(current);
287 live_bitmap_->Set(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700288 current += RoundUp(obj->SizeOf(), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700289 }
290}
291
Elliott Hughes92b3b562011-09-08 16:32:26 -0700292Object* Heap::AllocateLocked(size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700293 lock_->AssertHeld();
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700294 DCHECK(alloc_space_ != NULL);
295 Space* space = alloc_space_;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700296 Object* obj = AllocateLocked(space, size);
Carl Shapiro58551df2011-07-24 03:09:51 -0700297 if (obj != NULL) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700298 RecordAllocationLocked(space, obj);
Carl Shapiro58551df2011-07-24 03:09:51 -0700299 }
300 return obj;
301}
302
Elliott Hughes92b3b562011-09-08 16:32:26 -0700303Object* Heap::AllocateLocked(Space* space, size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700304 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700305
Carl Shapiro69759ea2011-07-21 18:13:35 -0700306 // Fail impossible allocations. TODO: collect soft references.
307 if (size > maximum_size_) {
308 return NULL;
309 }
310
Carl Shapiro58551df2011-07-24 03:09:51 -0700311 Object* ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700312 if (ptr != NULL) {
313 return ptr;
314 }
315
316 // The allocation failed. If the GC is running, block until it
317 // completes and retry.
318 if (is_gc_running_) {
319 // The GC is concurrently tracing the heap. Release the heap
320 // lock, wait for the GC to complete, and retrying allocating.
321 WaitForConcurrentGcToComplete();
Carl Shapiro58551df2011-07-24 03:09:51 -0700322 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700323 if (ptr != NULL) {
324 return ptr;
325 }
326 }
327
328 // Another failure. Our thread was starved or there may be too many
329 // live objects. Try a foreground GC. This will have no effect if
330 // the concurrent GC is already running.
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700331 if (Runtime::Current()->HasStatsEnabled()) {
332 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
333 ++Thread::Current()->GetStats()->gc_for_alloc_count;
334 }
Brian Carlstrom27ec9612011-09-19 20:20:38 -0700335 UNIMPLEMENTED(FATAL) << "No implicit GC, use larger -Xms -Xmx";
Carl Shapiro58551df2011-07-24 03:09:51 -0700336 CollectGarbageInternal();
337 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700338 if (ptr != NULL) {
339 return ptr;
340 }
341
342 // Even that didn't work; this is an exceptional state.
343 // Try harder, growing the heap if necessary.
Carl Shapiro58551df2011-07-24 03:09:51 -0700344 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700345 if (ptr != NULL) {
346 //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
Carl Shapiro58551df2011-07-24 03:09:51 -0700347 size_t new_footprint = space->MaxAllowedFootprint();
348 // TODO: may want to grow a little bit more so that the amount of
349 // free space is equal to the old free space + the
350 // utilization slop for the new allocation.
351 LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352 << "for " << size << "-byte allocation";
353 return ptr;
354 }
355
356 // Most allocations should have succeeded by now, so the heap is
357 // really full, really fragmented, or the requested size is really
358 // big. Do another GC, collecting SoftReferences this time. The VM
359 // spec requires that all SoftReferences have been collected and
360 // cleared before throwing an OOME.
361
Carl Shapiro58551df2011-07-24 03:09:51 -0700362 // TODO: wait for the finalizers from the previous GC to finish
Carl Shapiro69759ea2011-07-21 18:13:35 -0700363 LOG(INFO) << "Forcing collection of SoftReferences for "
364 << size << "-byte allocation";
Carl Shapiro58551df2011-07-24 03:09:51 -0700365 CollectGarbageInternal();
366 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700367 if (ptr != NULL) {
368 return ptr;
369 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700370
Carl Shapiro69759ea2011-07-21 18:13:35 -0700371 LOG(ERROR) << "Out of memory on a " << size << " byte allocation";
372
Carl Shapiro58551df2011-07-24 03:09:51 -0700373 // TODO: tell the HeapSource to dump its state
374 // TODO: dump stack traces for all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700375
Carl Shapiro69759ea2011-07-21 18:13:35 -0700376 return NULL;
377}
378
Elliott Hughesbf86d042011-08-31 17:53:14 -0700379int64_t Heap::GetMaxMemory() {
380 UNIMPLEMENTED(WARNING);
381 return 0;
382}
383
384int64_t Heap::GetTotalMemory() {
385 UNIMPLEMENTED(WARNING);
386 return 0;
387}
388
389int64_t Heap::GetFreeMemory() {
390 UNIMPLEMENTED(WARNING);
391 return 0;
392}
393
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700394class InstanceCounter {
395 public:
396 InstanceCounter(Class* c, bool count_assignable)
397 : class_(c), count_assignable_(count_assignable), count_(0) {
398 }
399
400 size_t GetCount() {
401 return count_;
402 }
403
404 static void Callback(Object* o, void* arg) {
405 reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o);
406 }
407
408 private:
409 void VisitInstance(Object* o) {
410 Class* instance_class = o->GetClass();
411 if (count_assignable_) {
412 if (instance_class == class_) {
413 ++count_;
414 }
415 } else {
416 if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) {
417 ++count_;
418 }
419 }
420 }
421
422 Class* class_;
423 bool count_assignable_;
424 size_t count_;
425};
426
427int64_t Heap::CountInstances(Class* c, bool count_assignable) {
428 ScopedHeapLock lock;
429 InstanceCounter counter(c, count_assignable);
430 live_bitmap_->Walk(InstanceCounter::Callback, &counter);
431 return counter.GetCount();
432}
433
Carl Shapiro69759ea2011-07-21 18:13:35 -0700434void Heap::CollectGarbage() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700435 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700436 CollectGarbageInternal();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700437}
438
439void Heap::CollectGarbageInternal() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700440 lock_->AssertHeld();
Carl Shapiro58551df2011-07-24 03:09:51 -0700441
Elliott Hughes8d768a92011-09-14 16:35:25 -0700442 ThreadList* thread_list = Runtime::Current()->GetThreadList();
443 thread_list->SuspendAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700444 {
445 MarkSweep mark_sweep;
446
447 mark_sweep.Init();
448
449 mark_sweep.MarkRoots();
450
451 // Push marked roots onto the mark stack
452
453 // TODO: if concurrent
454 // unlock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700455 // thread_list->ResumeAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700456
457 mark_sweep.RecursiveMark();
458
459 // TODO: if concurrent
460 // lock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700461 // thread_list->SuspendAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700462 // re-mark root set
463 // scan dirty objects
464
465 mark_sweep.ProcessReferences(false);
466
467 // TODO: swap bitmaps
468
469 mark_sweep.Sweep();
470 }
471
472 GrowForUtilization();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700473 thread_list->ResumeAll();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700474}
475
476void Heap::WaitForConcurrentGcToComplete() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700477 lock_->AssertHeld();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700478}
479
480// Given the current contents of the active heap, increase the allowed
481// heap footprint to match the target utilization ratio. This should
482// only be called immediately after a full garbage collection.
483void Heap::GrowForUtilization() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700484 lock_->AssertHeld();
Elliott Hughes53b61312011-08-12 18:28:20 -0700485 UNIMPLEMENTED(ERROR);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700486}
487
Elliott Hughes92b3b562011-09-08 16:32:26 -0700488void Heap::Lock() {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700489 // TODO: grab the lock, but put ourselves into Thread::kVmWait if it looks like
Elliott Hughes92b3b562011-09-08 16:32:26 -0700490 // we're going to have to wait on the mutex.
491 lock_->Lock();
492}
493
494void Heap::Unlock() {
495 lock_->Unlock();
496}
497
Carl Shapiro69759ea2011-07-21 18:13:35 -0700498} // namespace art