blob: 9e356d12d975356947f8a7f646da7540ad80bb05 [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
Brian Carlstrom58ae9412011-10-04 00:56:06 -07005#include <limits>
Carl Shapiro58551df2011-07-24 03:09:51 -07006#include <vector>
7
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include "UniquePtr.h"
Brian Carlstrom9cff8e12011-08-18 16:47:29 -07009#include "image.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070010#include "mark_sweep.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "object.h"
12#include "space.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070013#include "stl_util.h"
Elliott Hughes307f75d2011-10-12 18:04:40 -070014#include "timing_logger.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070015#include "thread_list.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070016
17namespace art {
18
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070019bool Heap::is_verbose_heap_ = false;
20
21bool Heap::is_verbose_gc_ = false;
22
Carl Shapiro58551df2011-07-24 03:09:51 -070023std::vector<Space*> Heap::spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070024
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070025Space* Heap::alloc_space_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -070026
27size_t Heap::maximum_size_ = 0;
28
Carl Shapiro58551df2011-07-24 03:09:51 -070029size_t Heap::num_bytes_allocated_ = 0;
30
31size_t Heap::num_objects_allocated_ = 0;
32
Carl Shapiro69759ea2011-07-21 18:13:35 -070033bool Heap::is_gc_running_ = false;
34
35HeapBitmap* Heap::mark_bitmap_ = NULL;
36
37HeapBitmap* Heap::live_bitmap_ = NULL;
38
Elliott Hughesadb460d2011-10-05 17:02:34 -070039Class* Heap::java_lang_ref_FinalizerReference_ = NULL;
40Class* Heap::java_lang_ref_ReferenceQueue_ = NULL;
41
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070042MemberOffset Heap::reference_referent_offset_ = MemberOffset(0);
43MemberOffset Heap::reference_queue_offset_ = MemberOffset(0);
44MemberOffset Heap::reference_queueNext_offset_ = MemberOffset(0);
45MemberOffset Heap::reference_pendingNext_offset_ = MemberOffset(0);
46MemberOffset Heap::finalizer_reference_zombie_offset_ = MemberOffset(0);
Brian Carlstrom1f870082011-08-23 16:02:11 -070047
Brian Carlstrom395520e2011-09-25 19:35:00 -070048float Heap::target_utilization_ = 0.5;
49
Elliott Hughes92b3b562011-09-08 16:32:26 -070050Mutex* Heap::lock_ = NULL;
51
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070052bool Heap::verify_objects_ = false;
53
Elliott Hughes92b3b562011-09-08 16:32:26 -070054class ScopedHeapLock {
55 public:
56 ScopedHeapLock() {
57 Heap::Lock();
58 }
59
60 ~ScopedHeapLock() {
61 Heap::Unlock();
62 }
63};
64
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070065void Heap::Init(bool is_verbose_heap, bool is_verbose_gc,
66 size_t initial_size, size_t maximum_size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070067 const std::vector<std::string>& image_file_names) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070068 is_verbose_heap_ = is_verbose_heap;
69 is_verbose_gc_ = is_verbose_gc;
70
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070071 const Runtime* runtime = Runtime::Current();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070072 if (is_verbose_heap_ || runtime->IsVerboseStartup()) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070073 LOG(INFO) << "Heap::Init entering";
74 }
75
Brian Carlstrom58ae9412011-10-04 00:56:06 -070076 // bounds of all spaces for allocating live and mark bitmaps
77 // there will be at least one space (the alloc space),
78 // so set to base to max and limit to min to start
79 byte* base = reinterpret_cast<byte*>(std::numeric_limits<uintptr_t>::max());
80 byte* limit = reinterpret_cast<byte*>(std::numeric_limits<uintptr_t>::min());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070081
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 byte* requested_base = NULL;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070083 std::vector<Space*> image_spaces;
84 for (size_t i = 0; i < image_file_names.size(); i++) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070085 Space* space = Space::CreateFromImage(image_file_names[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070086 if (space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070087 LOG(FATAL) << "Failed to create space from " << image_file_names[i];
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070088 }
89 image_spaces.push_back(space);
90 spaces_.push_back(space);
Brian Carlstrome24fa612011-09-29 00:53:55 -070091 byte* oat_limit_addr = space->GetImageHeader().GetOatLimitAddr();
Brian Carlstrom58ae9412011-10-04 00:56:06 -070092 if (oat_limit_addr > requested_base) {
93 requested_base = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr),
94 kPageSize));
95 }
96 base = std::min(base, space->GetBase());
97 limit = std::max(limit, space->GetLimit());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070098 }
99
Elliott Hughes307f75d2011-10-12 18:04:40 -0700100 alloc_space_ = Space::Create("alloc space", initial_size, maximum_size, requested_base);
101 if (alloc_space_ == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700102 LOG(FATAL) << "Failed to create alloc space";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700103 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700104 base = std::min(base, alloc_space_->GetBase());
105 limit = std::max(limit, alloc_space_->GetLimit());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700106 DCHECK_LT(base, limit);
107 size_t num_bytes = limit - base;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700108
109 // Allocate the initial live bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -0700110 UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create(base, num_bytes));
111 if (live_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700112 LOG(FATAL) << "Failed to create live bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700113 }
114
115 // Allocate the initial mark bitmap.
Elliott Hughes90a33692011-08-30 13:27:07 -0700116 UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create(base, num_bytes));
117 if (mark_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700118 LOG(FATAL) << "Failed to create mark bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700119 }
120
Elliott Hughes307f75d2011-10-12 18:04:40 -0700121 spaces_.push_back(alloc_space_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700122 maximum_size_ = maximum_size;
123 live_bitmap_ = live_bitmap.release();
124 mark_bitmap_ = mark_bitmap.release();
125
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700126 num_bytes_allocated_ = 0;
127 num_objects_allocated_ = 0;
128
Carl Shapiro69759ea2011-07-21 18:13:35 -0700129 // TODO: allocate the card table
130
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700131 // Make image objects live (after live_bitmap_ is set)
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
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700143 if (is_verbose_heap_ || runtime->IsVerboseStartup()) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700144 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
Elliott Hughes418dfe72011-10-06 18:56:27 -0700161Object* Heap::AllocObject(Class* klass, size_t byte_count) {
162 {
163 ScopedHeapLock lock;
164 DCHECK(klass == NULL || klass->GetDescriptor() == NULL ||
165 (klass->IsClassClass() && byte_count >= sizeof(Class)) ||
166 (klass->IsVariableSize() || klass->GetObjectSize() == byte_count));
167 DCHECK_GE(byte_count, sizeof(Object));
168 Object* obj = AllocateLocked(byte_count);
169 if (obj != NULL) {
170 obj->SetClass(klass);
171 return obj;
172 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700173 }
Elliott Hughes418dfe72011-10-06 18:56:27 -0700174
175 Thread::Current()->ThrowOutOfMemoryError(klass, byte_count);
176 return NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -0700177}
178
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700179bool Heap::IsHeapAddress(const Object* obj) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700180 // Note: we deliberately don't take the lock here, and mustn't test anything that would
181 // require taking the lock.
Elliott Hughes06b37d92011-10-16 11:51:29 -0700182 if (!IsAligned<kObjectAlignment>(obj)) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700183 return false;
184 }
185 // TODO
186 return true;
187}
188
Elliott Hughes3e465b12011-09-02 18:26:12 -0700189#if VERIFY_OBJECT_ENABLED
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700190void Heap::VerifyObject(const Object* obj) {
Elliott Hughes85d15452011-09-16 17:33:01 -0700191 if (!verify_objects_) {
192 return;
193 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700194 ScopedHeapLock lock;
195 Heap::VerifyObjectLocked(obj);
196}
197#endif
198
199void Heap::VerifyObjectLocked(const Object* obj) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700200 lock_->AssertHeld();
Elliott Hughes85d15452011-09-16 17:33:01 -0700201 if (obj != NULL) {
Elliott Hughes06b37d92011-10-16 11:51:29 -0700202 if (!IsAligned<kObjectAlignment>(obj)) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700203 LOG(FATAL) << "Object isn't aligned: " << obj;
204 } else if (!live_bitmap_->Test(obj)) {
205 // TODO: we don't hold a lock here as it is assumed the live bit map
206 // isn't changing if the mutator is running.
207 LOG(FATAL) << "Object is dead: " << obj;
208 }
209 // Ignore early dawn of the universe verifications
Brian Carlstromdbc05252011-09-09 01:59:59 -0700210 if (num_objects_allocated_ > 10) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700211 const byte* raw_addr = reinterpret_cast<const byte*>(obj) +
212 Object::ClassOffset().Int32Value();
213 const Class* c = *reinterpret_cast<Class* const *>(raw_addr);
214 if (c == NULL) {
215 LOG(FATAL) << "Null class" << " in object: " << obj;
Elliott Hughes06b37d92011-10-16 11:51:29 -0700216 } else if (!IsAligned<kObjectAlignment>(c)) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700217 LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj;
218 } else if (!live_bitmap_->Test(c)) {
219 LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj;
220 }
221 // Check obj.getClass().getClass() == obj.getClass().getClass().getClass()
Ian Rogersad25ac52011-10-04 19:13:33 -0700222 // Note: we don't use the accessors here as they have internal sanity checks
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 // that we don't want to run
224 raw_addr = reinterpret_cast<const byte*>(c) +
225 Object::ClassOffset().Int32Value();
226 const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr);
227 raw_addr = reinterpret_cast<const byte*>(c_c) +
228 Object::ClassOffset().Int32Value();
229 const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr);
230 CHECK_EQ(c_c, c_c_c);
231 }
232 }
233}
234
Brian Carlstrom78128a62011-09-15 17:21:19 -0700235void Heap::VerificationCallback(Object* obj, void* arg) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700236 DCHECK(obj != NULL);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700237 Heap::VerifyObjectLocked(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700238}
239
240void Heap::VerifyHeap() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700241 ScopedHeapLock lock;
242 live_bitmap_->Walk(Heap::VerificationCallback, NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700243}
244
Elliott Hughes92b3b562011-09-08 16:32:26 -0700245void Heap::RecordAllocationLocked(Space* space, const Object* obj) {
246#ifndef NDEBUG
247 if (Runtime::Current()->IsStarted()) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700248 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700249 }
250#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700251 size_t size = space->AllocationSize(obj);
252 DCHECK_NE(size, 0u);
253 num_bytes_allocated_ += size;
254 num_objects_allocated_ += 1;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700255
256 if (Runtime::Current()->HasStatsEnabled()) {
257 RuntimeStats* global_stats = Runtime::Current()->GetStats();
258 RuntimeStats* thread_stats = Thread::Current()->GetStats();
259 ++global_stats->allocated_objects;
260 ++thread_stats->allocated_objects;
261 global_stats->allocated_bytes += size;
262 thread_stats->allocated_bytes += size;
263 }
264
Carl Shapiro58551df2011-07-24 03:09:51 -0700265 live_bitmap_->Set(obj);
266}
267
Elliott Hughes307f75d2011-10-12 18:04:40 -0700268void Heap::RecordFreeLocked(size_t freed_objects, size_t freed_bytes) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700269 lock_->AssertHeld();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700270
271 if (freed_objects < num_objects_allocated_) {
272 num_objects_allocated_ -= freed_objects;
273 } else {
274 num_objects_allocated_ = 0;
275 }
276 if (freed_bytes < num_bytes_allocated_) {
277 num_bytes_allocated_ -= freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700278 } else {
279 num_bytes_allocated_ = 0;
280 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700281
282 if (Runtime::Current()->HasStatsEnabled()) {
283 RuntimeStats* global_stats = Runtime::Current()->GetStats();
284 RuntimeStats* thread_stats = Thread::Current()->GetStats();
285 ++global_stats->freed_objects;
286 ++thread_stats->freed_objects;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700287 global_stats->freed_bytes += freed_bytes;
288 thread_stats->freed_bytes += freed_bytes;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700289 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700290}
291
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700292void Heap::RecordImageAllocations(Space* space) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700293 const Runtime* runtime = Runtime::Current();
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700294 if (is_verbose_heap_ || runtime->IsVerboseStartup()) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700295 LOG(INFO) << "Heap::RecordImageAllocations entering";
296 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700297 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700298 CHECK(space != NULL);
299 CHECK(live_bitmap_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700300 byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700301 while (current < space->GetLimit()) {
Elliott Hughes06b37d92011-10-16 11:51:29 -0700302 DCHECK_ALIGNED(current, kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700303 const Object* obj = reinterpret_cast<const Object*>(current);
304 live_bitmap_->Set(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700305 current += RoundUp(obj->SizeOf(), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700306 }
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700307 if (is_verbose_heap_ || runtime->IsVerboseStartup()) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700308 LOG(INFO) << "Heap::RecordImageAllocations exiting";
309 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700310}
311
Elliott Hughes92b3b562011-09-08 16:32:26 -0700312Object* Heap::AllocateLocked(size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700313 lock_->AssertHeld();
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700314 DCHECK(alloc_space_ != NULL);
315 Space* space = alloc_space_;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700316 Object* obj = AllocateLocked(space, size);
Carl Shapiro58551df2011-07-24 03:09:51 -0700317 if (obj != NULL) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700318 RecordAllocationLocked(space, obj);
Carl Shapiro58551df2011-07-24 03:09:51 -0700319 }
320 return obj;
321}
322
Elliott Hughes92b3b562011-09-08 16:32:26 -0700323Object* Heap::AllocateLocked(Space* space, size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700324 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700325
Brian Carlstromb82b6872011-10-26 17:18:07 -0700326 // Since allocation can cause a GC which will need to SuspendAll,
327 // make sure all allocators are in the kRunnable state.
328 DCHECK_EQ(Thread::Current()->GetState(), Thread::kRunnable);
329
Carl Shapiro69759ea2011-07-21 18:13:35 -0700330 // Fail impossible allocations. TODO: collect soft references.
331 if (size > maximum_size_) {
332 return NULL;
333 }
334
Carl Shapiro58551df2011-07-24 03:09:51 -0700335 Object* ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700336 if (ptr != NULL) {
337 return ptr;
338 }
339
340 // The allocation failed. If the GC is running, block until it
341 // completes and retry.
342 if (is_gc_running_) {
343 // The GC is concurrently tracing the heap. Release the heap
344 // lock, wait for the GC to complete, and retrying allocating.
345 WaitForConcurrentGcToComplete();
Carl Shapiro58551df2011-07-24 03:09:51 -0700346 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700347 if (ptr != NULL) {
348 return ptr;
349 }
350 }
351
352 // Another failure. Our thread was starved or there may be too many
353 // live objects. Try a foreground GC. This will have no effect if
354 // the concurrent GC is already running.
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700355 if (Runtime::Current()->HasStatsEnabled()) {
356 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
357 ++Thread::Current()->GetStats()->gc_for_alloc_count;
358 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700359 CollectGarbageInternal();
360 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700361 if (ptr != NULL) {
362 return ptr;
363 }
364
365 // Even that didn't work; this is an exceptional state.
366 // Try harder, growing the heap if necessary.
Carl Shapiro58551df2011-07-24 03:09:51 -0700367 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700368 if (ptr != NULL) {
369 //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700370 size_t new_footprint = space->GetMaxAllowedFootprint();
Elliott Hughes418dfe72011-10-06 18:56:27 -0700371 // OLD-TODO: may want to grow a little bit more so that the amount of
Carl Shapiro58551df2011-07-24 03:09:51 -0700372 // free space is equal to the old free space + the
373 // utilization slop for the new allocation.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700374 if (is_verbose_gc_) {
375 LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700376 << " for " << size << "-byte allocation";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700377 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700378 return ptr;
379 }
380
381 // Most allocations should have succeeded by now, so the heap is
382 // really full, really fragmented, or the requested size is really
383 // big. Do another GC, collecting SoftReferences this time. The VM
384 // spec requires that all SoftReferences have been collected and
385 // cleared before throwing an OOME.
386
Elliott Hughes418dfe72011-10-06 18:56:27 -0700387 // OLD-TODO: wait for the finalizers from the previous GC to finish
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700388 if (is_verbose_gc_) {
389 LOG(INFO) << "Forcing collection of SoftReferences for "
390 << size << "-byte allocation";
391 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700392 CollectGarbageInternal();
393 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700394 if (ptr != NULL) {
395 return ptr;
396 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700397
Carl Shapiro69759ea2011-07-21 18:13:35 -0700398 LOG(ERROR) << "Out of memory on a " << size << " byte allocation";
399
Carl Shapiro58551df2011-07-24 03:09:51 -0700400 // TODO: tell the HeapSource to dump its state
401 // TODO: dump stack traces for all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700402
Carl Shapiro69759ea2011-07-21 18:13:35 -0700403 return NULL;
404}
405
Elliott Hughesbf86d042011-08-31 17:53:14 -0700406int64_t Heap::GetMaxMemory() {
407 UNIMPLEMENTED(WARNING);
408 return 0;
409}
410
411int64_t Heap::GetTotalMemory() {
412 UNIMPLEMENTED(WARNING);
413 return 0;
414}
415
416int64_t Heap::GetFreeMemory() {
417 UNIMPLEMENTED(WARNING);
418 return 0;
419}
420
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700421class InstanceCounter {
422 public:
423 InstanceCounter(Class* c, bool count_assignable)
424 : class_(c), count_assignable_(count_assignable), count_(0) {
425 }
426
427 size_t GetCount() {
428 return count_;
429 }
430
431 static void Callback(Object* o, void* arg) {
432 reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o);
433 }
434
435 private:
436 void VisitInstance(Object* o) {
437 Class* instance_class = o->GetClass();
438 if (count_assignable_) {
439 if (instance_class == class_) {
440 ++count_;
441 }
442 } else {
443 if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) {
444 ++count_;
445 }
446 }
447 }
448
449 Class* class_;
450 bool count_assignable_;
451 size_t count_;
452};
453
454int64_t Heap::CountInstances(Class* c, bool count_assignable) {
455 ScopedHeapLock lock;
456 InstanceCounter counter(c, count_assignable);
457 live_bitmap_->Walk(InstanceCounter::Callback, &counter);
458 return counter.GetCount();
459}
460
Carl Shapiro69759ea2011-07-21 18:13:35 -0700461void Heap::CollectGarbage() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700462 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700463 CollectGarbageInternal();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700464}
465
466void Heap::CollectGarbageInternal() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700467 lock_->AssertHeld();
Carl Shapiro58551df2011-07-24 03:09:51 -0700468
Elliott Hughes8d768a92011-09-14 16:35:25 -0700469 ThreadList* thread_list = Runtime::Current()->GetThreadList();
470 thread_list->SuspendAll();
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700471
472 size_t initial_size = num_bytes_allocated_;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700473 TimingLogger timings("CollectGarbageInternal");
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700474 uint64_t t0 = NanoTime();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700475 Object* cleared_references = NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -0700476 {
477 MarkSweep mark_sweep;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700478 timings.AddSplit("ctor");
Carl Shapiro58551df2011-07-24 03:09:51 -0700479
480 mark_sweep.Init();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700481 timings.AddSplit("Init");
Carl Shapiro58551df2011-07-24 03:09:51 -0700482
483 mark_sweep.MarkRoots();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700484 timings.AddSplit("MarkRoots");
Carl Shapiro58551df2011-07-24 03:09:51 -0700485
486 // Push marked roots onto the mark stack
487
488 // TODO: if concurrent
489 // unlock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700490 // thread_list->ResumeAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700491
492 mark_sweep.RecursiveMark();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700493 timings.AddSplit("RecursiveMark");
Carl Shapiro58551df2011-07-24 03:09:51 -0700494
495 // TODO: if concurrent
496 // lock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700497 // thread_list->SuspendAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700498 // re-mark root set
499 // scan dirty objects
500
501 mark_sweep.ProcessReferences(false);
Elliott Hughes307f75d2011-10-12 18:04:40 -0700502 timings.AddSplit("ProcessReferences");
Carl Shapiro58551df2011-07-24 03:09:51 -0700503
Elliott Hughes2da50362011-10-10 16:57:08 -0700504 // TODO: if concurrent
505 // swap bitmaps
Carl Shapiro58551df2011-07-24 03:09:51 -0700506
507 mark_sweep.Sweep();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700508 timings.AddSplit("Sweep");
Elliott Hughesadb460d2011-10-05 17:02:34 -0700509
510 cleared_references = mark_sweep.GetClearedReferences();
Carl Shapiro58551df2011-07-24 03:09:51 -0700511 }
512
513 GrowForUtilization();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700514 timings.AddSplit("GrowForUtilization");
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700515 uint64_t t1 = NanoTime();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700516 thread_list->ResumeAll();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700517
518 EnqueueClearedReferences(&cleared_references);
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700519
520 // TODO: somehow make the specific GC implementation (here MarkSweep) responsible for logging.
521 size_t bytes_freed = initial_size - num_bytes_allocated_;
522 bool is_small = (bytes_freed > 0 && bytes_freed < 1024);
523 size_t kib_freed = (bytes_freed > 0 ? std::max(bytes_freed/1024, 1U) : 0);
524
525 size_t footprint = alloc_space_->Size();
526 size_t percentFree = 100 - static_cast<size_t>(100.0f * float(num_bytes_allocated_) / footprint);
527
528 uint32_t duration = (t1 - t0)/1000/1000;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700529 if (is_verbose_gc_) {
530 LOG(INFO) << "GC freed " << (is_small ? "<" : "") << kib_freed << "KiB, "
531 << percentFree << "% free "
532 << (num_bytes_allocated_/1024) << "KiB/" << (footprint/1024) << "KiB, "
533 << "paused " << duration << "ms";
534 }
535 if (is_verbose_heap_) {
536 timings.Dump();
537 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700538}
539
540void Heap::WaitForConcurrentGcToComplete() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700541 lock_->AssertHeld();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700542}
543
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700544/* Terminology:
545 * 1. Footprint: Capacity we allocate from system.
546 * 2. Active space: a.k.a. alloc_space_.
547 * 3. Soft footprint: external allocation + spaces footprint + active space footprint
548 * 4. Overhead: soft footprint excluding active.
549 *
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700550 * Layout: (The spaces below might not be contiguous, but are lumped together to depict size.)
551 * |----------------------spaces footprint--------- --------------|----active space footprint----|
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700552 * |--active space allocated--|
553 * |--------------------soft footprint (include active)--------------------------------------|
554 * |----------------soft footprint excluding active---------------|
555 * |------------soft limit-------...|
556 * |------------------------------------ideal footprint-----------------------------------------...|
557 *
558 */
559
560// Sets the maximum number of bytes that the heap is allowed to
561// allocate from the system. Clamps to the appropriate maximum
562// value.
563// Old spaces will count against the ideal size.
564//
565void Heap::SetIdealFootprint(size_t max_allowed_footprint)
566{
567 if (max_allowed_footprint > Heap::maximum_size_) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700568 if (is_verbose_gc_) {
569 LOG(INFO) << "Clamp target GC heap from " << max_allowed_footprint
570 << " to " << Heap::maximum_size_;
571 }
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700572 max_allowed_footprint = Heap::maximum_size_;
573 }
574
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700575 alloc_space_->SetMaxAllowedFootprint(max_allowed_footprint);
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700576}
577
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700578// kHeapIdealFree is the ideal maximum free size, when we grow the heap for
579// utlization.
580static const size_t kHeapIdealFree = 2 * MB;
581// kHeapMinFree guarantees that you always have at least 512 KB free, when
582// you grow for utilization, regardless of target utilization ratio.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700583static const size_t kHeapMinFree = kHeapIdealFree / 4;
584
585// Given the current contents of the active space, increase the allowed
Carl Shapiro69759ea2011-07-21 18:13:35 -0700586// heap footprint to match the target utilization ratio. This should
587// only be called immediately after a full garbage collection.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700588//
Carl Shapiro69759ea2011-07-21 18:13:35 -0700589void Heap::GrowForUtilization() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700590 lock_->AssertHeld();
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700591
592 // We know what our utilization is at this moment.
593 // This doesn't actually resize any memory. It just lets the heap grow more
594 // when necessary.
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700595 size_t target_size(num_bytes_allocated_ / Heap::GetTargetHeapUtilization());
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700596
597 if (target_size > num_bytes_allocated_ + kHeapIdealFree) {
598 target_size = num_bytes_allocated_ + kHeapIdealFree;
599 } else if (target_size < num_bytes_allocated_ + kHeapMinFree) {
600 target_size = num_bytes_allocated_ + kHeapMinFree;
601 }
602
603 SetIdealFootprint(target_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700604}
605
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700606pid_t Heap::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700607 return lock_->GetOwner();
608}
609
Elliott Hughes92b3b562011-09-08 16:32:26 -0700610void Heap::Lock() {
Brian Carlstromfad71432011-10-16 20:25:10 -0700611 // Grab the lock, but put ourselves into Thread::kVmWait if it looks
612 // like we're going to have to wait on the mutex. This prevents
613 // deadlock if another thread is calling CollectGarbageInternal,
614 // since they will have the heap lock and be waiting for mutators to
615 // suspend.
616 if (!lock_->TryLock()) {
617 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
618 lock_->Lock();
619 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700620}
621
622void Heap::Unlock() {
623 lock_->Unlock();
624}
625
Elliott Hughesadb460d2011-10-05 17:02:34 -0700626void Heap::SetWellKnownClasses(Class* java_lang_ref_FinalizerReference,
627 Class* java_lang_ref_ReferenceQueue) {
628 java_lang_ref_FinalizerReference_ = java_lang_ref_FinalizerReference;
629 java_lang_ref_ReferenceQueue_ = java_lang_ref_ReferenceQueue;
630 CHECK(java_lang_ref_FinalizerReference_ != NULL);
631 CHECK(java_lang_ref_ReferenceQueue_ != NULL);
632}
633
634void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset,
635 MemberOffset reference_queue_offset,
636 MemberOffset reference_queueNext_offset,
637 MemberOffset reference_pendingNext_offset,
638 MemberOffset finalizer_reference_zombie_offset) {
639 reference_referent_offset_ = reference_referent_offset;
640 reference_queue_offset_ = reference_queue_offset;
641 reference_queueNext_offset_ = reference_queueNext_offset;
642 reference_pendingNext_offset_ = reference_pendingNext_offset;
643 finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset;
644 CHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
645 CHECK_NE(reference_queue_offset_.Uint32Value(), 0U);
646 CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U);
647 CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
648 CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
649}
650
651Object* Heap::GetReferenceReferent(Object* reference) {
652 DCHECK(reference != NULL);
653 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
654 return reference->GetFieldObject<Object*>(reference_referent_offset_, true);
655}
656
657void Heap::ClearReferenceReferent(Object* reference) {
658 DCHECK(reference != NULL);
659 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
660 reference->SetFieldObject(reference_referent_offset_, NULL, true);
661}
662
663// Returns true if the reference object has not yet been enqueued.
664bool Heap::IsEnqueuable(const Object* ref) {
665 DCHECK(ref != NULL);
666 const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false);
667 const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false);
668 return (queue != NULL) && (queue_next == NULL);
669}
670
671void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) {
672 DCHECK(ref != NULL);
673 CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL);
674 CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL);
675 EnqueuePendingReference(ref, cleared_reference_list);
676}
677
678void Heap::EnqueuePendingReference(Object* ref, Object** list) {
679 DCHECK(ref != NULL);
680 DCHECK(list != NULL);
681
682 if (*list == NULL) {
683 ref->SetFieldObject(reference_pendingNext_offset_, ref, false);
684 *list = ref;
685 } else {
686 Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
687 ref->SetFieldObject(reference_pendingNext_offset_, head, false);
688 (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false);
689 }
690}
691
692Object* Heap::DequeuePendingReference(Object** list) {
693 DCHECK(list != NULL);
694 DCHECK(*list != NULL);
695 Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
696 Object* ref;
697 if (*list == head) {
698 ref = *list;
699 *list = NULL;
700 } else {
701 Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
702 (*list)->SetFieldObject(reference_pendingNext_offset_, next, false);
703 ref = head;
704 }
705 ref->SetFieldObject(reference_pendingNext_offset_, NULL, false);
706 return ref;
707}
708
709void Heap::AddFinalizerReference(Object* object) {
710 static Method* FinalizerReference_add =
711 java_lang_ref_FinalizerReference_->FindDirectMethod("add", "(Ljava/lang/Object;)V");
712 DCHECK(FinalizerReference_add != NULL);
713 Object* args[] = { object };
714 FinalizerReference_add->Invoke(Thread::Current(), NULL, reinterpret_cast<byte*>(&args), NULL);
715}
716
717void Heap::EnqueueClearedReferences(Object** cleared) {
718 DCHECK(cleared != NULL);
719 if (*cleared != NULL) {
720 static Method* ReferenceQueue_add =
721 java_lang_ref_ReferenceQueue_->FindDirectMethod("add", "(Ljava/lang/ref/Reference;)V");
722 DCHECK(ReferenceQueue_add != NULL);
723
724 Thread* self = Thread::Current();
725 ScopedThreadStateChange tsc(self, Thread::kRunnable);
726 Object* args[] = { *cleared };
727 ReferenceQueue_add->Invoke(self, NULL, reinterpret_cast<byte*>(&args), NULL);
728 *cleared = NULL;
729 }
730}
731
Carl Shapiro69759ea2011-07-21 18:13:35 -0700732} // namespace art