blob: 015f6381f99b4b5d0df4879bc7915e79bd92230a [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 Carlstrom0a5b14d2011-09-27 13:29:15 -070061 const Runtime* runtime = Runtime::Current();
62 if (runtime->IsVerboseStartup()) {
63 LOG(INFO) << "Heap::Init entering";
64 }
65
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070066 Space* boot_space;
67 byte* requested_base;
68 if (boot_image_file_name == NULL) {
69 boot_space = NULL;
70 requested_base = NULL;
71 } else {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070072 boot_space = Space::CreateFromImage(boot_image_file_name);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070073 if (boot_space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070074 LOG(FATAL) << "Failed to create space from " << boot_image_file_name;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070075 }
76 spaces_.push_back(boot_space);
77 requested_base = boot_space->GetBase() + RoundUp(boot_space->Size(), kPageSize);
78 }
79
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070080 std::vector<Space*> image_spaces;
81 for (size_t i = 0; i < image_file_names.size(); i++) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070082 Space* space = Space::CreateFromImage(image_file_names[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070083 if (space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070084 LOG(FATAL) << "Failed to create space from " << image_file_names[i];
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070085 }
86 image_spaces.push_back(space);
87 spaces_.push_back(space);
88 requested_base = space->GetBase() + RoundUp(space->Size(), kPageSize);
89 }
90
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070091 Space* space = Space::Create(initial_size, maximum_size, requested_base);
Carl Shapiro58551df2011-07-24 03:09:51 -070092 if (space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070093 LOG(FATAL) << "Failed to create alloc space";
Carl Shapiro69759ea2011-07-21 18:13:35 -070094 }
95
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070096 if (boot_space == NULL) {
97 boot_space = space;
98 }
99 byte* base = std::min(boot_space->GetBase(), space->GetBase());
100 byte* limit = std::max(boot_space->GetLimit(), space->GetLimit());
101 DCHECK_LT(base, limit);
102 size_t num_bytes = limit - base;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700103
104 // Allocate the initial live bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -0700105 UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes));
106 if (live_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700107 LOG(FATAL) << "Failed to create live bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700108 }
109
110 // Allocate the initial mark bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -0700111 UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes));
112 if (mark_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700113 LOG(FATAL) << "Failed to create mark bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700114 }
115
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700116 alloc_space_ = space;
Carl Shapiro58551df2011-07-24 03:09:51 -0700117 spaces_.push_back(space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700118 maximum_size_ = maximum_size;
119 live_bitmap_ = live_bitmap.release();
120 mark_bitmap_ = mark_bitmap.release();
121
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700122 num_bytes_allocated_ = 0;
123 num_objects_allocated_ = 0;
124
Carl Shapiro69759ea2011-07-21 18:13:35 -0700125 // TODO: allocate the card table
126
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700127 // Make objects in boot_space live (after live_bitmap_ is set)
128 if (boot_image_file_name != NULL) {
Brian Carlstroma663ea52011-08-19 23:33:41 -0700129 boot_space_ = boot_space;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700130 RecordImageAllocations(boot_space);
131 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700132 for (size_t i = 0; i < image_spaces.size(); i++) {
133 RecordImageAllocations(image_spaces[i]);
134 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700135
Elliott Hughes85d15452011-09-16 17:33:01 -0700136 Heap::EnableObjectValidation();
137
Elliott Hughes92b3b562011-09-08 16:32:26 -0700138 // It's still to early to take a lock because there are no threads yet,
139 // but we can create the heap lock now. We don't create it earlier to
140 // make it clear that you can't use locks during heap initialization.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700141 lock_ = new Mutex("Heap lock");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700142
143 if (runtime->IsVerboseStartup()) {
144 LOG(INFO) << "Heap::Init exiting";
145 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700146}
147
148void Heap::Destroy() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700149 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700150 STLDeleteElements(&spaces_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700151 if (mark_bitmap_ != NULL) {
152 delete mark_bitmap_;
153 mark_bitmap_ = NULL;
154 }
155 if (live_bitmap_ != NULL) {
156 delete live_bitmap_;
157 }
158 live_bitmap_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700159}
160
Carl Shapiro58551df2011-07-24 03:09:51 -0700161Object* Heap::AllocObject(Class* klass, size_t num_bytes) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700162 ScopedHeapLock lock;
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700163 DCHECK(klass == NULL
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700164 || klass->GetDescriptor() == NULL
Brian Carlstrom4873d462011-08-21 15:23:39 -0700165 || (klass->IsClassClass() && num_bytes >= sizeof(Class))
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700166 || (klass->IsVariableSize() || klass->GetObjectSize() == num_bytes));
167 DCHECK(num_bytes >= sizeof(Object));
Elliott Hughes92b3b562011-09-08 16:32:26 -0700168 Object* obj = AllocateLocked(num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700169 if (obj != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700170 obj->SetClass(klass);
Carl Shapiro58551df2011-07-24 03:09:51 -0700171 }
172 return obj;
173}
174
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700175bool Heap::IsHeapAddress(const Object* obj) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700176 // Note: we deliberately don't take the lock here, and mustn't test anything that would
177 // require taking the lock.
Elliott Hughesa2501992011-08-26 19:39:54 -0700178 if (!IsAligned(obj, kObjectAlignment)) {
179 return false;
180 }
181 // TODO
182 return true;
183}
184
Elliott Hughes3e465b12011-09-02 18:26:12 -0700185#if VERIFY_OBJECT_ENABLED
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700186void Heap::VerifyObject(const Object* obj) {
Elliott Hughes85d15452011-09-16 17:33:01 -0700187 if (!verify_objects_) {
188 return;
189 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700190 ScopedHeapLock lock;
191 Heap::VerifyObjectLocked(obj);
192}
193#endif
194
195void Heap::VerifyObjectLocked(const Object* obj) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700196 lock_->AssertHeld();
Elliott Hughes85d15452011-09-16 17:33:01 -0700197 if (obj != NULL) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 if (!IsAligned(obj, kObjectAlignment)) {
199 LOG(FATAL) << "Object isn't aligned: " << obj;
200 } else if (!live_bitmap_->Test(obj)) {
201 // TODO: we don't hold a lock here as it is assumed the live bit map
202 // isn't changing if the mutator is running.
203 LOG(FATAL) << "Object is dead: " << obj;
204 }
205 // Ignore early dawn of the universe verifications
Brian Carlstromdbc05252011-09-09 01:59:59 -0700206 if (num_objects_allocated_ > 10) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700207 const byte* raw_addr = reinterpret_cast<const byte*>(obj) +
208 Object::ClassOffset().Int32Value();
209 const Class* c = *reinterpret_cast<Class* const *>(raw_addr);
210 if (c == NULL) {
211 LOG(FATAL) << "Null class" << " in object: " << obj;
212 } else if (!IsAligned(c, kObjectAlignment)) {
213 LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj;
214 } else if (!live_bitmap_->Test(c)) {
215 LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj;
216 }
217 // Check obj.getClass().getClass() == obj.getClass().getClass().getClass()
218 // NB we don't use the accessors here as they have internal sanity checks
219 // that we don't want to run
220 raw_addr = reinterpret_cast<const byte*>(c) +
221 Object::ClassOffset().Int32Value();
222 const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr);
223 raw_addr = reinterpret_cast<const byte*>(c_c) +
224 Object::ClassOffset().Int32Value();
225 const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr);
226 CHECK_EQ(c_c, c_c_c);
227 }
228 }
229}
230
Brian Carlstrom78128a62011-09-15 17:21:19 -0700231void Heap::VerificationCallback(Object* obj, void* arg) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700232 DCHECK(obj != NULL);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700233 Heap::VerifyObjectLocked(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700234}
235
236void Heap::VerifyHeap() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700237 ScopedHeapLock lock;
238 live_bitmap_->Walk(Heap::VerificationCallback, NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239}
240
Elliott Hughes92b3b562011-09-08 16:32:26 -0700241void Heap::RecordAllocationLocked(Space* space, const Object* obj) {
242#ifndef NDEBUG
243 if (Runtime::Current()->IsStarted()) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700244 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700245 }
246#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700247 size_t size = space->AllocationSize(obj);
248 DCHECK_NE(size, 0u);
249 num_bytes_allocated_ += size;
250 num_objects_allocated_ += 1;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700251
252 if (Runtime::Current()->HasStatsEnabled()) {
253 RuntimeStats* global_stats = Runtime::Current()->GetStats();
254 RuntimeStats* thread_stats = Thread::Current()->GetStats();
255 ++global_stats->allocated_objects;
256 ++thread_stats->allocated_objects;
257 global_stats->allocated_bytes += size;
258 thread_stats->allocated_bytes += size;
259 }
260
Carl Shapiro58551df2011-07-24 03:09:51 -0700261 live_bitmap_->Set(obj);
262}
263
Elliott Hughes92b3b562011-09-08 16:32:26 -0700264void Heap::RecordFreeLocked(Space* space, const Object* obj) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700265 lock_->AssertHeld();
Carl Shapiro58551df2011-07-24 03:09:51 -0700266 size_t size = space->AllocationSize(obj);
267 DCHECK_NE(size, 0u);
268 if (size < num_bytes_allocated_) {
269 num_bytes_allocated_ -= size;
270 } else {
271 num_bytes_allocated_ = 0;
272 }
273 live_bitmap_->Clear(obj);
274 if (num_objects_allocated_ > 0) {
275 num_objects_allocated_ -= 1;
276 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700277
278 if (Runtime::Current()->HasStatsEnabled()) {
279 RuntimeStats* global_stats = Runtime::Current()->GetStats();
280 RuntimeStats* thread_stats = Thread::Current()->GetStats();
281 ++global_stats->freed_objects;
282 ++thread_stats->freed_objects;
283 global_stats->freed_bytes += size;
284 thread_stats->freed_bytes += size;
285 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700286}
287
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700288void Heap::RecordImageAllocations(Space* space) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700289 const Runtime* runtime = Runtime::Current();
290 if (runtime->IsVerboseStartup()) {
291 LOG(INFO) << "Heap::RecordImageAllocations entering";
292 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700293 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700294 CHECK(space != NULL);
295 CHECK(live_bitmap_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700296 byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700297 while (current < space->GetLimit()) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700298 DCHECK(IsAligned(current, kObjectAlignment));
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700299 const Object* obj = reinterpret_cast<const Object*>(current);
300 live_bitmap_->Set(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700301 current += RoundUp(obj->SizeOf(), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700302 }
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700303 if (runtime->IsVerboseStartup()) {
304 LOG(INFO) << "Heap::RecordImageAllocations exiting";
305 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700306}
307
Elliott Hughes92b3b562011-09-08 16:32:26 -0700308Object* Heap::AllocateLocked(size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700309 lock_->AssertHeld();
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700310 DCHECK(alloc_space_ != NULL);
311 Space* space = alloc_space_;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700312 Object* obj = AllocateLocked(space, size);
Carl Shapiro58551df2011-07-24 03:09:51 -0700313 if (obj != NULL) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700314 RecordAllocationLocked(space, obj);
Carl Shapiro58551df2011-07-24 03:09:51 -0700315 }
316 return obj;
317}
318
Elliott Hughes92b3b562011-09-08 16:32:26 -0700319Object* Heap::AllocateLocked(Space* space, size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700320 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700321
Carl Shapiro69759ea2011-07-21 18:13:35 -0700322 // Fail impossible allocations. TODO: collect soft references.
323 if (size > maximum_size_) {
324 return NULL;
325 }
326
Carl Shapiro58551df2011-07-24 03:09:51 -0700327 Object* ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700328 if (ptr != NULL) {
329 return ptr;
330 }
331
332 // The allocation failed. If the GC is running, block until it
333 // completes and retry.
334 if (is_gc_running_) {
335 // The GC is concurrently tracing the heap. Release the heap
336 // lock, wait for the GC to complete, and retrying allocating.
337 WaitForConcurrentGcToComplete();
Carl Shapiro58551df2011-07-24 03:09:51 -0700338 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700339 if (ptr != NULL) {
340 return ptr;
341 }
342 }
343
344 // Another failure. Our thread was starved or there may be too many
345 // live objects. Try a foreground GC. This will have no effect if
346 // the concurrent GC is already running.
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700347 if (Runtime::Current()->HasStatsEnabled()) {
348 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
349 ++Thread::Current()->GetStats()->gc_for_alloc_count;
350 }
Ian Rogersd6b1f612011-09-27 13:38:14 -0700351 LOG(INFO) << "GC_FOR_ALLOC: TODO: test";
Carl Shapiro58551df2011-07-24 03:09:51 -0700352 CollectGarbageInternal();
353 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700354 if (ptr != NULL) {
355 return ptr;
356 }
Ian Rogersd6b1f612011-09-27 13:38:14 -0700357 UNIMPLEMENTED(FATAL) << "No AllocWithGrowth, use larger -Xms -Xmx";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700358
359 // Even that didn't work; this is an exceptional state.
360 // Try harder, growing the heap if necessary.
Carl Shapiro58551df2011-07-24 03:09:51 -0700361 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700362 if (ptr != NULL) {
363 //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
Carl Shapiro58551df2011-07-24 03:09:51 -0700364 size_t new_footprint = space->MaxAllowedFootprint();
365 // TODO: may want to grow a little bit more so that the amount of
366 // free space is equal to the old free space + the
367 // utilization slop for the new allocation.
368 LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB
Carl Shapiro69759ea2011-07-21 18:13:35 -0700369 << "for " << size << "-byte allocation";
370 return ptr;
371 }
372
373 // Most allocations should have succeeded by now, so the heap is
374 // really full, really fragmented, or the requested size is really
375 // big. Do another GC, collecting SoftReferences this time. The VM
376 // spec requires that all SoftReferences have been collected and
377 // cleared before throwing an OOME.
378
Carl Shapiro58551df2011-07-24 03:09:51 -0700379 // TODO: wait for the finalizers from the previous GC to finish
Carl Shapiro69759ea2011-07-21 18:13:35 -0700380 LOG(INFO) << "Forcing collection of SoftReferences for "
381 << size << "-byte allocation";
Carl Shapiro58551df2011-07-24 03:09:51 -0700382 CollectGarbageInternal();
383 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700384 if (ptr != NULL) {
385 return ptr;
386 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700387
Carl Shapiro69759ea2011-07-21 18:13:35 -0700388 LOG(ERROR) << "Out of memory on a " << size << " byte allocation";
389
Carl Shapiro58551df2011-07-24 03:09:51 -0700390 // TODO: tell the HeapSource to dump its state
391 // TODO: dump stack traces for all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700392
Carl Shapiro69759ea2011-07-21 18:13:35 -0700393 return NULL;
394}
395
Elliott Hughesbf86d042011-08-31 17:53:14 -0700396int64_t Heap::GetMaxMemory() {
397 UNIMPLEMENTED(WARNING);
398 return 0;
399}
400
401int64_t Heap::GetTotalMemory() {
402 UNIMPLEMENTED(WARNING);
403 return 0;
404}
405
406int64_t Heap::GetFreeMemory() {
407 UNIMPLEMENTED(WARNING);
408 return 0;
409}
410
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700411class InstanceCounter {
412 public:
413 InstanceCounter(Class* c, bool count_assignable)
414 : class_(c), count_assignable_(count_assignable), count_(0) {
415 }
416
417 size_t GetCount() {
418 return count_;
419 }
420
421 static void Callback(Object* o, void* arg) {
422 reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o);
423 }
424
425 private:
426 void VisitInstance(Object* o) {
427 Class* instance_class = o->GetClass();
428 if (count_assignable_) {
429 if (instance_class == class_) {
430 ++count_;
431 }
432 } else {
433 if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) {
434 ++count_;
435 }
436 }
437 }
438
439 Class* class_;
440 bool count_assignable_;
441 size_t count_;
442};
443
444int64_t Heap::CountInstances(Class* c, bool count_assignable) {
445 ScopedHeapLock lock;
446 InstanceCounter counter(c, count_assignable);
447 live_bitmap_->Walk(InstanceCounter::Callback, &counter);
448 return counter.GetCount();
449}
450
Carl Shapiro69759ea2011-07-21 18:13:35 -0700451void Heap::CollectGarbage() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700452 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700453 CollectGarbageInternal();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700454}
455
456void Heap::CollectGarbageInternal() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700457 lock_->AssertHeld();
Carl Shapiro58551df2011-07-24 03:09:51 -0700458
Elliott Hughes8d768a92011-09-14 16:35:25 -0700459 ThreadList* thread_list = Runtime::Current()->GetThreadList();
460 thread_list->SuspendAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700461 {
462 MarkSweep mark_sweep;
463
464 mark_sweep.Init();
465
466 mark_sweep.MarkRoots();
467
468 // Push marked roots onto the mark stack
469
470 // TODO: if concurrent
471 // unlock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700472 // thread_list->ResumeAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700473
474 mark_sweep.RecursiveMark();
475
476 // TODO: if concurrent
477 // lock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700478 // thread_list->SuspendAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700479 // re-mark root set
480 // scan dirty objects
481
482 mark_sweep.ProcessReferences(false);
483
484 // TODO: swap bitmaps
485
486 mark_sweep.Sweep();
487 }
488
489 GrowForUtilization();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700490 thread_list->ResumeAll();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700491}
492
493void Heap::WaitForConcurrentGcToComplete() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700494 lock_->AssertHeld();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700495}
496
497// Given the current contents of the active heap, increase the allowed
498// heap footprint to match the target utilization ratio. This should
499// only be called immediately after a full garbage collection.
500void Heap::GrowForUtilization() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700501 lock_->AssertHeld();
Elliott Hughes53b61312011-08-12 18:28:20 -0700502 UNIMPLEMENTED(ERROR);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700503}
504
Elliott Hughes92b3b562011-09-08 16:32:26 -0700505void Heap::Lock() {
Elliott Hughes93e74e82011-09-13 11:07:03 -0700506 // TODO: grab the lock, but put ourselves into Thread::kVmWait if it looks like
Elliott Hughes92b3b562011-09-08 16:32:26 -0700507 // we're going to have to wait on the mutex.
508 lock_->Lock();
509}
510
511void Heap::Unlock() {
512 lock_->Unlock();
513}
514
Carl Shapiro69759ea2011-07-21 18:13:35 -0700515} // namespace art