Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "heap.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 18 | |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_DALVIK |
| 20 | #include <cutils/trace.h> |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 21 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 22 | #include <limits> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 25 | #include "base/stl_util.h" |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 26 | #include "common_throws.h" |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 27 | #include "cutils/sched_policy.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 28 | #include "debugger.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 29 | #include "gc/accounting/atomic_stack.h" |
| 30 | #include "gc/accounting/card_table-inl.h" |
| 31 | #include "gc/accounting/heap_bitmap-inl.h" |
| 32 | #include "gc/accounting/mod_union_table-inl.h" |
| 33 | #include "gc/accounting/space_bitmap-inl.h" |
| 34 | #include "gc/collector/mark_sweep-inl.h" |
| 35 | #include "gc/collector/partial_mark_sweep.h" |
| 36 | #include "gc/collector/sticky_mark_sweep.h" |
| 37 | #include "gc/space/image_space.h" |
| 38 | #include "gc/space/large_object_space.h" |
| 39 | #include "gc/space/space-inl.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 40 | #include "image.h" |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 41 | #include "invoke_arg_array_builder.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 42 | #include "mirror/class-inl.h" |
| 43 | #include "mirror/field-inl.h" |
| 44 | #include "mirror/object.h" |
| 45 | #include "mirror/object-inl.h" |
| 46 | #include "mirror/object_array-inl.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 47 | #include "object_utils.h" |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 48 | #include "os.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 49 | #include "ScopedLocalRef.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 50 | #include "scoped_thread_state_change.h" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 51 | #include "sirt_ref.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 52 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 53 | #include "UniquePtr.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 54 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 55 | |
| 56 | namespace art { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 57 | namespace gc { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 58 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 59 | // When to create a log message about a slow GC, 100ms. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 60 | static const uint64_t kSlowGcThreshold = MsToNs(100); |
Mathieu Chartier | 8235331 | 2013-07-18 10:47:51 -0700 | [diff] [blame] | 61 | // When to create a log message about a long pause, 5ms. |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 62 | static const uint64_t kLongGcPauseThreshold = MsToNs(5); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 63 | static const bool kDumpGcPerformanceOnShutdown = false; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 64 | // Minimum amount of remaining bytes before a concurrent GC is triggered. |
| 65 | static const size_t kMinConcurrentRemainingBytes = 128 * KB; |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 66 | const double Heap::kDefaultTargetUtilization = 0.5; |
| 67 | |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 68 | Heap::Heap(size_t initial_size, size_t growth_limit, size_t min_free, size_t max_free, |
| 69 | double target_utilization, size_t capacity, |
Mathieu Chartier | 63a5434 | 2013-07-23 13:17:59 -0700 | [diff] [blame] | 70 | const std::string& original_image_file_name, bool concurrent_gc, size_t num_gc_threads) |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 71 | : alloc_space_(NULL), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 72 | card_table_(NULL), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 73 | concurrent_gc_(concurrent_gc), |
Mathieu Chartier | 63a5434 | 2013-07-23 13:17:59 -0700 | [diff] [blame] | 74 | num_gc_threads_(num_gc_threads), |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 75 | have_zygote_space_(false), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 76 | reference_queue_lock_(NULL), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 77 | is_gc_running_(false), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 78 | last_gc_type_(collector::kGcTypeNone), |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 79 | next_gc_type_(collector::kGcTypePartial), |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 80 | capacity_(capacity), |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 81 | growth_limit_(growth_limit), |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 82 | max_allowed_footprint_(initial_size), |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 83 | native_footprint_gc_watermark_(initial_size), |
| 84 | native_footprint_limit_(2 * initial_size), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 85 | concurrent_start_bytes_(concurrent_gc ? initial_size - (kMinConcurrentRemainingBytes) |
| 86 | : std::numeric_limits<size_t>::max()), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 87 | total_bytes_freed_ever_(0), |
| 88 | total_objects_freed_ever_(0), |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 89 | large_object_threshold_(3 * kPageSize), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 90 | num_bytes_allocated_(0), |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 91 | native_bytes_allocated_(0), |
Mathieu Chartier | 8235331 | 2013-07-18 10:47:51 -0700 | [diff] [blame] | 92 | process_state_(PROCESS_STATE_TOP), |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 93 | gc_memory_overhead_(0), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 94 | verify_missing_card_marks_(false), |
| 95 | verify_system_weaks_(false), |
| 96 | verify_pre_gc_heap_(false), |
| 97 | verify_post_gc_heap_(false), |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 98 | verify_mod_union_table_(false), |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 99 | min_alloc_space_size_for_sticky_gc_(2 * MB), |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 100 | min_remaining_space_for_sticky_gc_(1 * MB), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 101 | last_trim_time_ms_(0), |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 102 | allocation_rate_(0), |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 103 | max_allocation_stack_size_(kDesiredHeapVerification > kNoHeapVerification? KB : MB), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 104 | reference_referent_offset_(0), |
| 105 | reference_queue_offset_(0), |
| 106 | reference_queueNext_offset_(0), |
| 107 | reference_pendingNext_offset_(0), |
| 108 | finalizer_reference_zombie_offset_(0), |
Mathieu Chartier | 0051be6 | 2012-10-12 17:47:11 -0700 | [diff] [blame] | 109 | min_free_(min_free), |
| 110 | max_free_(max_free), |
| 111 | target_utilization_(target_utilization), |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 112 | total_wait_time_(0), |
| 113 | measure_allocation_time_(false), |
| 114 | total_allocation_time_(0), |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 115 | verify_object_mode_(kHeapVerificationNotPermitted) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 116 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 117 | LOG(INFO) << "Heap() entering"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 120 | live_bitmap_.reset(new accounting::HeapBitmap(this)); |
| 121 | mark_bitmap_.reset(new accounting::HeapBitmap(this)); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 122 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 123 | // Requested begin for the alloc space, to follow the mapped image and oat files |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 124 | byte* requested_alloc_space_begin = NULL; |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 125 | std::string image_file_name(original_image_file_name); |
| 126 | if (!image_file_name.empty()) { |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 127 | space::ImageSpace* image_space = space::ImageSpace::Create(image_file_name); |
| 128 | CHECK(image_space != NULL) << "Failed to create space for " << image_file_name; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 129 | AddContinuousSpace(image_space); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 130 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 131 | // isn't going to get in the middle |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 132 | byte* oat_file_end_addr = image_space->GetImageHeader().GetOatFileEnd(); |
| 133 | CHECK_GT(oat_file_end_addr, image_space->End()); |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 134 | if (oat_file_end_addr > requested_alloc_space_begin) { |
| 135 | requested_alloc_space_begin = |
| 136 | reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_file_end_addr), |
| 137 | kPageSize)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 138 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 141 | // Allocate the large object space. |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 142 | const bool kUseFreeListSpaceForLOS = false; |
| 143 | if (kUseFreeListSpaceForLOS) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 144 | large_object_space_ = space::FreeListSpace::Create("large object space", NULL, capacity); |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 145 | } else { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 146 | large_object_space_ = space::LargeObjectMapSpace::Create("large object space"); |
Ian Rogers | 22a2086 | 2013-03-16 16:34:57 -0700 | [diff] [blame] | 147 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 148 | CHECK(large_object_space_ != NULL) << "Failed to create large object space"; |
| 149 | AddDiscontinuousSpace(large_object_space_); |
Mathieu Chartier | 8e9a149 | 2012-10-04 12:25:40 -0700 | [diff] [blame] | 150 | |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 151 | alloc_space_ = space::DlMallocSpace::Create(Runtime::Current()->IsZygote() ? "zygote space" : "alloc space", |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 152 | initial_size, |
| 153 | growth_limit, capacity, |
Brian Carlstrom | 56d947f | 2013-07-15 13:14:23 -0700 | [diff] [blame] | 154 | requested_alloc_space_begin); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 155 | CHECK(alloc_space_ != NULL) << "Failed to create alloc space"; |
jeffhao | 8161c03 | 2012-10-31 15:50:00 -0700 | [diff] [blame] | 156 | alloc_space_->SetFootprintLimit(alloc_space_->Capacity()); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 157 | AddContinuousSpace(alloc_space_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 158 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 159 | // Compute heap capacity. Continuous spaces are sorted in order of Begin(). |
| 160 | byte* heap_begin = continuous_spaces_.front()->Begin(); |
| 161 | size_t heap_capacity = continuous_spaces_.back()->End() - continuous_spaces_.front()->Begin(); |
| 162 | if (continuous_spaces_.back()->IsDlMallocSpace()) { |
| 163 | heap_capacity += continuous_spaces_.back()->AsDlMallocSpace()->NonGrowthLimitCapacity(); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 164 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 165 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 166 | // Mark image objects in the live bitmap |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 167 | // TODO: C++0x |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 168 | typedef std::vector<space::ContinuousSpace*>::iterator It; |
| 169 | for (It it = continuous_spaces_.begin(); it != continuous_spaces_.end(); ++it) { |
| 170 | space::ContinuousSpace* space = *it; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 171 | if (space->IsImageSpace()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 172 | space::ImageSpace* image_space = space->AsImageSpace(); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 173 | image_space->RecordImageAllocations(image_space->GetLiveBitmap()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 177 | // Allocate the card table. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 178 | card_table_.reset(accounting::CardTable::Create(heap_begin, heap_capacity)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 179 | CHECK(card_table_.get() != NULL) << "Failed to create card table"; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 180 | |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 181 | image_mod_union_table_.reset(new accounting::ModUnionTableCardCache(this)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 182 | CHECK(image_mod_union_table_.get() != NULL) << "Failed to create image mod-union table"; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 183 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 184 | zygote_mod_union_table_.reset(new accounting::ModUnionTableCardCache(this)); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 185 | CHECK(zygote_mod_union_table_.get() != NULL) << "Failed to create Zygote mod-union table"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 186 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 187 | // TODO: Count objects in the image space here. |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 188 | num_bytes_allocated_ = 0; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 189 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 190 | // Default mark stack size in bytes. |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 191 | static const size_t default_mark_stack_size = 64 * KB; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 192 | mark_stack_.reset(accounting::ObjectStack::Create("mark stack", default_mark_stack_size)); |
| 193 | allocation_stack_.reset(accounting::ObjectStack::Create("allocation stack", |
| 194 | max_allocation_stack_size_)); |
| 195 | live_stack_.reset(accounting::ObjectStack::Create("live stack", |
| 196 | max_allocation_stack_size_)); |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 197 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 198 | // It's still too early to take a lock because there are no threads yet, but we can create locks |
| 199 | // now. We don't create it earlier to make it clear that you can't use locks during heap |
| 200 | // initialization. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 201 | gc_complete_lock_ = new Mutex("GC complete lock"); |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 202 | gc_complete_cond_.reset(new ConditionVariable("GC complete condition variable", |
| 203 | *gc_complete_lock_)); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 204 | |
Mathieu Chartier | 63a5434 | 2013-07-23 13:17:59 -0700 | [diff] [blame] | 205 | // Create the reference queue lock, this is required so for parallel object scanning in the GC. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 206 | reference_queue_lock_ = new Mutex("reference queue lock"); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 207 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 208 | last_gc_time_ns_ = NanoTime(); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 209 | last_gc_size_ = GetBytesAllocated(); |
| 210 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 211 | // Create our garbage collectors. |
| 212 | for (size_t i = 0; i < 2; ++i) { |
| 213 | const bool concurrent = i != 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 214 | mark_sweep_collectors_.push_back(new collector::MarkSweep(this, concurrent)); |
| 215 | mark_sweep_collectors_.push_back(new collector::PartialMarkSweep(this, concurrent)); |
| 216 | mark_sweep_collectors_.push_back(new collector::StickyMarkSweep(this, concurrent)); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Brian Carlstrom | 4274889 | 2013-07-18 18:04:08 -0700 | [diff] [blame] | 219 | CHECK_NE(max_allowed_footprint_, 0U); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 220 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 221 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 222 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 225 | void Heap::CreateThreadPool() { |
Mathieu Chartier | 63a5434 | 2013-07-23 13:17:59 -0700 | [diff] [blame] | 226 | thread_pool_.reset(new ThreadPool(num_gc_threads_)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void Heap::DeleteThreadPool() { |
| 230 | thread_pool_.reset(NULL); |
| 231 | } |
| 232 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 233 | // Sort spaces based on begin address |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 234 | struct ContinuousSpaceSorter { |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 235 | bool operator()(const space::ContinuousSpace* a, const space::ContinuousSpace* b) const { |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 236 | return a->Begin() < b->Begin(); |
| 237 | } |
| 238 | }; |
| 239 | |
Mathieu Chartier | 8235331 | 2013-07-18 10:47:51 -0700 | [diff] [blame] | 240 | void Heap::UpdateProcessState(ProcessState process_state) { |
| 241 | process_state_ = process_state; |
| 242 | } |
| 243 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 244 | void Heap::AddContinuousSpace(space::ContinuousSpace* space) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 245 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 246 | DCHECK(space != NULL); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 247 | DCHECK(space->GetLiveBitmap() != NULL); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 248 | live_bitmap_->AddContinuousSpaceBitmap(space->GetLiveBitmap()); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 249 | DCHECK(space->GetMarkBitmap() != NULL); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 250 | mark_bitmap_->AddContinuousSpaceBitmap(space->GetMarkBitmap()); |
| 251 | continuous_spaces_.push_back(space); |
| 252 | if (space->IsDlMallocSpace() && !space->IsLargeObjectSpace()) { |
| 253 | alloc_space_ = space->AsDlMallocSpace(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 256 | // Ensure that spaces remain sorted in increasing order of start address (required for CMS finger) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 257 | std::sort(continuous_spaces_.begin(), continuous_spaces_.end(), ContinuousSpaceSorter()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 258 | |
| 259 | // Ensure that ImageSpaces < ZygoteSpaces < AllocSpaces so that we can do address based checks to |
| 260 | // avoid redundant marking. |
| 261 | bool seen_zygote = false, seen_alloc = false; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 262 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 263 | for (It it = continuous_spaces_.begin(); it != continuous_spaces_.end(); ++it) { |
| 264 | space::ContinuousSpace* space = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 265 | if (space->IsImageSpace()) { |
| 266 | DCHECK(!seen_zygote); |
| 267 | DCHECK(!seen_alloc); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 268 | } else if (space->IsZygoteSpace()) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 269 | DCHECK(!seen_alloc); |
| 270 | seen_zygote = true; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 271 | } else if (space->IsDlMallocSpace()) { |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 272 | seen_alloc = true; |
| 273 | } |
| 274 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 277 | void Heap::RegisterGCAllocation(size_t bytes) { |
| 278 | if (this != NULL) { |
| 279 | gc_memory_overhead_.fetch_add(bytes); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void Heap::RegisterGCDeAllocation(size_t bytes) { |
| 284 | if (this != NULL) { |
| 285 | gc_memory_overhead_.fetch_sub(bytes); |
| 286 | } |
| 287 | } |
| 288 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 289 | void Heap::AddDiscontinuousSpace(space::DiscontinuousSpace* space) { |
| 290 | WriterMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
| 291 | DCHECK(space != NULL); |
| 292 | DCHECK(space->GetLiveObjects() != NULL); |
| 293 | live_bitmap_->AddDiscontinuousObjectSet(space->GetLiveObjects()); |
| 294 | DCHECK(space->GetMarkObjects() != NULL); |
| 295 | mark_bitmap_->AddDiscontinuousObjectSet(space->GetMarkObjects()); |
| 296 | discontinuous_spaces_.push_back(space); |
| 297 | } |
| 298 | |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 299 | void Heap::DumpGcPerformanceInfo(std::ostream& os) { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 300 | // Dump cumulative timings. |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 301 | os << "Dumping cumulative Gc timings\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 302 | uint64_t total_duration = 0; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 303 | |
| 304 | // Dump cumulative loggers for each GC type. |
| 305 | // TODO: C++0x |
| 306 | uint64_t total_paused_time = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 307 | typedef std::vector<collector::MarkSweep*>::const_iterator It; |
| 308 | for (It it = mark_sweep_collectors_.begin(); |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 309 | it != mark_sweep_collectors_.end(); ++it) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 310 | collector::MarkSweep* collector = *it; |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 311 | CumulativeLogger& logger = collector->GetCumulativeTimings(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 312 | if (logger.GetTotalNs() != 0) { |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 313 | os << Dumpable<CumulativeLogger>(logger); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 314 | const uint64_t total_ns = logger.GetTotalNs(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 315 | const uint64_t total_pause_ns = (*it)->GetTotalPausedTimeNs(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 316 | double seconds = NsToMs(logger.GetTotalNs()) / 1000.0; |
| 317 | const uint64_t freed_bytes = collector->GetTotalFreedBytes(); |
| 318 | const uint64_t freed_objects = collector->GetTotalFreedObjects(); |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 319 | os << collector->GetName() << " total time: " << PrettyDuration(total_ns) << "\n" |
| 320 | << collector->GetName() << " paused time: " << PrettyDuration(total_pause_ns) << "\n" |
| 321 | << collector->GetName() << " freed: " << freed_objects |
| 322 | << " objects with total size " << PrettySize(freed_bytes) << "\n" |
| 323 | << collector->GetName() << " throughput: " << freed_objects / seconds << "/s / " |
| 324 | << PrettySize(freed_bytes / seconds) << "/s\n"; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 325 | total_duration += total_ns; |
| 326 | total_paused_time += total_pause_ns; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | uint64_t allocation_time = static_cast<uint64_t>(total_allocation_time_) * kTimeAdjust; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 330 | size_t total_objects_allocated = GetObjectsAllocatedEver(); |
| 331 | size_t total_bytes_allocated = GetBytesAllocatedEver(); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 332 | if (total_duration != 0) { |
Brian Carlstrom | 2d88862 | 2013-07-18 17:02:00 -0700 | [diff] [blame] | 333 | const double total_seconds = static_cast<double>(total_duration / 1000) / 1000000.0; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 334 | os << "Total time spent in GC: " << PrettyDuration(total_duration) << "\n"; |
| 335 | os << "Mean GC size throughput: " |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 336 | << PrettySize(GetBytesFreedEver() / total_seconds) << "/s\n"; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 337 | os << "Mean GC object throughput: " |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 338 | << (GetObjectsFreedEver() / total_seconds) << " objects/s\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 339 | } |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 340 | os << "Total number of allocations: " << total_objects_allocated << "\n"; |
| 341 | os << "Total bytes allocated " << PrettySize(total_bytes_allocated) << "\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 342 | if (measure_allocation_time_) { |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 343 | os << "Total time spent allocating: " << PrettyDuration(allocation_time) << "\n"; |
| 344 | os << "Mean allocation time: " << PrettyDuration(allocation_time / total_objects_allocated) |
| 345 | << "\n"; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 346 | } |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 347 | os << "Total mutator paused time: " << PrettyDuration(total_paused_time) << "\n"; |
| 348 | os << "Total time waiting for GC to complete: " << PrettyDuration(total_wait_time_) << "\n"; |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 349 | os << "Approximate GC data structures memory overhead: " << gc_memory_overhead_; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 352 | Heap::~Heap() { |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 353 | if (kDumpGcPerformanceOnShutdown) { |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 354 | DumpGcPerformanceInfo(LOG(INFO)); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 357 | STLDeleteElements(&mark_sweep_collectors_); |
| 358 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 359 | // If we don't reset then the mark stack complains in it's destructor. |
| 360 | allocation_stack_->Reset(); |
| 361 | live_stack_->Reset(); |
| 362 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 363 | VLOG(heap) << "~Heap()"; |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 364 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 365 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 366 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 367 | // those threads can't resume. We're the only running thread, and we can do whatever we like... |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 368 | STLDeleteElements(&continuous_spaces_); |
| 369 | STLDeleteElements(&discontinuous_spaces_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 370 | delete gc_complete_lock_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 371 | delete reference_queue_lock_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 374 | space::ContinuousSpace* Heap::FindContinuousSpaceFromObject(const mirror::Object* obj, |
| 375 | bool fail_ok) const { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 376 | // TODO: C++0x auto |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 377 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 378 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 379 | if ((*it)->Contains(obj)) { |
| 380 | return *it; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 383 | if (!fail_ok) { |
| 384 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 385 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 386 | return NULL; |
| 387 | } |
| 388 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 389 | space::DiscontinuousSpace* Heap::FindDiscontinuousSpaceFromObject(const mirror::Object* obj, |
| 390 | bool fail_ok) const { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 391 | // TODO: C++0x auto |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 392 | typedef std::vector<space::DiscontinuousSpace*>::const_iterator It; |
| 393 | for (It it = discontinuous_spaces_.begin(), end = discontinuous_spaces_.end(); it != end; ++it) { |
| 394 | if ((*it)->Contains(obj)) { |
| 395 | return *it; |
| 396 | } |
| 397 | } |
| 398 | if (!fail_ok) { |
| 399 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 400 | } |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | space::Space* Heap::FindSpaceFromObject(const mirror::Object* obj, bool fail_ok) const { |
| 405 | space::Space* result = FindContinuousSpaceFromObject(obj, true); |
| 406 | if (result != NULL) { |
| 407 | return result; |
| 408 | } |
| 409 | return FindDiscontinuousSpaceFromObject(obj, true); |
| 410 | } |
| 411 | |
| 412 | space::ImageSpace* Heap::GetImageSpace() const { |
| 413 | // TODO: C++0x auto |
| 414 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 415 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 416 | if ((*it)->IsImageSpace()) { |
| 417 | return (*it)->AsImageSpace(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 423 | static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 424 | size_t chunk_size = reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start); |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 425 | if (used_bytes < chunk_size) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 426 | size_t chunk_free_bytes = chunk_size - used_bytes; |
| 427 | size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg); |
| 428 | max_contiguous_allocation = std::max(max_contiguous_allocation, chunk_free_bytes); |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 429 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 432 | mirror::Object* Heap::AllocObject(Thread* self, mirror::Class* c, size_t byte_count) { |
| 433 | DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(mirror::Class)) || |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 434 | (c->IsVariableSize() || c->GetObjectSize() == byte_count) || |
| 435 | strlen(ClassHelper(c).GetDescriptor()) == 0); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 436 | DCHECK_GE(byte_count, sizeof(mirror::Object)); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 437 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 438 | mirror::Object* obj = NULL; |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 439 | size_t size = 0; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 440 | uint64_t allocation_start = 0; |
Ian Rogers | 333cf1b | 2013-07-24 11:57:02 -0700 | [diff] [blame] | 441 | if (UNLIKELY(measure_allocation_time_)) { |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 442 | allocation_start = NanoTime() / kTimeAdjust; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 443 | } |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 444 | |
| 445 | // We need to have a zygote space or else our newly allocated large object can end up in the |
| 446 | // Zygote resulting in it being prematurely freed. |
| 447 | // We can only do this for primive objects since large objects will not be within the card table |
| 448 | // range. This also means that we rely on SetClass not dirtying the object's card. |
Ian Rogers | 333cf1b | 2013-07-24 11:57:02 -0700 | [diff] [blame] | 449 | bool large_object_allocation = |
| 450 | byte_count >= large_object_threshold_ && have_zygote_space_ && c->IsPrimitiveArray(); |
| 451 | if (UNLIKELY(large_object_allocation)) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 452 | size = RoundUp(byte_count, kPageSize); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 453 | obj = Allocate(self, large_object_space_, size); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 454 | // Make sure that our large object didn't get placed anywhere within the space interval or else |
| 455 | // it breaks the immune range. |
| 456 | DCHECK(obj == NULL || |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 457 | reinterpret_cast<byte*>(obj) < continuous_spaces_.front()->Begin() || |
| 458 | reinterpret_cast<byte*>(obj) >= continuous_spaces_.back()->End()); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 459 | } else { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 460 | obj = Allocate(self, alloc_space_, byte_count); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 461 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 462 | // Ensure that we did not allocate into a zygote space. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 463 | DCHECK(obj == NULL || !have_zygote_space_ || !FindSpaceFromObject(obj, false)->IsZygoteSpace()); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 464 | size = alloc_space_->AllocationSize(obj); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 467 | if (LIKELY(obj != NULL)) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 468 | obj->SetClass(c); |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 469 | |
| 470 | // Record allocation after since we want to use the atomic add for the atomic fence to guard |
| 471 | // the SetClass since we do not want the class to appear NULL in another thread. |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 472 | RecordAllocation(size, obj); |
Mathieu Chartier | 037813d | 2012-08-23 16:44:59 -0700 | [diff] [blame] | 473 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 474 | if (Dbg::IsAllocTrackingEnabled()) { |
| 475 | Dbg::RecordAllocation(c, byte_count); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 476 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 477 | if (static_cast<size_t>(num_bytes_allocated_) >= concurrent_start_bytes_) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 478 | // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 479 | SirtRef<mirror::Object> ref(self, obj); |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 480 | RequestConcurrentGC(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 481 | } |
| 482 | VerifyObject(obj); |
| 483 | |
Ian Rogers | 333cf1b | 2013-07-24 11:57:02 -0700 | [diff] [blame] | 484 | if (UNLIKELY(measure_allocation_time_)) { |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 485 | total_allocation_time_.fetch_add(NanoTime() / kTimeAdjust - allocation_start); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 488 | return obj; |
Ian Rogers | 333cf1b | 2013-07-24 11:57:02 -0700 | [diff] [blame] | 489 | } else { |
| 490 | std::ostringstream oss; |
| 491 | int64_t total_bytes_free = GetFreeMemory(); |
| 492 | oss << "Failed to allocate a " << byte_count << " byte allocation with " << total_bytes_free |
| 493 | << " free bytes"; |
| 494 | // If the allocation failed due to fragmentation, print out the largest continuous allocation. |
| 495 | if (!large_object_allocation && total_bytes_free >= byte_count) { |
| 496 | size_t max_contiguous_allocation = 0; |
| 497 | // TODO: C++0x auto |
| 498 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 499 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
| 500 | space::ContinuousSpace* space = *it; |
| 501 | if (space->IsDlMallocSpace()) { |
| 502 | space->AsDlMallocSpace()->Walk(MSpaceChunkCallback, &max_contiguous_allocation); |
| 503 | } |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 504 | } |
Ian Rogers | 333cf1b | 2013-07-24 11:57:02 -0700 | [diff] [blame] | 505 | oss << "; failed due to fragmentation (largest possible contiguous allocation " |
| 506 | << max_contiguous_allocation << " bytes)"; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 507 | } |
Ian Rogers | 333cf1b | 2013-07-24 11:57:02 -0700 | [diff] [blame] | 508 | self->ThrowOutOfMemoryError(oss.str().c_str()); |
| 509 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 510 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 513 | bool Heap::IsHeapAddress(const mirror::Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 514 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 515 | // require taking the lock. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 516 | if (obj == NULL) { |
| 517 | return true; |
| 518 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 519 | if (UNLIKELY(!IsAligned<kObjectAlignment>(obj))) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 520 | return false; |
| 521 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 522 | return FindSpaceFromObject(obj, true) != NULL; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 525 | bool Heap::IsLiveObjectLocked(const mirror::Object* obj) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 526 | //Locks::heap_bitmap_lock_->AssertReaderHeld(Thread::Current()); |
| 527 | if (obj == NULL) { |
| 528 | return false; |
| 529 | } |
| 530 | if (UNLIKELY(!IsAligned<kObjectAlignment>(obj))) { |
| 531 | return false; |
| 532 | } |
| 533 | space::ContinuousSpace* cont_space = FindContinuousSpaceFromObject(obj, true); |
| 534 | if (cont_space != NULL) { |
| 535 | if (cont_space->GetLiveBitmap()->Test(obj)) { |
| 536 | return true; |
| 537 | } |
| 538 | } else { |
| 539 | space::DiscontinuousSpace* disc_space = FindDiscontinuousSpaceFromObject(obj, true); |
| 540 | if (disc_space != NULL) { |
| 541 | if (disc_space->GetLiveObjects()->Test(obj)) { |
| 542 | return true; |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | for (size_t i = 0; i < 5; ++i) { |
| 547 | if (allocation_stack_->Contains(const_cast<mirror::Object*>(obj)) || |
| 548 | live_stack_->Contains(const_cast<mirror::Object*>(obj))) { |
| 549 | return true; |
| 550 | } |
| 551 | NanoSleep(MsToNs(10)); |
| 552 | } |
| 553 | return false; |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 556 | void Heap::VerifyObjectImpl(const mirror::Object* obj) { |
| 557 | if (Thread::Current() == NULL || |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 558 | Runtime::Current()->GetThreadList()->GetLockOwner() == Thread::Current()->GetTid()) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 559 | return; |
| 560 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 561 | VerifyObjectBody(obj); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 562 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 563 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 564 | void Heap::DumpSpaces() { |
| 565 | // TODO: C++0x auto |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 566 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 567 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
| 568 | space::ContinuousSpace* space = *it; |
| 569 | accounting::SpaceBitmap* live_bitmap = space->GetLiveBitmap(); |
| 570 | accounting::SpaceBitmap* mark_bitmap = space->GetMarkBitmap(); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 571 | LOG(INFO) << space << " " << *space << "\n" |
| 572 | << live_bitmap << " " << *live_bitmap << "\n" |
| 573 | << mark_bitmap << " " << *mark_bitmap; |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 574 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 575 | typedef std::vector<space::DiscontinuousSpace*>::const_iterator It2; |
| 576 | for (It2 it = discontinuous_spaces_.begin(), end = discontinuous_spaces_.end(); it != end; ++it) { |
| 577 | space::DiscontinuousSpace* space = *it; |
| 578 | LOG(INFO) << space << " " << *space << "\n"; |
Mathieu Chartier | 128c52c | 2012-10-16 14:12:41 -0700 | [diff] [blame] | 579 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 582 | void Heap::VerifyObjectBody(const mirror::Object* obj) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 583 | if (UNLIKELY(!IsAligned<kObjectAlignment>(obj))) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 584 | LOG(FATAL) << "Object isn't aligned: " << obj; |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 585 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 586 | if (UNLIKELY(GetObjectsAllocated() <= 10)) { // Ignore early dawn of the universe verifications. |
| 587 | return; |
| 588 | } |
| 589 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 590 | mirror::Object::ClassOffset().Int32Value(); |
| 591 | const mirror::Class* c = *reinterpret_cast<mirror::Class* const *>(raw_addr); |
| 592 | if (UNLIKELY(c == NULL)) { |
| 593 | LOG(FATAL) << "Null class in object: " << obj; |
| 594 | } else if (UNLIKELY(!IsAligned<kObjectAlignment>(c))) { |
| 595 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 596 | } |
| 597 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
| 598 | // Note: we don't use the accessors here as they have internal sanity checks |
| 599 | // that we don't want to run |
| 600 | raw_addr = reinterpret_cast<const byte*>(c) + mirror::Object::ClassOffset().Int32Value(); |
| 601 | const mirror::Class* c_c = *reinterpret_cast<mirror::Class* const *>(raw_addr); |
| 602 | raw_addr = reinterpret_cast<const byte*>(c_c) + mirror::Object::ClassOffset().Int32Value(); |
| 603 | const mirror::Class* c_c_c = *reinterpret_cast<mirror::Class* const *>(raw_addr); |
| 604 | CHECK_EQ(c_c, c_c_c); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 605 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 606 | if (verify_object_mode_ != kVerifyAllFast) { |
| 607 | // TODO: the bitmap tests below are racy if VerifyObjectBody is called without the |
| 608 | // heap_bitmap_lock_. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 609 | if (!IsLiveObjectLocked(obj)) { |
| 610 | DumpSpaces(); |
| 611 | LOG(FATAL) << "Object is dead: " << obj; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 612 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 613 | if (!IsLiveObjectLocked(c)) { |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 614 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 615 | } |
Mathieu Chartier | dcf8d72 | 2012-08-02 14:55:54 -0700 | [diff] [blame] | 616 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 619 | void Heap::VerificationCallback(mirror::Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 620 | DCHECK(obj != NULL); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 621 | reinterpret_cast<Heap*>(arg)->VerifyObjectBody(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | void Heap::VerifyHeap() { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 625 | ReaderMutexLock mu(Thread::Current(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 626 | GetLiveBitmap()->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 629 | void Heap::RecordAllocation(size_t size, mirror::Object* obj) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 630 | DCHECK(obj != NULL); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 631 | DCHECK_GT(size, 0u); |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 632 | num_bytes_allocated_.fetch_add(size); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 633 | |
| 634 | if (Runtime::Current()->HasStatsEnabled()) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 635 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 636 | ++thread_stats->allocated_objects; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 637 | thread_stats->allocated_bytes += size; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 638 | |
| 639 | // TODO: Update these atomically. |
| 640 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 641 | ++global_stats->allocated_objects; |
| 642 | global_stats->allocated_bytes += size; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 643 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 644 | |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 645 | // This is safe to do since the GC will never free objects which are neither in the allocation |
| 646 | // stack or the live bitmap. |
| 647 | while (!allocation_stack_->AtomicPushBack(obj)) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 648 | CollectGarbageInternal(collector::kGcTypeSticky, kGcCauseForAlloc, false); |
Mathieu Chartier | d8195f1 | 2012-10-05 12:21:28 -0700 | [diff] [blame] | 649 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 652 | void Heap::RecordFree(size_t freed_objects, size_t freed_bytes) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 653 | DCHECK_LE(freed_bytes, static_cast<size_t>(num_bytes_allocated_)); |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 654 | num_bytes_allocated_.fetch_sub(freed_bytes); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 655 | |
| 656 | if (Runtime::Current()->HasStatsEnabled()) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 657 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 658 | thread_stats->freed_objects += freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 659 | thread_stats->freed_bytes += freed_bytes; |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 660 | |
| 661 | // TODO: Do this concurrently. |
| 662 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 663 | global_stats->freed_objects += freed_objects; |
| 664 | global_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 665 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 666 | } |
| 667 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 668 | mirror::Object* Heap::TryToAllocate(Thread* self, space::AllocSpace* space, size_t alloc_size, |
| 669 | bool grow) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 670 | // Should we try to use a CAS here and fix up num_bytes_allocated_ later with AllocationSize? |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 671 | if (num_bytes_allocated_ + alloc_size > max_allowed_footprint_) { |
| 672 | // max_allowed_footprint_ <= growth_limit_ so it is safe to check in here. |
| 673 | if (num_bytes_allocated_ + alloc_size > growth_limit_) { |
| 674 | // Completely out of memory. |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 675 | return NULL; |
| 676 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 679 | return space->Alloc(self, alloc_size); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 682 | mirror::Object* Heap::Allocate(Thread* self, space::AllocSpace* space, size_t alloc_size) { |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 683 | // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are |
| 684 | // done in the runnable state where suspension is expected. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 685 | DCHECK_EQ(self->GetState(), kRunnable); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 686 | self->AssertThreadSuspensionIsAllowable(); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 687 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 688 | mirror::Object* ptr = TryToAllocate(self, space, alloc_size, false); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 689 | if (ptr != NULL) { |
| 690 | return ptr; |
| 691 | } |
| 692 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 693 | // The allocation failed. If the GC is running, block until it completes, and then retry the |
| 694 | // allocation. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 695 | collector::GcType last_gc = WaitForConcurrentGcToComplete(self); |
| 696 | if (last_gc != collector::kGcTypeNone) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 697 | // A GC was in progress and we blocked, retry allocation now that memory has been freed. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 698 | ptr = TryToAllocate(self, space, alloc_size, false); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 699 | if (ptr != NULL) { |
| 700 | return ptr; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 704 | // Loop through our different Gc types and try to Gc until we get enough free memory. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 705 | for (size_t i = static_cast<size_t>(last_gc) + 1; |
| 706 | i < static_cast<size_t>(collector::kGcTypeMax); ++i) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 707 | bool run_gc = false; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 708 | collector::GcType gc_type = static_cast<collector::GcType>(i); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 709 | switch (gc_type) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 710 | case collector::kGcTypeSticky: { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 711 | const size_t alloc_space_size = alloc_space_->Size(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 712 | run_gc = alloc_space_size > min_alloc_space_size_for_sticky_gc_ && |
| 713 | alloc_space_->Capacity() - alloc_space_size >= min_remaining_space_for_sticky_gc_; |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 714 | break; |
| 715 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 716 | case collector::kGcTypePartial: |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 717 | run_gc = have_zygote_space_; |
| 718 | break; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 719 | case collector::kGcTypeFull: |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 720 | run_gc = true; |
| 721 | break; |
| 722 | default: |
| 723 | break; |
| 724 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 725 | |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 726 | if (run_gc) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 727 | // If we actually ran a different type of Gc than requested, we can skip the index forwards. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 728 | collector::GcType gc_type_ran = CollectGarbageInternal(gc_type, kGcCauseForAlloc, false); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 729 | DCHECK_GE(static_cast<size_t>(gc_type_ran), i); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 730 | i = static_cast<size_t>(gc_type_ran); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 731 | |
| 732 | // Did we free sufficient memory for the allocation to succeed? |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 733 | ptr = TryToAllocate(self, space, alloc_size, false); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 734 | if (ptr != NULL) { |
| 735 | return ptr; |
| 736 | } |
| 737 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | // Allocations have failed after GCs; this is an exceptional state. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 741 | // Try harder, growing the heap if necessary. |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 742 | ptr = TryToAllocate(self, space, alloc_size, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 743 | if (ptr != NULL) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 744 | return ptr; |
| 745 | } |
| 746 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 747 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 748 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 749 | // VM spec requires that all SoftReferences have been collected and cleared before throwing OOME. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 750 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 751 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 752 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) |
| 753 | << " allocation"; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 754 | |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 755 | // We don't need a WaitForConcurrentGcToComplete here either. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 756 | CollectGarbageInternal(collector::kGcTypeFull, kGcCauseForAlloc, true); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 757 | return TryToAllocate(self, space, alloc_size, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 758 | } |
| 759 | |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 760 | void Heap::SetTargetHeapUtilization(float target) { |
| 761 | DCHECK_GT(target, 0.0f); // asserted in Java code |
| 762 | DCHECK_LT(target, 1.0f); |
| 763 | target_utilization_ = target; |
| 764 | } |
| 765 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 766 | size_t Heap::GetObjectsAllocated() const { |
| 767 | size_t total = 0; |
| 768 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 769 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
| 770 | space::ContinuousSpace* space = *it; |
| 771 | if (space->IsDlMallocSpace()) { |
| 772 | total += space->AsDlMallocSpace()->GetObjectsAllocated(); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 775 | typedef std::vector<space::DiscontinuousSpace*>::const_iterator It2; |
| 776 | for (It2 it = discontinuous_spaces_.begin(), end = discontinuous_spaces_.end(); it != end; ++it) { |
| 777 | space::DiscontinuousSpace* space = *it; |
| 778 | total += space->AsLargeObjectSpace()->GetObjectsAllocated(); |
| 779 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 780 | return total; |
| 781 | } |
| 782 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 783 | size_t Heap::GetObjectsAllocatedEver() const { |
| 784 | size_t total = 0; |
| 785 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 786 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
| 787 | space::ContinuousSpace* space = *it; |
| 788 | if (space->IsDlMallocSpace()) { |
| 789 | total += space->AsDlMallocSpace()->GetTotalObjectsAllocated(); |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 790 | } |
| 791 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 792 | typedef std::vector<space::DiscontinuousSpace*>::const_iterator It2; |
| 793 | for (It2 it = discontinuous_spaces_.begin(), end = discontinuous_spaces_.end(); it != end; ++it) { |
| 794 | space::DiscontinuousSpace* space = *it; |
| 795 | total += space->AsLargeObjectSpace()->GetTotalObjectsAllocated(); |
| 796 | } |
| 797 | return total; |
| 798 | } |
| 799 | |
| 800 | size_t Heap::GetBytesAllocatedEver() const { |
| 801 | size_t total = 0; |
| 802 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 803 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
| 804 | space::ContinuousSpace* space = *it; |
| 805 | if (space->IsDlMallocSpace()) { |
| 806 | total += space->AsDlMallocSpace()->GetTotalBytesAllocated(); |
| 807 | } |
| 808 | } |
| 809 | typedef std::vector<space::DiscontinuousSpace*>::const_iterator It2; |
| 810 | for (It2 it = discontinuous_spaces_.begin(), end = discontinuous_spaces_.end(); it != end; ++it) { |
| 811 | space::DiscontinuousSpace* space = *it; |
| 812 | total += space->AsLargeObjectSpace()->GetTotalBytesAllocated(); |
| 813 | } |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 814 | return total; |
| 815 | } |
| 816 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 817 | class InstanceCounter { |
| 818 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 819 | InstanceCounter(const std::vector<mirror::Class*>& classes, bool use_is_assignable_from, uint64_t* counts) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 820 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 821 | : classes_(classes), use_is_assignable_from_(use_is_assignable_from), counts_(counts) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 822 | } |
| 823 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 824 | void operator()(const mirror::Object* o) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 825 | for (size_t i = 0; i < classes_.size(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 826 | const mirror::Class* instance_class = o->GetClass(); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 827 | if (use_is_assignable_from_) { |
| 828 | if (instance_class != NULL && classes_[i]->IsAssignableFrom(instance_class)) { |
| 829 | ++counts_[i]; |
| 830 | } |
| 831 | } else { |
| 832 | if (instance_class == classes_[i]) { |
| 833 | ++counts_[i]; |
| 834 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | } |
| 838 | |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 839 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 840 | const std::vector<mirror::Class*>& classes_; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 841 | bool use_is_assignable_from_; |
| 842 | uint64_t* const counts_; |
| 843 | |
| 844 | DISALLOW_COPY_AND_ASSIGN(InstanceCounter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 845 | }; |
| 846 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 847 | void Heap::CountInstances(const std::vector<mirror::Class*>& classes, bool use_is_assignable_from, |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 848 | uint64_t* counts) { |
| 849 | // We only want reachable instances, so do a GC. This also ensures that the alloc stack |
| 850 | // is empty, so the live bitmap is the only place we need to look. |
| 851 | Thread* self = Thread::Current(); |
| 852 | self->TransitionFromRunnableToSuspended(kNative); |
| 853 | CollectGarbage(false); |
| 854 | self->TransitionFromSuspendedToRunnable(); |
| 855 | |
| 856 | InstanceCounter counter(classes, use_is_assignable_from, counts); |
| 857 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 858 | GetLiveBitmap()->Visit(counter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 859 | } |
| 860 | |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 861 | class InstanceCollector { |
| 862 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 863 | InstanceCollector(mirror::Class* c, int32_t max_count, std::vector<mirror::Object*>& instances) |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 864 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 865 | : class_(c), max_count_(max_count), instances_(instances) { |
| 866 | } |
| 867 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 868 | void operator()(const mirror::Object* o) const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 869 | const mirror::Class* instance_class = o->GetClass(); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 870 | if (instance_class == class_) { |
| 871 | if (max_count_ == 0 || instances_.size() < max_count_) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 872 | instances_.push_back(const_cast<mirror::Object*>(o)); |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 878 | mirror::Class* class_; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 879 | uint32_t max_count_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 880 | std::vector<mirror::Object*>& instances_; |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 881 | |
| 882 | DISALLOW_COPY_AND_ASSIGN(InstanceCollector); |
| 883 | }; |
| 884 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 885 | void Heap::GetInstances(mirror::Class* c, int32_t max_count, |
| 886 | std::vector<mirror::Object*>& instances) { |
Elliott Hughes | 3b78c94 | 2013-01-15 17:35:41 -0800 | [diff] [blame] | 887 | // We only want reachable instances, so do a GC. This also ensures that the alloc stack |
| 888 | // is empty, so the live bitmap is the only place we need to look. |
| 889 | Thread* self = Thread::Current(); |
| 890 | self->TransitionFromRunnableToSuspended(kNative); |
| 891 | CollectGarbage(false); |
| 892 | self->TransitionFromSuspendedToRunnable(); |
| 893 | |
| 894 | InstanceCollector collector(c, max_count, instances); |
| 895 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 896 | GetLiveBitmap()->Visit(collector); |
| 897 | } |
| 898 | |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 899 | class ReferringObjectsFinder { |
| 900 | public: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 901 | ReferringObjectsFinder(mirror::Object* object, int32_t max_count, |
| 902 | std::vector<mirror::Object*>& referring_objects) |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 903 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
| 904 | : object_(object), max_count_(max_count), referring_objects_(referring_objects) { |
| 905 | } |
| 906 | |
| 907 | // For bitmap Visit. |
| 908 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for |
| 909 | // annotalysis on visitors. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 910 | void operator()(const mirror::Object* o) const NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 911 | collector::MarkSweep::VisitObjectReferences(o, *this); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 912 | } |
| 913 | |
| 914 | // For MarkSweep::VisitObjectReferences. |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 915 | void operator()(const mirror::Object* referrer, const mirror::Object* object, |
| 916 | const MemberOffset&, bool) const { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 917 | if (object == object_ && (max_count_ == 0 || referring_objects_.size() < max_count_)) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 918 | referring_objects_.push_back(const_cast<mirror::Object*>(referrer)); |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | |
| 922 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 923 | mirror::Object* object_; |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 924 | uint32_t max_count_; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 925 | std::vector<mirror::Object*>& referring_objects_; |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 926 | |
| 927 | DISALLOW_COPY_AND_ASSIGN(ReferringObjectsFinder); |
| 928 | }; |
| 929 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 930 | void Heap::GetReferringObjects(mirror::Object* o, int32_t max_count, |
| 931 | std::vector<mirror::Object*>& referring_objects) { |
Elliott Hughes | 0cbaff5 | 2013-01-16 15:28:01 -0800 | [diff] [blame] | 932 | // We only want reachable instances, so do a GC. This also ensures that the alloc stack |
| 933 | // is empty, so the live bitmap is the only place we need to look. |
| 934 | Thread* self = Thread::Current(); |
| 935 | self->TransitionFromRunnableToSuspended(kNative); |
| 936 | CollectGarbage(false); |
| 937 | self->TransitionFromSuspendedToRunnable(); |
| 938 | |
| 939 | ReferringObjectsFinder finder(o, max_count, referring_objects); |
| 940 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 941 | GetLiveBitmap()->Visit(finder); |
| 942 | } |
| 943 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 944 | void Heap::CollectGarbage(bool clear_soft_references) { |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 945 | // Even if we waited for a GC we still need to do another GC since weaks allocated during the |
| 946 | // last GC will not have necessarily been cleared. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 947 | Thread* self = Thread::Current(); |
| 948 | WaitForConcurrentGcToComplete(self); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 949 | CollectGarbageInternal(collector::kGcTypeFull, kGcCauseExplicit, clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 950 | } |
| 951 | |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 952 | void Heap::PreZygoteFork() { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 953 | static Mutex zygote_creation_lock_("zygote creation lock", kZygoteCreationLock); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 954 | // Do this before acquiring the zygote creation lock so that we don't get lock order violations. |
| 955 | CollectGarbage(false); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 956 | Thread* self = Thread::Current(); |
| 957 | MutexLock mu(self, zygote_creation_lock_); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 958 | |
| 959 | // Try to see if we have any Zygote spaces. |
| 960 | if (have_zygote_space_) { |
| 961 | return; |
| 962 | } |
| 963 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 964 | VLOG(heap) << "Starting PreZygoteFork with alloc space size " << PrettySize(alloc_space_->Size()); |
| 965 | |
| 966 | { |
| 967 | // Flush the alloc stack. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 968 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 969 | FlushAllocStack(); |
| 970 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 971 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 972 | // Turns the current alloc space into a Zygote space and obtain the new alloc space composed |
| 973 | // of the remaining available heap memory. |
| 974 | space::DlMallocSpace* zygote_space = alloc_space_; |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 975 | alloc_space_ = zygote_space->CreateZygoteSpace("alloc space"); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 976 | alloc_space_->SetFootprintLimit(alloc_space_->Capacity()); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 977 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 978 | // Change the GC retention policy of the zygote space to only collect when full. |
| 979 | zygote_space->SetGcRetentionPolicy(space::kGcRetentionPolicyFullCollect); |
| 980 | AddContinuousSpace(alloc_space_); |
| 981 | have_zygote_space_ = true; |
Mathieu Chartier | 1cd9c5c | 2012-08-23 10:52:44 -0700 | [diff] [blame] | 982 | |
Ian Rogers | 5f5a2c0 | 2012-09-17 10:52:08 -0700 | [diff] [blame] | 983 | // Reset the cumulative loggers since we now have a few additional timing phases. |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 984 | // TODO: C++0x |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 985 | typedef std::vector<collector::MarkSweep*>::const_iterator It; |
| 986 | for (It it = mark_sweep_collectors_.begin(), end = mark_sweep_collectors_.end(); |
| 987 | it != end; ++it) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 988 | (*it)->ResetCumulativeStatistics(); |
Mathieu Chartier | 0325e62 | 2012-09-05 14:22:51 -0700 | [diff] [blame] | 989 | } |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 992 | void Heap::FlushAllocStack() { |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 993 | MarkAllocStack(alloc_space_->GetLiveBitmap(), large_object_space_->GetLiveObjects(), |
| 994 | allocation_stack_.get()); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 995 | allocation_stack_->Reset(); |
| 996 | } |
| 997 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 998 | void Heap::MarkAllocStack(accounting::SpaceBitmap* bitmap, accounting::SpaceSetMap* large_objects, |
| 999 | accounting::ObjectStack* stack) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1000 | mirror::Object** limit = stack->End(); |
| 1001 | for (mirror::Object** it = stack->Begin(); it != limit; ++it) { |
| 1002 | const mirror::Object* obj = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1003 | DCHECK(obj != NULL); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1004 | if (LIKELY(bitmap->HasAddress(obj))) { |
| 1005 | bitmap->Set(obj); |
| 1006 | } else { |
| 1007 | large_objects->Set(obj); |
| 1008 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1009 | } |
| 1010 | } |
| 1011 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1012 | void Heap::UnMarkAllocStack(accounting::SpaceBitmap* bitmap, accounting::SpaceSetMap* large_objects, |
| 1013 | accounting::ObjectStack* stack) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1014 | mirror::Object** limit = stack->End(); |
| 1015 | for (mirror::Object** it = stack->Begin(); it != limit; ++it) { |
| 1016 | const mirror::Object* obj = *it; |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1017 | DCHECK(obj != NULL); |
Mathieu Chartier | e0f0cb3 | 2012-08-28 11:26:00 -0700 | [diff] [blame] | 1018 | if (LIKELY(bitmap->HasAddress(obj))) { |
| 1019 | bitmap->Clear(obj); |
| 1020 | } else { |
| 1021 | large_objects->Clear(obj); |
| 1022 | } |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1026 | collector::GcType Heap::CollectGarbageInternal(collector::GcType gc_type, GcCause gc_cause, |
| 1027 | bool clear_soft_references) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1028 | Thread* self = Thread::Current(); |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 1029 | |
| 1030 | switch (gc_cause) { |
| 1031 | case kGcCauseForAlloc: |
| 1032 | ATRACE_BEGIN("GC (alloc)"); |
| 1033 | break; |
| 1034 | case kGcCauseBackground: |
| 1035 | ATRACE_BEGIN("GC (background)"); |
| 1036 | break; |
| 1037 | case kGcCauseExplicit: |
| 1038 | ATRACE_BEGIN("GC (explicit)"); |
| 1039 | break; |
| 1040 | } |
| 1041 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1042 | ScopedThreadStateChange tsc(self, kWaitingPerformingGc); |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1043 | Locks::mutator_lock_->AssertNotHeld(self); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 1044 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1045 | if (self->IsHandlingStackOverflow()) { |
| 1046 | LOG(WARNING) << "Performing GC on a thread that is handling a stack overflow."; |
| 1047 | } |
| 1048 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1049 | // Ensure there is only one GC at a time. |
| 1050 | bool start_collect = false; |
| 1051 | while (!start_collect) { |
| 1052 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1053 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1054 | if (!is_gc_running_) { |
| 1055 | is_gc_running_ = true; |
| 1056 | start_collect = true; |
| 1057 | } |
| 1058 | } |
| 1059 | if (!start_collect) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1060 | WaitForConcurrentGcToComplete(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1061 | // TODO: if another thread beat this one to do the GC, perhaps we should just return here? |
| 1062 | // Not doing at the moment to ensure soft references are cleared. |
| 1063 | } |
| 1064 | } |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1065 | gc_complete_lock_->AssertNotHeld(self); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1066 | |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1067 | if (gc_cause == kGcCauseForAlloc && Runtime::Current()->HasStatsEnabled()) { |
| 1068 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 1069 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 1070 | } |
| 1071 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1072 | uint64_t gc_start_time_ns = NanoTime(); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1073 | uint64_t gc_start_size = GetBytesAllocated(); |
| 1074 | // Approximate allocation rate in bytes / second. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1075 | if (UNLIKELY(gc_start_time_ns == last_gc_time_ns_)) { |
Jeff Hao | 9bd0281 | 2013-02-08 14:29:50 -0800 | [diff] [blame] | 1076 | LOG(WARNING) << "Timers are broken (gc_start_time == last_gc_time_)."; |
| 1077 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1078 | uint64_t ms_delta = NsToMs(gc_start_time_ns - last_gc_time_ns_); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1079 | if (ms_delta != 0) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1080 | allocation_rate_ = ((gc_start_size - last_gc_size_) * 1000) / ms_delta; |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1081 | VLOG(heap) << "Allocation rate: " << PrettySize(allocation_rate_) << "/s"; |
| 1082 | } |
| 1083 | |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 1084 | if (gc_type == collector::kGcTypeSticky && |
| 1085 | alloc_space_->Size() < min_alloc_space_size_for_sticky_gc_) { |
| 1086 | gc_type = collector::kGcTypePartial; |
| 1087 | } |
| 1088 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1089 | DCHECK_LT(gc_type, collector::kGcTypeMax); |
| 1090 | DCHECK_NE(gc_type, collector::kGcTypeNone); |
| 1091 | collector::MarkSweep* collector = NULL; |
| 1092 | typedef std::vector<collector::MarkSweep*>::iterator It; |
| 1093 | for (It it = mark_sweep_collectors_.begin(), end = mark_sweep_collectors_.end(); |
| 1094 | it != end; ++it) { |
| 1095 | collector::MarkSweep* cur_collector = *it; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1096 | if (cur_collector->IsConcurrent() == concurrent_gc_ && cur_collector->GetGcType() == gc_type) { |
| 1097 | collector = cur_collector; |
| 1098 | break; |
| 1099 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1100 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1101 | CHECK(collector != NULL) |
| 1102 | << "Could not find garbage collector with concurrent=" << concurrent_gc_ |
| 1103 | << " and type=" << gc_type; |
| 1104 | collector->clear_soft_references_ = clear_soft_references; |
| 1105 | collector->Run(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1106 | total_objects_freed_ever_ += collector->GetFreedObjects(); |
| 1107 | total_bytes_freed_ever_ += collector->GetFreedBytes(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1108 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1109 | const size_t duration = collector->GetDurationNs(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1110 | std::vector<uint64_t> pauses = collector->GetPauseTimes(); |
| 1111 | bool was_slow = duration > kSlowGcThreshold || |
| 1112 | (gc_cause == kGcCauseForAlloc && duration > kLongGcPauseThreshold); |
| 1113 | for (size_t i = 0; i < pauses.size(); ++i) { |
| 1114 | if (pauses[i] > kLongGcPauseThreshold) { |
| 1115 | was_slow = true; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | if (was_slow) { |
| 1120 | const size_t percent_free = GetPercentFree(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1121 | const size_t current_heap_size = GetBytesAllocated(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1122 | const size_t total_memory = GetTotalMemory(); |
| 1123 | std::ostringstream pause_string; |
| 1124 | for (size_t i = 0; i < pauses.size(); ++i) { |
| 1125 | pause_string << PrettyDuration((pauses[i] / 1000) * 1000) |
| 1126 | << ((i != pauses.size() - 1) ? ", " : ""); |
| 1127 | } |
| 1128 | LOG(INFO) << gc_cause << " " << collector->GetName() |
Sameer Abu Asal | a843954 | 2013-02-14 16:06:42 -0800 | [diff] [blame] | 1129 | << "GC freed " << PrettySize(collector->GetFreedBytes()) << ", " |
| 1130 | << percent_free << "% free, " << PrettySize(current_heap_size) << "/" |
| 1131 | << PrettySize(total_memory) << ", " << "paused " << pause_string.str() |
| 1132 | << " total " << PrettyDuration((duration / 1000) * 1000); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1133 | if (VLOG_IS_ON(heap)) { |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame^] | 1134 | LOG(INFO) << Dumpable<base::TimingLogger>(collector->GetTimings()); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1135 | } |
| 1136 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1137 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1138 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1139 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1140 | is_gc_running_ = false; |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1141 | last_gc_type_ = gc_type; |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1142 | // Wake anyone who may have been waiting for the GC to complete. |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1143 | gc_complete_cond_->Broadcast(self); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1144 | } |
Mathieu Chartier | 0a9dc05 | 2013-07-25 11:01:28 -0700 | [diff] [blame] | 1145 | |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1146 | // Inform DDMS that a GC completed. |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 1147 | ATRACE_END(); |
Ian Rogers | 15bf2d3 | 2012-08-28 17:33:04 -0700 | [diff] [blame] | 1148 | Dbg::GcDidFinish(); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1149 | return gc_type; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1150 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1151 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame^] | 1152 | void Heap::UpdateAndMarkModUnion(collector::MarkSweep* mark_sweep, base::TimingLogger& timings, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1153 | collector::GcType gc_type) { |
| 1154 | if (gc_type == collector::kGcTypeSticky) { |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1155 | // Don't need to do anything for mod union table in this case since we are only scanning dirty |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1156 | // cards. |
| 1157 | return; |
| 1158 | } |
| 1159 | |
| 1160 | // Update zygote mod union table. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1161 | if (gc_type == collector::kGcTypePartial) { |
| 1162 | timings.NewSplit("UpdateZygoteModUnionTable"); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1163 | zygote_mod_union_table_->Update(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1164 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1165 | timings.NewSplit("ZygoteMarkReferences"); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1166 | zygote_mod_union_table_->MarkReferences(mark_sweep); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | // Processes the cards we cleared earlier and adds their objects into the mod-union table. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1170 | timings.NewSplit("UpdateModUnionTable"); |
| 1171 | image_mod_union_table_->Update(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1172 | |
| 1173 | // Scans all objects in the mod-union table. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1174 | timings.NewSplit("MarkImageToAllocSpaceReferences"); |
| 1175 | image_mod_union_table_->MarkReferences(mark_sweep); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1178 | static void RootMatchesObjectVisitor(const mirror::Object* root, void* arg) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1179 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(arg); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1180 | if (root == obj) { |
| 1181 | LOG(INFO) << "Object " << obj << " is a root"; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | class ScanVisitor { |
| 1186 | public: |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1187 | void operator()(const mirror::Object* obj) const { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1188 | LOG(INFO) << "Would have rescanned object " << obj; |
| 1189 | } |
| 1190 | }; |
| 1191 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1192 | // Verify a reference from an object. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1193 | class VerifyReferenceVisitor { |
| 1194 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1195 | explicit VerifyReferenceVisitor(Heap* heap) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1196 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1197 | : heap_(heap), failed_(false) {} |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1198 | |
| 1199 | bool Failed() const { |
| 1200 | return failed_; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for smarter |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1204 | // analysis on visitors. |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1205 | void operator()(const mirror::Object* obj, const mirror::Object* ref, |
| 1206 | const MemberOffset& offset, bool /* is_static */) const |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1207 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1208 | // Verify that the reference is live. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1209 | if (UNLIKELY(ref != NULL && !IsLive(ref))) { |
| 1210 | accounting::CardTable* card_table = heap_->GetCardTable(); |
| 1211 | accounting::ObjectStack* alloc_stack = heap_->allocation_stack_.get(); |
| 1212 | accounting::ObjectStack* live_stack = heap_->live_stack_.get(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1213 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1214 | if (obj != NULL) { |
| 1215 | byte* card_addr = card_table->CardFromAddr(obj); |
| 1216 | LOG(ERROR) << "Object " << obj << " references dead object " << ref << " at offset " << offset |
| 1217 | << "\nIsDirty = " << (*card_addr == accounting::CardTable::kCardDirty) |
| 1218 | << "\nObj type " << PrettyTypeOf(obj) |
| 1219 | << "\nRef type " << PrettyTypeOf(ref); |
| 1220 | card_table->CheckAddrIsInCardTable(reinterpret_cast<const byte*>(obj)); |
| 1221 | void* cover_begin = card_table->AddrFromCard(card_addr); |
| 1222 | void* cover_end = reinterpret_cast<void*>(reinterpret_cast<size_t>(cover_begin) + |
| 1223 | accounting::CardTable::kCardSize); |
| 1224 | LOG(ERROR) << "Card " << reinterpret_cast<void*>(card_addr) << " covers " << cover_begin |
| 1225 | << "-" << cover_end; |
| 1226 | accounting::SpaceBitmap* bitmap = heap_->GetLiveBitmap()->GetContinuousSpaceBitmap(obj); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1227 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1228 | // Print out how the object is live. |
| 1229 | if (bitmap != NULL && bitmap->Test(obj)) { |
| 1230 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 1231 | } |
| 1232 | if (alloc_stack->Contains(const_cast<mirror::Object*>(obj))) { |
| 1233 | LOG(ERROR) << "Object " << obj << " found in allocation stack"; |
| 1234 | } |
| 1235 | if (live_stack->Contains(const_cast<mirror::Object*>(obj))) { |
| 1236 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
| 1237 | } |
| 1238 | // Attempt to see if the card table missed the reference. |
| 1239 | ScanVisitor scan_visitor; |
| 1240 | byte* byte_cover_begin = reinterpret_cast<byte*>(card_table->AddrFromCard(card_addr)); |
| 1241 | card_table->Scan(bitmap, byte_cover_begin, |
| 1242 | byte_cover_begin + accounting::CardTable::kCardSize, |
| 1243 | scan_visitor, VoidFunctor()); |
| 1244 | |
| 1245 | // Search to see if any of the roots reference our object. |
| 1246 | void* arg = const_cast<void*>(reinterpret_cast<const void*>(obj)); |
| 1247 | Runtime::Current()->VisitRoots(&RootMatchesObjectVisitor, arg, false, false); |
| 1248 | |
| 1249 | // Search to see if any of the roots reference our reference. |
| 1250 | arg = const_cast<void*>(reinterpret_cast<const void*>(ref)); |
| 1251 | Runtime::Current()->VisitRoots(&RootMatchesObjectVisitor, arg, false, false); |
| 1252 | } else { |
| 1253 | LOG(ERROR) << "Root references dead object " << ref << "\nRef type " << PrettyTypeOf(ref); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1254 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1255 | if (alloc_stack->Contains(const_cast<mirror::Object*>(ref))) { |
| 1256 | LOG(ERROR) << "Reference " << ref << " found in allocation stack!"; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1257 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1258 | if (live_stack->Contains(const_cast<mirror::Object*>(ref))) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1259 | LOG(ERROR) << "Reference " << ref << " found in live stack!"; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1260 | } |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1261 | heap_->image_mod_union_table_->Dump(LOG(ERROR) << "Image mod-union table: "); |
| 1262 | heap_->zygote_mod_union_table_->Dump(LOG(ERROR) << "Zygote mod-union table: "); |
| 1263 | failed_ = true; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1264 | } |
| 1265 | } |
| 1266 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1267 | bool IsLive(const mirror::Object* obj) const NO_THREAD_SAFETY_ANALYSIS { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1268 | return heap_->IsLiveObjectLocked(obj); |
| 1269 | } |
| 1270 | |
| 1271 | static void VerifyRoots(const mirror::Object* root, void* arg) { |
| 1272 | VerifyReferenceVisitor* visitor = reinterpret_cast<VerifyReferenceVisitor*>(arg); |
| 1273 | (*visitor)(NULL, root, MemberOffset(0), true); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1277 | Heap* const heap_; |
| 1278 | mutable bool failed_; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1279 | }; |
| 1280 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1281 | // Verify all references within an object, for use with HeapBitmap::Visit. |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1282 | class VerifyObjectVisitor { |
| 1283 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1284 | explicit VerifyObjectVisitor(Heap* heap) : heap_(heap), failed_(false) {} |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1285 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1286 | void operator()(const mirror::Object* obj) const |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 1287 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1288 | // Note: we are verifying the references in obj but not obj itself, this is because obj must |
| 1289 | // be live or else how did we find it in the live bitmap? |
| 1290 | VerifyReferenceVisitor visitor(heap_); |
| 1291 | collector::MarkSweep::VisitObjectReferences(obj, visitor); |
| 1292 | failed_ = failed_ || visitor.Failed(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | bool Failed() const { |
| 1296 | return failed_; |
| 1297 | } |
| 1298 | |
| 1299 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1300 | Heap* const heap_; |
| 1301 | mutable bool failed_; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1302 | }; |
| 1303 | |
| 1304 | // Must do this with mutators suspended since we are directly accessing the allocation stacks. |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1305 | bool Heap::VerifyHeapReferences() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1306 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1307 | // Lets sort our allocation stacks so that we can efficiently binary search them. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1308 | allocation_stack_->Sort(); |
| 1309 | live_stack_->Sort(); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1310 | // Perform the verification. |
| 1311 | VerifyObjectVisitor visitor(this); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1312 | Runtime::Current()->VisitRoots(VerifyReferenceVisitor::VerifyRoots, &visitor, false, false); |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1313 | GetLiveBitmap()->Visit(visitor); |
| 1314 | // We don't want to verify the objects in the allocation stack since they themselves may be |
| 1315 | // pointing to dead objects if they are not reachable. |
| 1316 | if (visitor.Failed()) { |
| 1317 | DumpSpaces(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1318 | return false; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1319 | } |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1320 | return true; |
| 1321 | } |
| 1322 | |
| 1323 | class VerifyReferenceCardVisitor { |
| 1324 | public: |
| 1325 | VerifyReferenceCardVisitor(Heap* heap, bool* failed) |
| 1326 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, |
| 1327 | Locks::heap_bitmap_lock_) |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1328 | : heap_(heap), failed_(failed) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1331 | // TODO: Fix lock analysis to not use NO_THREAD_SAFETY_ANALYSIS, requires support for |
| 1332 | // annotalysis on visitors. |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1333 | void operator()(const mirror::Object* obj, const mirror::Object* ref, const MemberOffset& offset, |
| 1334 | bool is_static) const NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1335 | // Filter out class references since changing an object's class does not mark the card as dirty. |
| 1336 | // Also handles large objects, since the only reference they hold is a class reference. |
| 1337 | if (ref != NULL && !ref->IsClass()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1338 | accounting::CardTable* card_table = heap_->GetCardTable(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1339 | // If the object is not dirty and it is referencing something in the live stack other than |
| 1340 | // class, then it must be on a dirty card. |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1341 | if (!card_table->AddrIsInCardTable(obj)) { |
| 1342 | LOG(ERROR) << "Object " << obj << " is not in the address range of the card table"; |
| 1343 | *failed_ = true; |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1344 | } else if (!card_table->IsDirty(obj)) { |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1345 | // Card should be either kCardDirty if it got re-dirtied after we aged it, or |
| 1346 | // kCardDirty - 1 if it didnt get touched since we aged it. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1347 | accounting::ObjectStack* live_stack = heap_->live_stack_.get(); |
| 1348 | if (live_stack->Contains(const_cast<mirror::Object*>(ref))) { |
| 1349 | if (live_stack->Contains(const_cast<mirror::Object*>(obj))) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1350 | LOG(ERROR) << "Object " << obj << " found in live stack"; |
| 1351 | } |
| 1352 | if (heap_->GetLiveBitmap()->Test(obj)) { |
| 1353 | LOG(ERROR) << "Object " << obj << " found in live bitmap"; |
| 1354 | } |
| 1355 | LOG(ERROR) << "Object " << obj << " " << PrettyTypeOf(obj) |
| 1356 | << " references " << ref << " " << PrettyTypeOf(ref) << " in live stack"; |
| 1357 | |
| 1358 | // Print which field of the object is dead. |
| 1359 | if (!obj->IsObjectArray()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1360 | const mirror::Class* klass = is_static ? obj->AsClass() : obj->GetClass(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1361 | CHECK(klass != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1362 | const mirror::ObjectArray<mirror::Field>* fields = is_static ? klass->GetSFields() |
| 1363 | : klass->GetIFields(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1364 | CHECK(fields != NULL); |
| 1365 | for (int32_t i = 0; i < fields->GetLength(); ++i) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1366 | const mirror::Field* cur = fields->Get(i); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1367 | if (cur->GetOffset().Int32Value() == offset.Int32Value()) { |
| 1368 | LOG(ERROR) << (is_static ? "Static " : "") << "field in the live stack is " |
| 1369 | << PrettyField(cur); |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1374 | const mirror::ObjectArray<mirror::Object>* object_array = |
| 1375 | obj->AsObjectArray<mirror::Object>(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1376 | for (int32_t i = 0; i < object_array->GetLength(); ++i) { |
| 1377 | if (object_array->Get(i) == ref) { |
| 1378 | LOG(ERROR) << (is_static ? "Static " : "") << "obj[" << i << "] = ref"; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | *failed_ = true; |
| 1384 | } |
| 1385 | } |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1390 | Heap* const heap_; |
| 1391 | bool* const failed_; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1392 | }; |
| 1393 | |
| 1394 | class VerifyLiveStackReferences { |
| 1395 | public: |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1396 | explicit VerifyLiveStackReferences(Heap* heap) |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1397 | : heap_(heap), |
Brian Carlstrom | 93ba893 | 2013-07-17 21:31:49 -0700 | [diff] [blame] | 1398 | failed_(false) {} |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1399 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 1400 | void operator()(const mirror::Object* obj) const |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1401 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1402 | VerifyReferenceCardVisitor visitor(heap_, const_cast<bool*>(&failed_)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1403 | collector::MarkSweep::VisitObjectReferences(obj, visitor); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | bool Failed() const { |
| 1407 | return failed_; |
| 1408 | } |
| 1409 | |
| 1410 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1411 | Heap* const heap_; |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1412 | bool failed_; |
| 1413 | }; |
| 1414 | |
| 1415 | bool Heap::VerifyMissingCardMarks() { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1416 | Locks::mutator_lock_->AssertExclusiveHeld(Thread::Current()); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1417 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1418 | // We need to sort the live stack since we binary search it. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1419 | live_stack_->Sort(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1420 | VerifyLiveStackReferences visitor(this); |
| 1421 | GetLiveBitmap()->Visit(visitor); |
| 1422 | |
| 1423 | // We can verify objects in the live stack since none of these should reference dead objects. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1424 | for (mirror::Object** it = live_stack_->Begin(); it != live_stack_->End(); ++it) { |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1425 | visitor(*it); |
| 1426 | } |
| 1427 | |
| 1428 | if (visitor.Failed()) { |
| 1429 | DumpSpaces(); |
| 1430 | return false; |
| 1431 | } |
| 1432 | return true; |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1433 | } |
| 1434 | |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1435 | void Heap::SwapStacks() { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1436 | allocation_stack_.swap(live_stack_); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1437 | |
| 1438 | // Sort the live stack so that we can quickly binary search it later. |
Ian Rogers | 04d7aa9 | 2013-03-16 14:29:17 -0700 | [diff] [blame] | 1439 | if (verify_object_mode_ > kNoHeapVerification) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1440 | live_stack_->Sort(); |
Mathieu Chartier | c7b83a0 | 2012-09-11 18:07:39 -0700 | [diff] [blame] | 1441 | } |
| 1442 | } |
| 1443 | |
Anwar Ghuloum | 6f28d91 | 2013-07-24 15:02:53 -0700 | [diff] [blame^] | 1444 | void Heap::ProcessCards(base::TimingLogger& timings) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1445 | // Clear cards and keep track of cards cleared in the mod-union table. |
| 1446 | typedef std::vector<space::ContinuousSpace*>::iterator It; |
| 1447 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
| 1448 | space::ContinuousSpace* space = *it; |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1449 | if (space->IsImageSpace()) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1450 | timings.NewSplit("ModUnionClearCards"); |
| 1451 | image_mod_union_table_->ClearCards(space); |
| 1452 | } else if (space->IsZygoteSpace()) { |
| 1453 | timings.NewSplit("ZygoteModUnionClearCards"); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1454 | zygote_mod_union_table_->ClearCards(space); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1455 | } else { |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1456 | // No mod union table for the AllocSpace. Age the cards so that the GC knows that these cards |
| 1457 | // were dirty before the GC started. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1458 | timings.NewSplit("AllocSpaceClearCards"); |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1459 | card_table_->ModifyCardsAtomic(space->Begin(), space->End(), AgeCardVisitor(), VoidFunctor()); |
Mathieu Chartier | 7469ebf | 2012-09-24 16:28:36 -0700 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | } |
| 1463 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1464 | void Heap::PreGcVerification(collector::GarbageCollector* gc) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1465 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 1466 | Thread* self = Thread::Current(); |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1467 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1468 | if (verify_pre_gc_heap_) { |
| 1469 | thread_list->SuspendAll(); |
| 1470 | { |
| 1471 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1472 | if (!VerifyHeapReferences()) { |
| 1473 | LOG(FATAL) << "Pre " << gc->GetName() << " heap verification failed"; |
| 1474 | } |
| 1475 | } |
| 1476 | thread_list->ResumeAll(); |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1477 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1478 | |
| 1479 | // Check that all objects which reference things in the live stack are on dirty cards. |
| 1480 | if (verify_missing_card_marks_) { |
| 1481 | thread_list->SuspendAll(); |
| 1482 | { |
| 1483 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1484 | SwapStacks(); |
| 1485 | // Sort the live stack so that we can quickly binary search it later. |
| 1486 | if (!VerifyMissingCardMarks()) { |
| 1487 | LOG(FATAL) << "Pre " << gc->GetName() << " missing card mark verification failed"; |
| 1488 | } |
| 1489 | SwapStacks(); |
| 1490 | } |
| 1491 | thread_list->ResumeAll(); |
| 1492 | } |
| 1493 | |
| 1494 | if (verify_mod_union_table_) { |
| 1495 | thread_list->SuspendAll(); |
| 1496 | ReaderMutexLock reader_lock(self, *Locks::heap_bitmap_lock_); |
| 1497 | zygote_mod_union_table_->Update(); |
| 1498 | zygote_mod_union_table_->Verify(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1499 | image_mod_union_table_->Update(); |
| 1500 | image_mod_union_table_->Verify(); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1501 | thread_list->ResumeAll(); |
| 1502 | } |
Mathieu Chartier | 4da7f2f | 2012-11-13 12:51:01 -0800 | [diff] [blame] | 1503 | } |
| 1504 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1505 | void Heap::PreSweepingGcVerification(collector::GarbageCollector* gc) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1506 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1507 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1508 | // Called before sweeping occurs since we want to make sure we are not going so reclaim any |
| 1509 | // reachable objects. |
| 1510 | if (verify_post_gc_heap_) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1511 | Thread* self = Thread::Current(); |
| 1512 | CHECK_NE(self->GetState(), kRunnable); |
| 1513 | Locks::mutator_lock_->SharedUnlock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1514 | thread_list->SuspendAll(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1515 | { |
| 1516 | WriterMutexLock mu(self, *Locks::heap_bitmap_lock_); |
| 1517 | // Swapping bound bitmaps does nothing. |
| 1518 | gc->SwapBitmaps(); |
| 1519 | if (!VerifyHeapReferences()) { |
| 1520 | LOG(FATAL) << "Post " << gc->GetName() << "GC verification failed"; |
| 1521 | } |
| 1522 | gc->SwapBitmaps(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1523 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1524 | thread_list->ResumeAll(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1525 | Locks::mutator_lock_->SharedLock(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1526 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1527 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1528 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1529 | void Heap::PostGcVerification(collector::GarbageCollector* gc) { |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1530 | Thread* self = Thread::Current(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1531 | |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1532 | if (verify_system_weaks_) { |
| 1533 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1534 | collector::MarkSweep* mark_sweep = down_cast<collector::MarkSweep*>(gc); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1535 | mark_sweep->VerifySystemWeaks(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1536 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1537 | } |
| 1538 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1539 | collector::GcType Heap::WaitForConcurrentGcToComplete(Thread* self) { |
| 1540 | collector::GcType last_gc_type = collector::kGcTypeNone; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1541 | if (concurrent_gc_) { |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 1542 | ATRACE_BEGIN("GC: Wait For Concurrent"); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1543 | bool do_wait; |
| 1544 | uint64_t wait_start = NanoTime(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1545 | { |
| 1546 | // Check if GC is running holding gc_complete_lock_. |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1547 | MutexLock mu(self, *gc_complete_lock_); |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1548 | do_wait = is_gc_running_; |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1549 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1550 | if (do_wait) { |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1551 | uint64_t wait_time; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1552 | // We must wait, change thread state then sleep on gc_complete_cond_; |
| 1553 | ScopedThreadStateChange tsc(Thread::Current(), kWaitingForGcToComplete); |
| 1554 | { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1555 | MutexLock mu(self, *gc_complete_lock_); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1556 | while (is_gc_running_) { |
Ian Rogers | c604d73 | 2012-10-14 16:09:54 -0700 | [diff] [blame] | 1557 | gc_complete_cond_->Wait(self); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1558 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1559 | last_gc_type = last_gc_type_; |
Brian Carlstrom | f69863b | 2013-07-17 21:53:13 -0700 | [diff] [blame] | 1560 | wait_time = NanoTime() - wait_start; |
Mathieu Chartier | 155dfe9 | 2012-10-09 14:24:49 -0700 | [diff] [blame] | 1561 | total_wait_time_ += wait_time; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1562 | } |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1563 | if (wait_time > kLongGcPauseThreshold) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1564 | LOG(INFO) << "WaitForConcurrentGcToComplete blocked for " << PrettyDuration(wait_time); |
| 1565 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1566 | } |
Mathieu Chartier | 752a0e6 | 2013-06-27 11:03:27 -0700 | [diff] [blame] | 1567 | ATRACE_END(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1568 | } |
Mathieu Chartier | 866fb2a | 2012-09-10 10:47:49 -0700 | [diff] [blame] | 1569 | return last_gc_type; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1572 | void Heap::DumpForSigQuit(std::ostream& os) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1573 | os << "Heap: " << GetPercentFree() << "% free, " << PrettySize(GetBytesAllocated()) << "/" |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1574 | << PrettySize(GetTotalMemory()) << "; " << GetObjectsAllocated() << " objects\n"; |
Elliott Hughes | 8b788fe | 2013-04-17 15:57:01 -0700 | [diff] [blame] | 1575 | DumpGcPerformanceInfo(os); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | size_t Heap::GetPercentFree() { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1579 | return static_cast<size_t>(100.0f * static_cast<float>(GetFreeMemory()) / GetTotalMemory()); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1582 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1583 | if (max_allowed_footprint > GetMaxMemory()) { |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1584 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) << " to " |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1585 | << PrettySize(GetMaxMemory()); |
| 1586 | max_allowed_footprint = GetMaxMemory(); |
| 1587 | } |
Mathieu Chartier | 1c23e1e | 2012-10-12 14:14:11 -0700 | [diff] [blame] | 1588 | max_allowed_footprint_ = max_allowed_footprint; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1591 | void Heap::UpdateMaxNativeFootprint() { |
| 1592 | size_t native_size = native_bytes_allocated_; |
| 1593 | // TODO: Tune the native heap utilization to be a value other than the java heap utilization. |
| 1594 | size_t target_size = native_size / GetTargetHeapUtilization(); |
| 1595 | if (target_size > native_size + max_free_) { |
| 1596 | target_size = native_size + max_free_; |
| 1597 | } else if (target_size < native_size + min_free_) { |
| 1598 | target_size = native_size + min_free_; |
| 1599 | } |
| 1600 | native_footprint_gc_watermark_ = target_size; |
| 1601 | native_footprint_limit_ = 2 * target_size - native_size; |
| 1602 | } |
| 1603 | |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 1604 | void Heap::GrowForUtilization(collector::GcType gc_type, uint64_t gc_duration) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1605 | // We know what our utilization is at this moment. |
| 1606 | // This doesn't actually resize any memory. It just lets the heap grow more when necessary. |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1607 | const size_t bytes_allocated = GetBytesAllocated(); |
| 1608 | last_gc_size_ = bytes_allocated; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1609 | last_gc_time_ns_ = NanoTime(); |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1610 | |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 1611 | size_t target_size; |
| 1612 | if (gc_type != collector::kGcTypeSticky) { |
| 1613 | // Grow the heap for non sticky GC. |
| 1614 | target_size = bytes_allocated / GetTargetHeapUtilization(); |
| 1615 | if (target_size > bytes_allocated + max_free_) { |
| 1616 | target_size = bytes_allocated + max_free_; |
| 1617 | } else if (target_size < bytes_allocated + min_free_) { |
| 1618 | target_size = bytes_allocated + min_free_; |
| 1619 | } |
| 1620 | next_gc_type_ = collector::kGcTypeSticky; |
| 1621 | } else { |
| 1622 | // Based on how close the current heap size is to the target size, decide |
| 1623 | // whether or not to do a partial or sticky GC next. |
| 1624 | if (bytes_allocated + min_free_ <= max_allowed_footprint_) { |
| 1625 | next_gc_type_ = collector::kGcTypeSticky; |
| 1626 | } else { |
| 1627 | next_gc_type_ = collector::kGcTypePartial; |
| 1628 | } |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1629 | |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 1630 | // If we have freed enough memory, shrink the heap back down. |
| 1631 | if (bytes_allocated + max_free_ < max_allowed_footprint_) { |
| 1632 | target_size = bytes_allocated + max_free_; |
| 1633 | } else { |
| 1634 | target_size = std::max(bytes_allocated, max_allowed_footprint_); |
| 1635 | } |
| 1636 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 1637 | SetIdealFootprint(target_size); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1638 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1639 | // Calculate when to perform the next ConcurrentGC. |
| 1640 | if (concurrent_gc_) { |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1641 | // Calculate the estimated GC duration. |
| 1642 | double gc_duration_seconds = NsToMs(gc_duration) / 1000.0; |
| 1643 | // Estimate how many remaining bytes we will have when we need to start the next GC. |
| 1644 | size_t remaining_bytes = allocation_rate_ * gc_duration_seconds; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1645 | remaining_bytes = std::max(remaining_bytes, kMinConcurrentRemainingBytes); |
| 1646 | if (UNLIKELY(remaining_bytes > max_allowed_footprint_)) { |
| 1647 | // A never going to happen situation that from the estimated allocation rate we will exceed |
| 1648 | // the applications entire footprint with the given estimated allocation rate. Schedule |
| 1649 | // another GC straight away. |
| 1650 | concurrent_start_bytes_ = bytes_allocated; |
| 1651 | } else { |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1652 | // Start a concurrent GC when we get close to the estimated remaining bytes. When the |
| 1653 | // allocation rate is very high, remaining_bytes could tell us that we should start a GC |
| 1654 | // right away. |
| 1655 | concurrent_start_bytes_ = std::max(max_allowed_footprint_ - remaining_bytes, bytes_allocated); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1656 | } |
| 1657 | DCHECK_LE(concurrent_start_bytes_, max_allowed_footprint_); |
| 1658 | DCHECK_LE(max_allowed_footprint_, growth_limit_); |
| 1659 | } |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1660 | |
| 1661 | UpdateMaxNativeFootprint(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1662 | } |
| 1663 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1664 | void Heap::ClearGrowthLimit() { |
Mathieu Chartier | 80de7a6 | 2012-11-27 17:21:50 -0800 | [diff] [blame] | 1665 | growth_limit_ = capacity_; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 1666 | alloc_space_->ClearGrowthLimit(); |
| 1667 | } |
| 1668 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1669 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
Mathieu Chartier | fd678be | 2012-08-30 14:50:54 -0700 | [diff] [blame] | 1670 | MemberOffset reference_queue_offset, |
| 1671 | MemberOffset reference_queueNext_offset, |
| 1672 | MemberOffset reference_pendingNext_offset, |
| 1673 | MemberOffset finalizer_reference_zombie_offset) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1674 | reference_referent_offset_ = reference_referent_offset; |
| 1675 | reference_queue_offset_ = reference_queue_offset; |
| 1676 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 1677 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 1678 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 1679 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1680 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 1681 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 1682 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 1683 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 1684 | } |
| 1685 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1686 | mirror::Object* Heap::GetReferenceReferent(mirror::Object* reference) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1687 | DCHECK(reference != NULL); |
| 1688 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1689 | return reference->GetFieldObject<mirror::Object*>(reference_referent_offset_, true); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1690 | } |
| 1691 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1692 | void Heap::ClearReferenceReferent(mirror::Object* reference) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1693 | DCHECK(reference != NULL); |
| 1694 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 1695 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 1696 | } |
| 1697 | |
| 1698 | // Returns true if the reference object has not yet been enqueued. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1699 | bool Heap::IsEnqueuable(const mirror::Object* ref) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1700 | DCHECK(ref != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1701 | const mirror::Object* queue = |
| 1702 | ref->GetFieldObject<mirror::Object*>(reference_queue_offset_, false); |
| 1703 | const mirror::Object* queue_next = |
| 1704 | ref->GetFieldObject<mirror::Object*>(reference_queueNext_offset_, false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1705 | return (queue != NULL) && (queue_next == NULL); |
| 1706 | } |
| 1707 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1708 | void Heap::EnqueueReference(mirror::Object* ref, mirror::Object** cleared_reference_list) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1709 | DCHECK(ref != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1710 | CHECK(ref->GetFieldObject<mirror::Object*>(reference_queue_offset_, false) != NULL); |
| 1711 | CHECK(ref->GetFieldObject<mirror::Object*>(reference_queueNext_offset_, false) == NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1712 | EnqueuePendingReference(ref, cleared_reference_list); |
| 1713 | } |
| 1714 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1715 | void Heap::EnqueuePendingReference(mirror::Object* ref, mirror::Object** list) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1716 | DCHECK(ref != NULL); |
| 1717 | DCHECK(list != NULL); |
| 1718 | |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1719 | // TODO: Remove this lock, use atomic stacks for storing references. |
| 1720 | MutexLock mu(Thread::Current(), *reference_queue_lock_); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1721 | if (*list == NULL) { |
| 1722 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 1723 | *list = ref; |
| 1724 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1725 | mirror::Object* head = |
| 1726 | (*list)->GetFieldObject<mirror::Object*>(reference_pendingNext_offset_, false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1727 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 1728 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 1729 | } |
| 1730 | } |
| 1731 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1732 | mirror::Object* Heap::DequeuePendingReference(mirror::Object** list) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1733 | DCHECK(list != NULL); |
| 1734 | DCHECK(*list != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1735 | mirror::Object* head = (*list)->GetFieldObject<mirror::Object*>(reference_pendingNext_offset_, |
| 1736 | false); |
| 1737 | mirror::Object* ref; |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1738 | |
Mathieu Chartier | d22d548 | 2012-11-06 17:14:12 -0800 | [diff] [blame] | 1739 | // Note: the following code is thread-safe because it is only called from ProcessReferences which |
| 1740 | // is single threaded. |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1741 | if (*list == head) { |
| 1742 | ref = *list; |
| 1743 | *list = NULL; |
| 1744 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1745 | mirror::Object* next = head->GetFieldObject<mirror::Object*>(reference_pendingNext_offset_, |
| 1746 | false); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1747 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 1748 | ref = head; |
| 1749 | } |
| 1750 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 1751 | return ref; |
| 1752 | } |
| 1753 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1754 | void Heap::AddFinalizerReference(Thread* self, mirror::Object* object) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1755 | ScopedObjectAccess soa(self); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 1756 | JValue result; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 1757 | ArgArray arg_array(NULL, 0); |
| 1758 | arg_array.Append(reinterpret_cast<uint32_t>(object)); |
| 1759 | soa.DecodeMethod(WellKnownClasses::java_lang_ref_FinalizerReference_add)->Invoke(self, |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 1760 | arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'V'); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1761 | } |
| 1762 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 1763 | void Heap::EnqueueClearedReferences(mirror::Object** cleared) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1764 | DCHECK(cleared != NULL); |
| 1765 | if (*cleared != NULL) { |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 1766 | // When a runtime isn't started there are no reference queues to care about so ignore. |
| 1767 | if (LIKELY(Runtime::Current()->IsStarted())) { |
| 1768 | ScopedObjectAccess soa(Thread::Current()); |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 1769 | JValue result; |
Jeff Hao | 5d91730 | 2013-02-27 17:57:33 -0800 | [diff] [blame] | 1770 | ArgArray arg_array(NULL, 0); |
| 1771 | arg_array.Append(reinterpret_cast<uint32_t>(*cleared)); |
| 1772 | soa.DecodeMethod(WellKnownClasses::java_lang_ref_ReferenceQueue_add)->Invoke(soa.Self(), |
Jeff Hao | 6474d19 | 2013-03-26 14:08:09 -0700 | [diff] [blame] | 1773 | arg_array.GetArray(), arg_array.GetNumBytes(), &result, 'V'); |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 1774 | } |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1775 | *cleared = NULL; |
| 1776 | } |
| 1777 | } |
| 1778 | |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 1779 | void Heap::RequestConcurrentGC(Thread* self) { |
Mathieu Chartier | 069387a | 2012-06-18 12:01:01 -0700 | [diff] [blame] | 1780 | // Make sure that we can do a concurrent GC. |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1781 | Runtime* runtime = Runtime::Current(); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1782 | DCHECK(concurrent_gc_); |
Mathieu Chartier | 2b82db4 | 2012-11-14 17:29:05 -0800 | [diff] [blame] | 1783 | if (runtime == NULL || !runtime->IsFinishedStarting() || |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1784 | !runtime->IsConcurrentGcEnabled()) { |
| 1785 | return; |
| 1786 | } |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1787 | { |
| 1788 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 1789 | if (runtime->IsShuttingDown()) { |
| 1790 | return; |
| 1791 | } |
| 1792 | } |
| 1793 | if (self->IsHandlingStackOverflow()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1794 | return; |
| 1795 | } |
| 1796 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1797 | // We already have a request pending, no reason to start more until we update |
| 1798 | // concurrent_start_bytes_. |
| 1799 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
| 1800 | |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1801 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1802 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1803 | DCHECK(WellKnownClasses::java_lang_Daemons_requestGC != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1804 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, |
| 1805 | WellKnownClasses::java_lang_Daemons_requestGC); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1806 | CHECK(!env->ExceptionCheck()); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 1809 | void Heap::ConcurrentGC(Thread* self) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1810 | { |
| 1811 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1812 | if (Runtime::Current()->IsShuttingDown()) { |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1813 | return; |
| 1814 | } |
Mathieu Chartier | 2542d66 | 2012-06-21 17:14:11 -0700 | [diff] [blame] | 1815 | } |
Mathieu Chartier | 357e9be | 2012-08-01 11:00:14 -0700 | [diff] [blame] | 1816 | |
Mathieu Chartier | 65db880 | 2012-11-20 12:36:46 -0800 | [diff] [blame] | 1817 | // Wait for any GCs currently running to finish. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1818 | if (WaitForConcurrentGcToComplete(self) == collector::kGcTypeNone) { |
Mathieu Chartier | bdd0fb9 | 2013-07-02 10:16:15 -0700 | [diff] [blame] | 1819 | CollectGarbageInternal(next_gc_type_, kGcCauseBackground, false); |
Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame] | 1820 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1821 | } |
| 1822 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1823 | void Heap::RequestHeapTrim() { |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 1824 | // GC completed and now we must decide whether to request a heap trim (advising pages back to the |
| 1825 | // kernel) or not. Issuing a request will also cause trimming of the libc heap. As a trim scans |
| 1826 | // a space it will hold its lock and can become a cause of jank. |
| 1827 | // Note, the large object space self trims and the Zygote space was trimmed and unchanging since |
| 1828 | // forking. |
| 1829 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1830 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 1831 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 1832 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 1833 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 1834 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 1835 | // not how much use we're making of those pages. |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 1836 | uint64_t ms_time = MilliTime(); |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1837 | float utilization = |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1838 | static_cast<float>(alloc_space_->GetBytesAllocated()) / alloc_space_->Size(); |
| 1839 | if ((utilization > 0.75f) || ((ms_time - last_trim_time_ms_) < 2 * 1000)) { |
Mathieu Chartier | 2fde533 | 2012-09-14 14:51:54 -0700 | [diff] [blame] | 1840 | // Don't bother trimming the alloc space if it's more than 75% utilized, or if a |
| 1841 | // heap trim occurred in the last two seconds. |
| 1842 | return; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1843 | } |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1844 | |
| 1845 | Thread* self = Thread::Current(); |
| 1846 | { |
| 1847 | MutexLock mu(self, *Locks::runtime_shutdown_lock_); |
| 1848 | Runtime* runtime = Runtime::Current(); |
| 1849 | if (runtime == NULL || !runtime->IsFinishedStarting() || runtime->IsShuttingDown()) { |
| 1850 | // Heap trimming isn't supported without a Java runtime or Daemons (such as at dex2oat time) |
| 1851 | // Also: we do not wish to start a heap trim if the runtime is shutting down (a racy check |
| 1852 | // as we don't hold the lock while requesting the trim). |
| 1853 | return; |
| 1854 | } |
Ian Rogers | e1d490c | 2012-02-03 09:09:07 -0800 | [diff] [blame] | 1855 | } |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 1856 | |
| 1857 | SchedPolicy policy; |
| 1858 | get_sched_policy(self->GetTid(), &policy); |
| 1859 | if (policy == SP_FOREGROUND || policy == SP_AUDIO_APP) { |
| 1860 | // Don't trim the heap if we are a foreground or audio app. |
| 1861 | return; |
| 1862 | } |
| 1863 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1864 | last_trim_time_ms_ = ms_time; |
Ian Rogers | 120f1c7 | 2012-09-28 17:17:10 -0700 | [diff] [blame] | 1865 | JNIEnv* env = self->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1866 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1867 | DCHECK(WellKnownClasses::java_lang_Daemons_requestHeapTrim != NULL); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1868 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, |
| 1869 | WellKnownClasses::java_lang_Daemons_requestHeapTrim); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1870 | CHECK(!env->ExceptionCheck()); |
| 1871 | } |
| 1872 | |
Ian Rogers | 4893188 | 2013-01-22 14:35:16 -0800 | [diff] [blame] | 1873 | size_t Heap::Trim() { |
| 1874 | // Handle a requested heap trim on a thread outside of the main GC thread. |
| 1875 | return alloc_space_->Trim(); |
| 1876 | } |
| 1877 | |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1878 | bool Heap::IsGCRequestPending() const { |
| 1879 | return concurrent_start_bytes_ != std::numeric_limits<size_t>::max(); |
| 1880 | } |
| 1881 | |
| 1882 | void Heap::RegisterNativeAllocation(int bytes) { |
| 1883 | // Total number of native bytes allocated. |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 1884 | native_bytes_allocated_.fetch_add(bytes); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1885 | Thread* self = Thread::Current(); |
| 1886 | if (static_cast<size_t>(native_bytes_allocated_) > native_footprint_gc_watermark_) { |
| 1887 | // The second watermark is higher than the gc watermark. If you hit this it means you are |
| 1888 | // allocating native objects faster than the GC can keep up with. |
| 1889 | if (static_cast<size_t>(native_bytes_allocated_) > native_footprint_limit_) { |
| 1890 | JNIEnv* env = self->GetJniEnv(); |
| 1891 | // Can't do this in WellKnownClasses::Init since System is not properly set up at that |
| 1892 | // point. |
| 1893 | if (WellKnownClasses::java_lang_System_runFinalization == NULL) { |
| 1894 | DCHECK(WellKnownClasses::java_lang_System != NULL); |
| 1895 | WellKnownClasses::java_lang_System_runFinalization = |
| 1896 | CacheMethod(env, WellKnownClasses::java_lang_System, true, "runFinalization", "()V"); |
| 1897 | assert(WellKnownClasses::java_lang_System_runFinalization != NULL); |
| 1898 | } |
| 1899 | if (WaitForConcurrentGcToComplete(self) != collector::kGcTypeNone) { |
| 1900 | // Just finished a GC, attempt to run finalizers. |
| 1901 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_System, |
| 1902 | WellKnownClasses::java_lang_System_runFinalization); |
| 1903 | CHECK(!env->ExceptionCheck()); |
| 1904 | } |
| 1905 | |
| 1906 | // If we still are over the watermark, attempt a GC for alloc and run finalizers. |
| 1907 | if (static_cast<size_t>(native_bytes_allocated_) > native_footprint_limit_) { |
| 1908 | CollectGarbageInternal(collector::kGcTypePartial, kGcCauseForAlloc, false); |
| 1909 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_System, |
| 1910 | WellKnownClasses::java_lang_System_runFinalization); |
| 1911 | CHECK(!env->ExceptionCheck()); |
| 1912 | } |
| 1913 | // We have just run finalizers, update the native watermark since it is very likely that |
| 1914 | // finalizers released native managed allocations. |
| 1915 | UpdateMaxNativeFootprint(); |
| 1916 | } else { |
| 1917 | if (!IsGCRequestPending()) { |
| 1918 | RequestConcurrentGC(self); |
| 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | void Heap::RegisterNativeFree(int bytes) { |
| 1925 | int expected_size, new_size; |
| 1926 | do { |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 1927 | expected_size = native_bytes_allocated_.load(); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1928 | new_size = expected_size - bytes; |
| 1929 | if (new_size < 0) { |
| 1930 | ThrowRuntimeException("attempted to free %d native bytes with only %d native bytes registered as allocated", |
| 1931 | bytes, expected_size); |
| 1932 | break; |
| 1933 | } |
Mathieu Chartier | 4b95e8f | 2013-07-15 16:32:50 -0700 | [diff] [blame] | 1934 | } while (!native_bytes_allocated_.compare_and_swap(expected_size, new_size)); |
Mathieu Chartier | 987ccff | 2013-07-08 11:05:21 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 1937 | int64_t Heap::GetTotalMemory() const { |
| 1938 | int64_t ret = 0; |
| 1939 | typedef std::vector<space::ContinuousSpace*>::const_iterator It; |
| 1940 | for (It it = continuous_spaces_.begin(), end = continuous_spaces_.end(); it != end; ++it) { |
| 1941 | space::ContinuousSpace* space = *it; |
| 1942 | if (space->IsImageSpace()) { |
| 1943 | // Currently don't include the image space. |
| 1944 | } else if (space->IsDlMallocSpace()) { |
| 1945 | // Zygote or alloc space |
| 1946 | ret += space->AsDlMallocSpace()->GetFootprint(); |
| 1947 | } |
| 1948 | } |
| 1949 | typedef std::vector<space::DiscontinuousSpace*>::const_iterator It2; |
| 1950 | for (It2 it = discontinuous_spaces_.begin(), end = discontinuous_spaces_.end(); it != end; ++it) { |
| 1951 | space::DiscontinuousSpace* space = *it; |
| 1952 | if (space->IsLargeObjectSpace()) { |
| 1953 | ret += space->AsLargeObjectSpace()->GetBytesAllocated(); |
| 1954 | } |
| 1955 | } |
| 1956 | return ret; |
| 1957 | } |
| 1958 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 1959 | } // namespace gc |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1960 | } // namespace art |