blob: ac0c3669c60b5dd588c5ae4cb9b40412c1114306 [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
Ian Rogers5d76c432011-10-31 21:42:49 -07008#include "card_table.h"
Elliott Hughes767a1472011-10-26 18:49:02 -07009#include "debugger.h"
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070010#include "image.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070011#include "mark_sweep.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "object.h"
13#include "space.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070014#include "stl_util.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070015#include "thread_list.h"
Elliott Hughes767a1472011-10-26 18:49:02 -070016#include "timing_logger.h"
17#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070018
19namespace art {
20
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070021bool Heap::is_verbose_heap_ = false;
22
23bool Heap::is_verbose_gc_ = false;
24
Carl Shapiro58551df2011-07-24 03:09:51 -070025std::vector<Space*> Heap::spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -070026
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070027Space* Heap::alloc_space_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -070028
29size_t Heap::maximum_size_ = 0;
30
jeffhaoc1160702011-10-27 15:48:45 -070031size_t Heap::growth_size_ = 0;
32
Carl Shapiro58551df2011-07-24 03:09:51 -070033size_t Heap::num_bytes_allocated_ = 0;
34
35size_t Heap::num_objects_allocated_ = 0;
36
Carl Shapiro69759ea2011-07-21 18:13:35 -070037bool Heap::is_gc_running_ = false;
38
39HeapBitmap* Heap::mark_bitmap_ = NULL;
40
41HeapBitmap* Heap::live_bitmap_ = NULL;
42
Ian Rogers5d76c432011-10-31 21:42:49 -070043CardTable* Heap::card_table_ = NULL;
44
45bool Heap::card_marking_disabled_ = false;
46
Elliott Hughesadb460d2011-10-05 17:02:34 -070047Class* Heap::java_lang_ref_FinalizerReference_ = NULL;
48Class* Heap::java_lang_ref_ReferenceQueue_ = NULL;
49
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070050MemberOffset Heap::reference_referent_offset_ = MemberOffset(0);
51MemberOffset Heap::reference_queue_offset_ = MemberOffset(0);
52MemberOffset Heap::reference_queueNext_offset_ = MemberOffset(0);
53MemberOffset Heap::reference_pendingNext_offset_ = MemberOffset(0);
54MemberOffset Heap::finalizer_reference_zombie_offset_ = MemberOffset(0);
Brian Carlstrom1f870082011-08-23 16:02:11 -070055
Brian Carlstrom395520e2011-09-25 19:35:00 -070056float Heap::target_utilization_ = 0.5;
57
Elliott Hughes92b3b562011-09-08 16:32:26 -070058Mutex* Heap::lock_ = NULL;
59
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070060bool Heap::verify_objects_ = false;
61
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070062void Heap::Init(bool is_verbose_heap, bool is_verbose_gc,
jeffhaoc1160702011-10-27 15:48:45 -070063 size_t initial_size, size_t maximum_size, size_t growth_size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070064 const std::vector<std::string>& image_file_names) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070065 is_verbose_heap_ = is_verbose_heap;
66 is_verbose_gc_ = is_verbose_gc;
67
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070068 const Runtime* runtime = Runtime::Current();
Elliott Hughes352a4242011-10-31 15:15:21 -070069 if (Heap::IsVerboseHeap() || runtime->IsVerboseStartup()) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070070 LOG(INFO) << "Heap::Init entering";
71 }
72
Brian Carlstrom58ae9412011-10-04 00:56:06 -070073 // bounds of all spaces for allocating live and mark bitmaps
74 // there will be at least one space (the alloc space),
jeffhao39da0352011-11-04 14:58:55 -070075 // so set base to max, and limit and min to start
Brian Carlstrom58ae9412011-10-04 00:56:06 -070076 byte* base = reinterpret_cast<byte*>(std::numeric_limits<uintptr_t>::max());
Ian Rogers5d76c432011-10-31 21:42:49 -070077 byte* max = reinterpret_cast<byte*>(std::numeric_limits<uintptr_t>::min());
jeffhao39da0352011-11-04 14:58:55 -070078 byte* limit = reinterpret_cast<byte*>(std::numeric_limits<uintptr_t>::min());
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070079
Brian Carlstrom58ae9412011-10-04 00:56:06 -070080 byte* requested_base = NULL;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070081 std::vector<Space*> image_spaces;
82 for (size_t i = 0; i < image_file_names.size(); i++) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -070083 Space* space = Space::CreateFromImage(image_file_names[i]);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070084 if (space == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -070085 LOG(FATAL) << "Failed to create space from " << image_file_names[i];
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070086 }
87 image_spaces.push_back(space);
88 spaces_.push_back(space);
Brian Carlstrome24fa612011-09-29 00:53:55 -070089 byte* oat_limit_addr = space->GetImageHeader().GetOatLimitAddr();
Brian Carlstrom58ae9412011-10-04 00:56:06 -070090 if (oat_limit_addr > requested_base) {
91 requested_base = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_limit_addr),
92 kPageSize));
93 }
94 base = std::min(base, space->GetBase());
Ian Rogers5d76c432011-10-31 21:42:49 -070095 max = std::max(max, space->GetMax());
jeffhao39da0352011-11-04 14:58:55 -070096 limit = std::max(limit, space->GetLimit());
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070097 }
98
jeffhaoc1160702011-10-27 15:48:45 -070099 alloc_space_ = Space::Create("alloc space", initial_size, maximum_size, growth_size, requested_base);
Elliott Hughes307f75d2011-10-12 18:04:40 -0700100 if (alloc_space_ == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700101 LOG(FATAL) << "Failed to create alloc space";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700102 }
Elliott Hughes307f75d2011-10-12 18:04:40 -0700103 base = std::min(base, alloc_space_->GetBase());
Ian Rogers5d76c432011-10-31 21:42:49 -0700104 max = std::max(max, alloc_space_->GetMax());
jeffhao39da0352011-11-04 14:58:55 -0700105 limit = std::max(limit, alloc_space_->GetLimit());
Ian Rogers5d76c432011-10-31 21:42:49 -0700106 DCHECK_LT(base, max);
jeffhao39da0352011-11-04 14:58:55 -0700107 DCHECK_LT(base, limit);
Ian Rogers5d76c432011-10-31 21:42:49 -0700108 size_t num_bytes = max - base;
jeffhao39da0352011-11-04 14:58:55 -0700109 size_t limit_bytes = limit - base;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700110
111 // Allocate the initial live bitmap.
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800112 UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create("dalvik-bitmap-1", base, num_bytes));
Elliott Hughes90a33692011-08-30 13:27:07 -0700113 if (live_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700114 LOG(FATAL) << "Failed to create live bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115 }
116
117 // Allocate the initial mark bitmap.
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800118 UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create("dalvik-bitmap-2", base, num_bytes));
Elliott Hughes90a33692011-08-30 13:27:07 -0700119 if (mark_bitmap.get() == NULL) {
Elliott Hughesbe759c62011-09-08 19:38:21 -0700120 LOG(FATAL) << "Failed to create mark bitmap";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700121 }
122
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800123 // Allocate the card table.
jeffhao39da0352011-11-04 14:58:55 -0700124 UniquePtr<CardTable> card_table(CardTable::Create(base, num_bytes, limit_bytes));
Ian Rogers5d76c432011-10-31 21:42:49 -0700125 if (card_table.get() == NULL) {
126 LOG(FATAL) << "Failed to create card table";
127 }
128
Elliott Hughes307f75d2011-10-12 18:04:40 -0700129 spaces_.push_back(alloc_space_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700130 maximum_size_ = maximum_size;
jeffhaoc1160702011-10-27 15:48:45 -0700131 growth_size_ = growth_size;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700132 live_bitmap_ = live_bitmap.release();
133 mark_bitmap_ = mark_bitmap.release();
Ian Rogers5d76c432011-10-31 21:42:49 -0700134 card_table_ = card_table.release();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700135
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700136 num_bytes_allocated_ = 0;
137 num_objects_allocated_ = 0;
138
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700139 // Make image objects live (after live_bitmap_ is set)
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700140 for (size_t i = 0; i < image_spaces.size(); i++) {
141 RecordImageAllocations(image_spaces[i]);
142 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700143
Elliott Hughes85d15452011-09-16 17:33:01 -0700144 Heap::EnableObjectValidation();
145
Elliott Hughes92b3b562011-09-08 16:32:26 -0700146 // It's still to early to take a lock because there are no threads yet,
147 // but we can create the heap lock now. We don't create it earlier to
148 // make it clear that you can't use locks during heap initialization.
Elliott Hughes8daa0922011-09-11 13:46:25 -0700149 lock_ = new Mutex("Heap lock");
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700150
Elliott Hughes352a4242011-10-31 15:15:21 -0700151 if (Heap::IsVerboseHeap() || runtime->IsVerboseStartup()) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700152 LOG(INFO) << "Heap::Init exiting";
153 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700154}
155
156void Heap::Destroy() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700157 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700158 STLDeleteElements(&spaces_);
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700159 if (mark_bitmap_ != NULL) {
160 delete mark_bitmap_;
161 mark_bitmap_ = NULL;
162 }
163 if (live_bitmap_ != NULL) {
164 delete live_bitmap_;
165 }
166 live_bitmap_ = NULL;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700167}
168
Elliott Hughes418dfe72011-10-06 18:56:27 -0700169Object* Heap::AllocObject(Class* klass, size_t byte_count) {
170 {
171 ScopedHeapLock lock;
172 DCHECK(klass == NULL || klass->GetDescriptor() == NULL ||
173 (klass->IsClassClass() && byte_count >= sizeof(Class)) ||
174 (klass->IsVariableSize() || klass->GetObjectSize() == byte_count));
175 DCHECK_GE(byte_count, sizeof(Object));
176 Object* obj = AllocateLocked(byte_count);
177 if (obj != NULL) {
178 obj->SetClass(klass);
Elliott Hughes545a0642011-11-08 19:10:03 -0800179 if (Dbg::IsAllocTrackingEnabled()) {
180 Dbg::RecordAllocation(klass, byte_count);
181 }
Elliott Hughes418dfe72011-10-06 18:56:27 -0700182 return obj;
183 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700184 }
Elliott Hughes418dfe72011-10-06 18:56:27 -0700185
186 Thread::Current()->ThrowOutOfMemoryError(klass, byte_count);
187 return NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -0700188}
189
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700190bool Heap::IsHeapAddress(const Object* obj) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700191 // Note: we deliberately don't take the lock here, and mustn't test anything that would
192 // require taking the lock.
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700193 if (obj == NULL || !IsAligned<kObjectAlignment>(obj)) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700194 return false;
195 }
196 // TODO
197 return true;
198}
199
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700200bool Heap::IsLiveObjectLocked(const Object* obj) {
201 lock_->AssertHeld();
202 return IsHeapAddress(obj) && live_bitmap_->Test(obj);
203}
204
Elliott Hughes3e465b12011-09-02 18:26:12 -0700205#if VERIFY_OBJECT_ENABLED
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700206void Heap::VerifyObject(const Object* obj) {
Elliott Hughes85d15452011-09-16 17:33:01 -0700207 if (!verify_objects_) {
208 return;
209 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700210 ScopedHeapLock lock;
211 Heap::VerifyObjectLocked(obj);
212}
213#endif
214
215void Heap::VerifyObjectLocked(const Object* obj) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700216 lock_->AssertHeld();
Elliott Hughes85d15452011-09-16 17:33:01 -0700217 if (obj != NULL) {
Elliott Hughes06b37d92011-10-16 11:51:29 -0700218 if (!IsAligned<kObjectAlignment>(obj)) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700219 LOG(FATAL) << "Object isn't aligned: " << obj;
220 } else if (!live_bitmap_->Test(obj)) {
221 // TODO: we don't hold a lock here as it is assumed the live bit map
222 // isn't changing if the mutator is running.
223 LOG(FATAL) << "Object is dead: " << obj;
224 }
225 // Ignore early dawn of the universe verifications
Brian Carlstromdbc05252011-09-09 01:59:59 -0700226 if (num_objects_allocated_ > 10) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700227 const byte* raw_addr = reinterpret_cast<const byte*>(obj) +
228 Object::ClassOffset().Int32Value();
229 const Class* c = *reinterpret_cast<Class* const *>(raw_addr);
230 if (c == NULL) {
231 LOG(FATAL) << "Null class" << " in object: " << obj;
Elliott Hughes06b37d92011-10-16 11:51:29 -0700232 } else if (!IsAligned<kObjectAlignment>(c)) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700233 LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj;
234 } else if (!live_bitmap_->Test(c)) {
235 LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj;
236 }
237 // Check obj.getClass().getClass() == obj.getClass().getClass().getClass()
Ian Rogersad25ac52011-10-04 19:13:33 -0700238 // Note: we don't use the accessors here as they have internal sanity checks
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239 // that we don't want to run
240 raw_addr = reinterpret_cast<const byte*>(c) +
241 Object::ClassOffset().Int32Value();
242 const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr);
243 raw_addr = reinterpret_cast<const byte*>(c_c) +
244 Object::ClassOffset().Int32Value();
245 const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr);
246 CHECK_EQ(c_c, c_c_c);
247 }
248 }
249}
250
Brian Carlstrom78128a62011-09-15 17:21:19 -0700251void Heap::VerificationCallback(Object* obj, void* arg) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700252 DCHECK(obj != NULL);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700253 Heap::VerifyObjectLocked(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700254}
255
256void Heap::VerifyHeap() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700257 ScopedHeapLock lock;
258 live_bitmap_->Walk(Heap::VerificationCallback, NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700259}
260
Elliott Hughes92b3b562011-09-08 16:32:26 -0700261void Heap::RecordAllocationLocked(Space* space, const Object* obj) {
262#ifndef NDEBUG
263 if (Runtime::Current()->IsStarted()) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700264 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700265 }
266#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700267 size_t size = space->AllocationSize(obj);
268 DCHECK_NE(size, 0u);
269 num_bytes_allocated_ += size;
270 num_objects_allocated_ += 1;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700271
272 if (Runtime::Current()->HasStatsEnabled()) {
273 RuntimeStats* global_stats = Runtime::Current()->GetStats();
274 RuntimeStats* thread_stats = Thread::Current()->GetStats();
275 ++global_stats->allocated_objects;
276 ++thread_stats->allocated_objects;
277 global_stats->allocated_bytes += size;
278 thread_stats->allocated_bytes += size;
279 }
280
Carl Shapiro58551df2011-07-24 03:09:51 -0700281 live_bitmap_->Set(obj);
282}
283
Elliott Hughes307f75d2011-10-12 18:04:40 -0700284void Heap::RecordFreeLocked(size_t freed_objects, size_t freed_bytes) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700285 lock_->AssertHeld();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700286
287 if (freed_objects < num_objects_allocated_) {
288 num_objects_allocated_ -= freed_objects;
289 } else {
290 num_objects_allocated_ = 0;
291 }
292 if (freed_bytes < num_bytes_allocated_) {
293 num_bytes_allocated_ -= freed_bytes;
Carl Shapiro58551df2011-07-24 03:09:51 -0700294 } else {
295 num_bytes_allocated_ = 0;
296 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700297
298 if (Runtime::Current()->HasStatsEnabled()) {
299 RuntimeStats* global_stats = Runtime::Current()->GetStats();
300 RuntimeStats* thread_stats = Thread::Current()->GetStats();
301 ++global_stats->freed_objects;
302 ++thread_stats->freed_objects;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700303 global_stats->freed_bytes += freed_bytes;
304 thread_stats->freed_bytes += freed_bytes;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700305 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700306}
307
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700308void Heap::RecordImageAllocations(Space* space) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700309 const Runtime* runtime = Runtime::Current();
Elliott Hughes352a4242011-10-31 15:15:21 -0700310 if (Heap::IsVerboseHeap() || runtime->IsVerboseStartup()) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700311 LOG(INFO) << "Heap::RecordImageAllocations entering";
312 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700313 DCHECK(!Runtime::Current()->IsStarted());
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700314 CHECK(space != NULL);
315 CHECK(live_bitmap_ != NULL);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700316 byte* current = space->GetBase() + RoundUp(sizeof(ImageHeader), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700317 while (current < space->GetLimit()) {
Elliott Hughes06b37d92011-10-16 11:51:29 -0700318 DCHECK_ALIGNED(current, kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700319 const Object* obj = reinterpret_cast<const Object*>(current);
320 live_bitmap_->Set(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700321 current += RoundUp(obj->SizeOf(), kObjectAlignment);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700322 }
Elliott Hughes352a4242011-10-31 15:15:21 -0700323 if (Heap::IsVerboseHeap() || runtime->IsVerboseStartup()) {
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700324 LOG(INFO) << "Heap::RecordImageAllocations exiting";
325 }
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700326}
327
Elliott Hughes92b3b562011-09-08 16:32:26 -0700328Object* Heap::AllocateLocked(size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700329 lock_->AssertHeld();
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700330 DCHECK(alloc_space_ != NULL);
331 Space* space = alloc_space_;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700332 Object* obj = AllocateLocked(space, size);
Carl Shapiro58551df2011-07-24 03:09:51 -0700333 if (obj != NULL) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700334 RecordAllocationLocked(space, obj);
Carl Shapiro58551df2011-07-24 03:09:51 -0700335 }
336 return obj;
337}
338
Elliott Hughes92b3b562011-09-08 16:32:26 -0700339Object* Heap::AllocateLocked(Space* space, size_t size) {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700340 lock_->AssertHeld();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700341
Brian Carlstromb82b6872011-10-26 17:18:07 -0700342 // Since allocation can cause a GC which will need to SuspendAll,
343 // make sure all allocators are in the kRunnable state.
344 DCHECK_EQ(Thread::Current()->GetState(), Thread::kRunnable);
345
Carl Shapiro69759ea2011-07-21 18:13:35 -0700346 // Fail impossible allocations. TODO: collect soft references.
jeffhaoc1160702011-10-27 15:48:45 -0700347 if (size > growth_size_) {
Carl Shapiro69759ea2011-07-21 18:13:35 -0700348 return NULL;
349 }
350
Carl Shapiro58551df2011-07-24 03:09:51 -0700351 Object* ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352 if (ptr != NULL) {
353 return ptr;
354 }
355
356 // The allocation failed. If the GC is running, block until it
357 // completes and retry.
358 if (is_gc_running_) {
359 // The GC is concurrently tracing the heap. Release the heap
360 // lock, wait for the GC to complete, and retrying allocating.
361 WaitForConcurrentGcToComplete();
Carl Shapiro58551df2011-07-24 03:09:51 -0700362 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700363 if (ptr != NULL) {
364 return ptr;
365 }
366 }
367
368 // Another failure. Our thread was starved or there may be too many
369 // live objects. Try a foreground GC. This will have no effect if
370 // the concurrent GC is already running.
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700371 if (Runtime::Current()->HasStatsEnabled()) {
372 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
373 ++Thread::Current()->GetStats()->gc_for_alloc_count;
374 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700375 CollectGarbageInternal();
376 ptr = space->AllocWithoutGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700377 if (ptr != NULL) {
378 return ptr;
379 }
380
381 // Even that didn't work; this is an exceptional state.
382 // Try harder, growing the heap if necessary.
Carl Shapiro58551df2011-07-24 03:09:51 -0700383 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700384 if (ptr != NULL) {
385 //size_t new_footprint = dvmHeapSourceGetIdealFootprint();
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700386 size_t new_footprint = space->GetMaxAllowedFootprint();
Elliott Hughes418dfe72011-10-06 18:56:27 -0700387 // OLD-TODO: may want to grow a little bit more so that the amount of
Carl Shapiro58551df2011-07-24 03:09:51 -0700388 // free space is equal to the old free space + the
389 // utilization slop for the new allocation.
Elliott Hughes352a4242011-10-31 15:15:21 -0700390 if (Heap::IsVerboseGc()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700391 LOG(INFO) << "Grow heap (frag case) to " << new_footprint / MB
Brian Carlstromf28bc5b2011-10-26 01:15:03 -0700392 << " for " << size << "-byte allocation";
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700393 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700394 return ptr;
395 }
396
397 // Most allocations should have succeeded by now, so the heap is
398 // really full, really fragmented, or the requested size is really
399 // big. Do another GC, collecting SoftReferences this time. The VM
400 // spec requires that all SoftReferences have been collected and
401 // cleared before throwing an OOME.
402
Elliott Hughes418dfe72011-10-06 18:56:27 -0700403 // OLD-TODO: wait for the finalizers from the previous GC to finish
Elliott Hughes352a4242011-10-31 15:15:21 -0700404 if (Heap::IsVerboseGc()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700405 LOG(INFO) << "Forcing collection of SoftReferences for "
406 << size << "-byte allocation";
407 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700408 CollectGarbageInternal();
409 ptr = space->AllocWithGrowth(size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700410 if (ptr != NULL) {
411 return ptr;
412 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700413
Carl Shapiro69759ea2011-07-21 18:13:35 -0700414 LOG(ERROR) << "Out of memory on a " << size << " byte allocation";
415
Carl Shapiro58551df2011-07-24 03:09:51 -0700416 // TODO: tell the HeapSource to dump its state
417 // TODO: dump stack traces for all threads
Carl Shapiro69759ea2011-07-21 18:13:35 -0700418
Carl Shapiro69759ea2011-07-21 18:13:35 -0700419 return NULL;
420}
421
Elliott Hughesbf86d042011-08-31 17:53:14 -0700422int64_t Heap::GetMaxMemory() {
jeffhaoc1160702011-10-27 15:48:45 -0700423 return growth_size_;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700424}
425
426int64_t Heap::GetTotalMemory() {
Elliott Hughes7162ad92011-10-27 14:08:42 -0700427 return alloc_space_->Size();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700428}
429
430int64_t Heap::GetFreeMemory() {
Elliott Hughes7162ad92011-10-27 14:08:42 -0700431 return alloc_space_->Size() - num_bytes_allocated_;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700432}
433
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700434class InstanceCounter {
435 public:
436 InstanceCounter(Class* c, bool count_assignable)
437 : class_(c), count_assignable_(count_assignable), count_(0) {
438 }
439
440 size_t GetCount() {
441 return count_;
442 }
443
444 static void Callback(Object* o, void* arg) {
445 reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o);
446 }
447
448 private:
449 void VisitInstance(Object* o) {
450 Class* instance_class = o->GetClass();
451 if (count_assignable_) {
452 if (instance_class == class_) {
453 ++count_;
454 }
455 } else {
456 if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) {
457 ++count_;
458 }
459 }
460 }
461
462 Class* class_;
463 bool count_assignable_;
464 size_t count_;
465};
466
467int64_t Heap::CountInstances(Class* c, bool count_assignable) {
468 ScopedHeapLock lock;
469 InstanceCounter counter(c, count_assignable);
470 live_bitmap_->Walk(InstanceCounter::Callback, &counter);
471 return counter.GetCount();
472}
473
Carl Shapiro69759ea2011-07-21 18:13:35 -0700474void Heap::CollectGarbage() {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700475 ScopedHeapLock lock;
Carl Shapiro58551df2011-07-24 03:09:51 -0700476 CollectGarbageInternal();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700477}
478
479void Heap::CollectGarbageInternal() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700480 lock_->AssertHeld();
Carl Shapiro58551df2011-07-24 03:09:51 -0700481
Elliott Hughes8d768a92011-09-14 16:35:25 -0700482 ThreadList* thread_list = Runtime::Current()->GetThreadList();
483 thread_list->SuspendAll();
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700484
485 size_t initial_size = num_bytes_allocated_;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700486 TimingLogger timings("CollectGarbageInternal");
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700487 uint64_t t0 = NanoTime();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700488 Object* cleared_references = NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -0700489 {
490 MarkSweep mark_sweep;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700491 timings.AddSplit("ctor");
Carl Shapiro58551df2011-07-24 03:09:51 -0700492
493 mark_sweep.Init();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700494 timings.AddSplit("Init");
Carl Shapiro58551df2011-07-24 03:09:51 -0700495
496 mark_sweep.MarkRoots();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700497 timings.AddSplit("MarkRoots");
Carl Shapiro58551df2011-07-24 03:09:51 -0700498
Ian Rogers5d76c432011-10-31 21:42:49 -0700499 mark_sweep.ScanDirtyImageRoots();
500 timings.AddSplit("DirtyImageRoots");
501
502 // Roots are marked on the bitmap and the mark_stack is empty
503 DCHECK(mark_sweep.IsMarkStackEmpty());
Carl Shapiro58551df2011-07-24 03:09:51 -0700504
505 // TODO: if concurrent
506 // unlock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700507 // thread_list->ResumeAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700508
Ian Rogers5d76c432011-10-31 21:42:49 -0700509 // Recursively mark all bits set in the non-image mark bitmap
Carl Shapiro58551df2011-07-24 03:09:51 -0700510 mark_sweep.RecursiveMark();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700511 timings.AddSplit("RecursiveMark");
Carl Shapiro58551df2011-07-24 03:09:51 -0700512
513 // TODO: if concurrent
514 // lock heap
Elliott Hughes8d768a92011-09-14 16:35:25 -0700515 // thread_list->SuspendAll();
Carl Shapiro58551df2011-07-24 03:09:51 -0700516 // re-mark root set
517 // scan dirty objects
518
519 mark_sweep.ProcessReferences(false);
Elliott Hughes307f75d2011-10-12 18:04:40 -0700520 timings.AddSplit("ProcessReferences");
Carl Shapiro58551df2011-07-24 03:09:51 -0700521
Elliott Hughes2da50362011-10-10 16:57:08 -0700522 // TODO: if concurrent
523 // swap bitmaps
Carl Shapiro58551df2011-07-24 03:09:51 -0700524
525 mark_sweep.Sweep();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700526 timings.AddSplit("Sweep");
Elliott Hughesadb460d2011-10-05 17:02:34 -0700527
528 cleared_references = mark_sweep.GetClearedReferences();
Carl Shapiro58551df2011-07-24 03:09:51 -0700529 }
530
531 GrowForUtilization();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700532 timings.AddSplit("GrowForUtilization");
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700533 uint64_t t1 = NanoTime();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700534 thread_list->ResumeAll();
Elliott Hughesadb460d2011-10-05 17:02:34 -0700535
536 EnqueueClearedReferences(&cleared_references);
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700537
538 // TODO: somehow make the specific GC implementation (here MarkSweep) responsible for logging.
539 size_t bytes_freed = initial_size - num_bytes_allocated_;
540 bool is_small = (bytes_freed > 0 && bytes_freed < 1024);
541 size_t kib_freed = (bytes_freed > 0 ? std::max(bytes_freed/1024, 1U) : 0);
542
Elliott Hughes7162ad92011-10-27 14:08:42 -0700543 size_t total = GetTotalMemory();
544 size_t percentFree = 100 - static_cast<size_t>(100.0f * float(num_bytes_allocated_) / total);
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700545
546 uint32_t duration = (t1 - t0)/1000/1000;
Elliott Hughesaaed81d2011-11-07 15:11:47 -0800547 bool gc_was_particularly_slow = (duration > 100); // TODO: crank this down for concurrent.
548 if (Heap::IsVerboseGc() || gc_was_particularly_slow) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700549 LOG(INFO) << "GC freed " << (is_small ? "<" : "") << kib_freed << "KiB, "
550 << percentFree << "% free "
Elliott Hughes7162ad92011-10-27 14:08:42 -0700551 << (num_bytes_allocated_/1024) << "KiB/" << (total/1024) << "KiB, "
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700552 << "paused " << duration << "ms";
553 }
Elliott Hughes767a1472011-10-26 18:49:02 -0700554 Dbg::GcDidFinish();
Elliott Hughes352a4242011-10-31 15:15:21 -0700555 if (Heap::IsVerboseHeap()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700556 timings.Dump();
557 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700558}
559
560void Heap::WaitForConcurrentGcToComplete() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700561 lock_->AssertHeld();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700562}
563
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700564void Heap::WalkHeap(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg) {
565 typedef std::vector<Space*>::iterator It; // C++0x auto.
566 for (It it = spaces_.begin(); it != spaces_.end(); ++it) {
567 (*it)->Walk(callback, arg);
568 }
569}
570
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700571/* Terminology:
572 * 1. Footprint: Capacity we allocate from system.
573 * 2. Active space: a.k.a. alloc_space_.
574 * 3. Soft footprint: external allocation + spaces footprint + active space footprint
575 * 4. Overhead: soft footprint excluding active.
576 *
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700577 * Layout: (The spaces below might not be contiguous, but are lumped together to depict size.)
578 * |----------------------spaces footprint--------- --------------|----active space footprint----|
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700579 * |--active space allocated--|
580 * |--------------------soft footprint (include active)--------------------------------------|
581 * |----------------soft footprint excluding active---------------|
582 * |------------soft limit-------...|
583 * |------------------------------------ideal footprint-----------------------------------------...|
584 *
585 */
586
587// Sets the maximum number of bytes that the heap is allowed to
588// allocate from the system. Clamps to the appropriate maximum
589// value.
590// Old spaces will count against the ideal size.
591//
592void Heap::SetIdealFootprint(size_t max_allowed_footprint)
593{
jeffhaoc1160702011-10-27 15:48:45 -0700594 if (max_allowed_footprint > Heap::growth_size_) {
Elliott Hughes352a4242011-10-31 15:15:21 -0700595 if (Heap::IsVerboseGc()) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700596 LOG(INFO) << "Clamp target GC heap from " << max_allowed_footprint
jeffhaoc1160702011-10-27 15:48:45 -0700597 << " to " << Heap::growth_size_;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700598 }
jeffhaoc1160702011-10-27 15:48:45 -0700599 max_allowed_footprint = Heap::growth_size_;
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700600 }
601
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700602 alloc_space_->SetMaxAllowedFootprint(max_allowed_footprint);
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700603}
604
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700605// kHeapIdealFree is the ideal maximum free size, when we grow the heap for
606// utlization.
607static const size_t kHeapIdealFree = 2 * MB;
608// kHeapMinFree guarantees that you always have at least 512 KB free, when
609// you grow for utilization, regardless of target utilization ratio.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700610static const size_t kHeapMinFree = kHeapIdealFree / 4;
611
612// Given the current contents of the active space, increase the allowed
Carl Shapiro69759ea2011-07-21 18:13:35 -0700613// heap footprint to match the target utilization ratio. This should
614// only be called immediately after a full garbage collection.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700615//
Carl Shapiro69759ea2011-07-21 18:13:35 -0700616void Heap::GrowForUtilization() {
Elliott Hughes8daa0922011-09-11 13:46:25 -0700617 lock_->AssertHeld();
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700618
619 // We know what our utilization is at this moment.
620 // This doesn't actually resize any memory. It just lets the heap grow more
621 // when necessary.
Elliott Hughes362f9bc2011-10-17 18:56:41 -0700622 size_t target_size(num_bytes_allocated_ / Heap::GetTargetHeapUtilization());
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700623
624 if (target_size > num_bytes_allocated_ + kHeapIdealFree) {
625 target_size = num_bytes_allocated_ + kHeapIdealFree;
626 } else if (target_size < num_bytes_allocated_ + kHeapMinFree) {
627 target_size = num_bytes_allocated_ + kHeapMinFree;
628 }
629
630 SetIdealFootprint(target_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700631}
632
jeffhaoc1160702011-10-27 15:48:45 -0700633void Heap::ClearGrowthLimit() {
634 ScopedHeapLock lock;
635 WaitForConcurrentGcToComplete();
636 CHECK_GE(maximum_size_, growth_size_);
637 growth_size_ = maximum_size_;
638 alloc_space_->ClearGrowthLimit();
jeffhao39da0352011-11-04 14:58:55 -0700639 card_table_->ClearGrowthLimit();
jeffhaoc1160702011-10-27 15:48:45 -0700640}
641
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700642pid_t Heap::GetLockOwner() {
Elliott Hughesaccd83d2011-10-17 14:25:58 -0700643 return lock_->GetOwner();
644}
645
Elliott Hughes92b3b562011-09-08 16:32:26 -0700646void Heap::Lock() {
Brian Carlstromfad71432011-10-16 20:25:10 -0700647 // Grab the lock, but put ourselves into Thread::kVmWait if it looks
648 // like we're going to have to wait on the mutex. This prevents
649 // deadlock if another thread is calling CollectGarbageInternal,
650 // since they will have the heap lock and be waiting for mutators to
651 // suspend.
652 if (!lock_->TryLock()) {
653 ScopedThreadStateChange tsc(Thread::Current(), Thread::kVmWait);
654 lock_->Lock();
655 }
Elliott Hughes92b3b562011-09-08 16:32:26 -0700656}
657
658void Heap::Unlock() {
659 lock_->Unlock();
660}
661
Elliott Hughesadb460d2011-10-05 17:02:34 -0700662void Heap::SetWellKnownClasses(Class* java_lang_ref_FinalizerReference,
663 Class* java_lang_ref_ReferenceQueue) {
664 java_lang_ref_FinalizerReference_ = java_lang_ref_FinalizerReference;
665 java_lang_ref_ReferenceQueue_ = java_lang_ref_ReferenceQueue;
666 CHECK(java_lang_ref_FinalizerReference_ != NULL);
667 CHECK(java_lang_ref_ReferenceQueue_ != NULL);
668}
669
670void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset,
671 MemberOffset reference_queue_offset,
672 MemberOffset reference_queueNext_offset,
673 MemberOffset reference_pendingNext_offset,
674 MemberOffset finalizer_reference_zombie_offset) {
675 reference_referent_offset_ = reference_referent_offset;
676 reference_queue_offset_ = reference_queue_offset;
677 reference_queueNext_offset_ = reference_queueNext_offset;
678 reference_pendingNext_offset_ = reference_pendingNext_offset;
679 finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset;
680 CHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
681 CHECK_NE(reference_queue_offset_.Uint32Value(), 0U);
682 CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U);
683 CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
684 CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
685}
686
687Object* Heap::GetReferenceReferent(Object* reference) {
688 DCHECK(reference != NULL);
689 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
690 return reference->GetFieldObject<Object*>(reference_referent_offset_, true);
691}
692
693void Heap::ClearReferenceReferent(Object* reference) {
694 DCHECK(reference != NULL);
695 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
696 reference->SetFieldObject(reference_referent_offset_, NULL, true);
697}
698
699// Returns true if the reference object has not yet been enqueued.
700bool Heap::IsEnqueuable(const Object* ref) {
701 DCHECK(ref != NULL);
702 const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false);
703 const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false);
704 return (queue != NULL) && (queue_next == NULL);
705}
706
707void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) {
708 DCHECK(ref != NULL);
709 CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL);
710 CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL);
711 EnqueuePendingReference(ref, cleared_reference_list);
712}
713
714void Heap::EnqueuePendingReference(Object* ref, Object** list) {
715 DCHECK(ref != NULL);
716 DCHECK(list != NULL);
717
718 if (*list == NULL) {
719 ref->SetFieldObject(reference_pendingNext_offset_, ref, false);
720 *list = ref;
721 } else {
722 Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
723 ref->SetFieldObject(reference_pendingNext_offset_, head, false);
724 (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false);
725 }
726}
727
728Object* Heap::DequeuePendingReference(Object** list) {
729 DCHECK(list != NULL);
730 DCHECK(*list != NULL);
731 Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
732 Object* ref;
733 if (*list == head) {
734 ref = *list;
735 *list = NULL;
736 } else {
737 Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
738 (*list)->SetFieldObject(reference_pendingNext_offset_, next, false);
739 ref = head;
740 }
741 ref->SetFieldObject(reference_pendingNext_offset_, NULL, false);
742 return ref;
743}
744
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700745void Heap::AddFinalizerReference(Thread* self, Object* object) {
746 ScopedThreadStateChange tsc(self, Thread::kRunnable);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700747 static Method* FinalizerReference_add =
748 java_lang_ref_FinalizerReference_->FindDirectMethod("add", "(Ljava/lang/Object;)V");
749 DCHECK(FinalizerReference_add != NULL);
750 Object* args[] = { object };
Ian Rogers5d4bdc22011-11-02 22:15:43 -0700751 FinalizerReference_add->Invoke(self, NULL, reinterpret_cast<byte*>(&args), NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700752}
753
754void Heap::EnqueueClearedReferences(Object** cleared) {
755 DCHECK(cleared != NULL);
756 if (*cleared != NULL) {
757 static Method* ReferenceQueue_add =
758 java_lang_ref_ReferenceQueue_->FindDirectMethod("add", "(Ljava/lang/ref/Reference;)V");
759 DCHECK(ReferenceQueue_add != NULL);
760
761 Thread* self = Thread::Current();
762 ScopedThreadStateChange tsc(self, Thread::kRunnable);
763 Object* args[] = { *cleared };
764 ReferenceQueue_add->Invoke(self, NULL, reinterpret_cast<byte*>(&args), NULL);
765 *cleared = NULL;
766 }
767}
768
Carl Shapiro69759ea2011-07-21 18:13:35 -0700769} // namespace art