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