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