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 | |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <sys/wait.h> |
| 21 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 22 | #include <limits> |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 25 | #include "card_table.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 26 | #include "debugger.h" |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 27 | #include "image.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 28 | #include "mark_sweep.h" |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 29 | #include "mod_union_table.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 30 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 31 | #include "object_utils.h" |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 32 | #include "os.h" |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 33 | #include "scoped_heap_lock.h" |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 34 | #include "scoped_jni_thread_state.h" |
Mathieu Chartier | 06f7987 | 2012-06-21 13:51:52 -0700 | [diff] [blame] | 35 | #include "scoped_thread_list_lock_releaser.h" |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 36 | #include "ScopedLocalRef.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 37 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 38 | #include "stl_util.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 39 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 40 | #include "timing_logger.h" |
| 41 | #include "UniquePtr.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 42 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 43 | |
| 44 | namespace art { |
| 45 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 46 | static void UpdateFirstAndLastSpace(Space** first_space, Space** last_space, Space* space) { |
| 47 | if (*first_space == NULL) { |
| 48 | *first_space = space; |
| 49 | *last_space = space; |
| 50 | } else { |
| 51 | if ((*first_space)->Begin() > space->Begin()) { |
| 52 | *first_space = space; |
| 53 | } else if (space->Begin() > (*last_space)->Begin()) { |
| 54 | *last_space = space; |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 59 | static bool GenerateImage(const std::string& image_file_name) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 60 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 61 | std::vector<std::string> boot_class_path; |
| 62 | Split(boot_class_path_string, ':', boot_class_path); |
Brian Carlstrom | b279337 | 2012-03-17 18:27:16 -0700 | [diff] [blame] | 63 | if (boot_class_path.empty()) { |
| 64 | LOG(FATAL) << "Failed to generate image because no boot class path specified"; |
| 65 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 66 | |
| 67 | std::vector<char*> arg_vector; |
| 68 | |
| 69 | std::string dex2oat_string(GetAndroidRoot()); |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 70 | dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 71 | const char* dex2oat = dex2oat_string.c_str(); |
| 72 | arg_vector.push_back(strdup(dex2oat)); |
| 73 | |
| 74 | std::string image_option_string("--image="); |
| 75 | image_option_string += image_file_name; |
| 76 | const char* image_option = image_option_string.c_str(); |
| 77 | arg_vector.push_back(strdup(image_option)); |
| 78 | |
| 79 | arg_vector.push_back(strdup("--runtime-arg")); |
| 80 | arg_vector.push_back(strdup("-Xms64m")); |
| 81 | |
| 82 | arg_vector.push_back(strdup("--runtime-arg")); |
| 83 | arg_vector.push_back(strdup("-Xmx64m")); |
| 84 | |
| 85 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 86 | std::string dex_file_option_string("--dex-file="); |
| 87 | dex_file_option_string += boot_class_path[i]; |
| 88 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 89 | arg_vector.push_back(strdup(dex_file_option)); |
| 90 | } |
| 91 | |
| 92 | std::string oat_file_option_string("--oat-file="); |
| 93 | oat_file_option_string += image_file_name; |
| 94 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 95 | oat_file_option_string += "oat"; |
| 96 | const char* oat_file_option = oat_file_option_string.c_str(); |
| 97 | arg_vector.push_back(strdup(oat_file_option)); |
| 98 | |
| 99 | arg_vector.push_back(strdup("--base=0x60000000")); |
| 100 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 101 | std::string command_line(Join(arg_vector, ' ')); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 102 | LOG(INFO) << command_line; |
| 103 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 104 | arg_vector.push_back(NULL); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 105 | char** argv = &arg_vector[0]; |
| 106 | |
| 107 | // fork and exec dex2oat |
| 108 | pid_t pid = fork(); |
| 109 | if (pid == 0) { |
| 110 | // no allocation allowed between fork and exec |
| 111 | |
| 112 | // change process groups, so we don't get reaped by ProcessManager |
| 113 | setpgid(0, 0); |
| 114 | |
| 115 | execv(dex2oat, argv); |
| 116 | |
| 117 | PLOG(FATAL) << "execv(" << dex2oat << ") failed"; |
| 118 | return false; |
| 119 | } else { |
| 120 | STLDeleteElements(&arg_vector); |
| 121 | |
| 122 | // wait for dex2oat to finish |
| 123 | int status; |
| 124 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 125 | if (got_pid != pid) { |
| 126 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 127 | return false; |
| 128 | } |
| 129 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 130 | LOG(ERROR) << dex2oat << " failed: " << command_line; |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 137 | Heap::Heap(size_t initial_size, size_t growth_limit, size_t capacity, |
| 138 | const std::string& original_image_file_name) |
| 139 | : lock_(NULL), |
| 140 | alloc_space_(NULL), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 141 | card_table_(NULL), |
| 142 | card_marking_disabled_(false), |
| 143 | is_gc_running_(false), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 144 | concurrent_start_size_(128 * KB), |
| 145 | concurrent_min_free_(256 * KB), |
| 146 | try_running_gc_(false), |
| 147 | requesting_gc_(false), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 148 | num_bytes_allocated_(0), |
| 149 | num_objects_allocated_(0), |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 150 | last_trim_time_(0), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 151 | reference_referent_offset_(0), |
| 152 | reference_queue_offset_(0), |
| 153 | reference_queueNext_offset_(0), |
| 154 | reference_pendingNext_offset_(0), |
| 155 | finalizer_reference_zombie_offset_(0), |
| 156 | target_utilization_(0.5), |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 157 | verify_objects_(false) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 158 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 159 | LOG(INFO) << "Heap() entering"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 162 | // Compute the bounds of all spaces for allocating live and mark bitmaps |
| 163 | // there will be at least one space (the alloc space) |
| 164 | Space* first_space = NULL; |
| 165 | Space* last_space = NULL; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 166 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 167 | live_bitmap_.reset(new HeapBitmap); |
| 168 | mark_bitmap_.reset(new HeapBitmap); |
| 169 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 170 | // Requested begin for the alloc space, to follow the mapped image and oat files |
| 171 | byte* requested_begin = NULL; |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 172 | std::string image_file_name(original_image_file_name); |
| 173 | if (!image_file_name.empty()) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 174 | Space* image_space = NULL; |
| 175 | |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 176 | if (OS::FileExists(image_file_name.c_str())) { |
| 177 | // If the /system file exists, it should be up-to-date, don't try to generate |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 178 | image_space = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 179 | } else { |
| 180 | // If the /system file didn't exist, we need to use one from the art-cache. |
| 181 | // If the cache file exists, try to open, but if it fails, regenerate. |
| 182 | // If it does not exist, generate. |
| 183 | image_file_name = GetArtCacheFilenameOrDie(image_file_name); |
| 184 | if (OS::FileExists(image_file_name.c_str())) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 185 | image_space = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 186 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 187 | if (image_space == NULL) { |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 188 | if (!GenerateImage(image_file_name)) { |
| 189 | LOG(FATAL) << "Failed to generate image: " << image_file_name; |
| 190 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 191 | image_space = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 194 | if (image_space == NULL) { |
Brian Carlstrom | 223f20f | 2012-02-04 23:06:55 -0800 | [diff] [blame] | 195 | LOG(FATAL) << "Failed to create space from " << image_file_name; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 196 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 197 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 198 | AddSpace(image_space); |
| 199 | UpdateFirstAndLastSpace(&first_space, &last_space, image_space); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 200 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 201 | // isn't going to get in the middle |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 202 | byte* oat_end_addr = GetImageSpace()->GetImageHeader().GetOatEnd(); |
| 203 | CHECK(oat_end_addr > GetImageSpace()->End()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 204 | if (oat_end_addr > requested_begin) { |
| 205 | requested_begin = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr), |
| 206 | kPageSize)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 207 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 210 | UniquePtr<AllocSpace> alloc_space(Space::CreateAllocSpace( |
| 211 | "alloc space", initial_size, growth_limit, capacity, requested_begin)); |
| 212 | alloc_space_ = alloc_space.release(); |
| 213 | AddSpace(alloc_space_); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 214 | if (alloc_space_ == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 215 | LOG(FATAL) << "Failed to create alloc space"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 216 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 217 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 218 | UpdateFirstAndLastSpace(&first_space, &last_space, alloc_space_); |
| 219 | byte* heap_begin = first_space->Begin(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 220 | size_t heap_capacity = (last_space->Begin() - first_space->Begin()) + last_space->NonGrowthLimitCapacity(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 221 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 222 | // Mark image objects in the live bitmap |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 223 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 224 | Space* space = spaces_[i]; |
| 225 | if (space->IsImageSpace()) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 226 | space->AsImageSpace()->RecordImageAllocations(space->GetLiveBitmap()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 230 | // Allocate the card table. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 231 | UniquePtr<CardTable> card_table(CardTable::Create(heap_begin, heap_capacity)); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 232 | if (card_table.get() == NULL) { |
| 233 | LOG(FATAL) << "Failed to create card table"; |
| 234 | } |
| 235 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 236 | // Allocate the mod-union table |
| 237 | ModUnionTableBitmap* mod_union_table = new ModUnionTableBitmap(this); |
| 238 | mod_union_table->Init(); |
| 239 | mod_union_table_ = mod_union_table; |
| 240 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 241 | card_table_ = card_table.release(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 242 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 243 | num_bytes_allocated_ = 0; |
| 244 | num_objects_allocated_ = 0; |
| 245 | |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 246 | mark_stack_ = MarkStack::Create(); |
| 247 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 248 | // It's still too early to take a lock because there are no threads yet, |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 249 | // but we can create the heap lock now. We don't create it earlier to |
| 250 | // make it clear that you can't use locks during heap initialization. |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 251 | lock_ = new Mutex("Heap lock", kHeapLock); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 252 | condition_ = new ConditionVariable("Heap condition variable"); |
| 253 | |
| 254 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 255 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 256 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 257 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 258 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 261 | void Heap::AddSpace(Space* space) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 262 | DCHECK(space->GetLiveBitmap() != NULL); |
| 263 | live_bitmap_->AddSpaceBitmap(space->GetLiveBitmap()); |
| 264 | |
| 265 | DCHECK(space->GetMarkBitmap() != NULL); |
| 266 | mark_bitmap_->AddSpaceBitmap(space->GetMarkBitmap()); |
| 267 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 268 | spaces_.push_back(space); |
| 269 | } |
| 270 | |
| 271 | Heap::~Heap() { |
| 272 | VLOG(heap) << "~Heap()"; |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 273 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 274 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 275 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 276 | // those threads can't resume. We're the only running thread, and we can do whatever we like... |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 277 | STLDeleteElements(&spaces_); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 278 | delete card_table_; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 279 | delete mod_union_table_; |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 280 | delete mark_stack_; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 281 | delete condition_; |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 282 | delete lock_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 285 | Space* Heap::FindSpaceFromObject(const Object* obj) const { |
| 286 | // TODO: C++0x auto |
| 287 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 288 | if ((*cur)->Contains(obj)) { |
| 289 | return *cur; |
| 290 | } |
| 291 | } |
| 292 | LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!"; |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | ImageSpace* Heap::GetImageSpace() { |
| 297 | // TODO: C++0x auto |
| 298 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 299 | if ((*cur)->IsImageSpace()) { |
| 300 | return (*cur)->AsImageSpace(); |
| 301 | } |
| 302 | } |
| 303 | return NULL; |
| 304 | } |
| 305 | |
| 306 | AllocSpace* Heap::GetAllocSpace() { |
| 307 | return alloc_space_; |
| 308 | } |
| 309 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 310 | static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 311 | size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg); |
| 312 | |
| 313 | size_t chunk_size = static_cast<size_t>(reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start)); |
| 314 | size_t chunk_free_bytes = 0; |
| 315 | if (used_bytes < chunk_size) { |
| 316 | chunk_free_bytes = chunk_size - used_bytes; |
| 317 | } |
| 318 | |
| 319 | if (chunk_free_bytes > max_contiguous_allocation) { |
| 320 | max_contiguous_allocation = chunk_free_bytes; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | Object* Heap::AllocObject(Class* c, size_t byte_count) { |
| 325 | // Used in the detail message if we throw an OOME. |
| 326 | int64_t total_bytes_free; |
| 327 | size_t max_contiguous_allocation; |
| 328 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 329 | { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 330 | ScopedHeapLock heap_lock; |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 331 | DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(Class)) || |
| 332 | (c->IsVariableSize() || c->GetObjectSize() == byte_count) || |
| 333 | strlen(ClassHelper(c).GetDescriptor()) == 0); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 334 | DCHECK_GE(byte_count, sizeof(Object)); |
| 335 | Object* obj = AllocateLocked(byte_count); |
| 336 | if (obj != NULL) { |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 337 | obj->SetClass(c); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 338 | if (Dbg::IsAllocTrackingEnabled()) { |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 339 | Dbg::RecordAllocation(c, byte_count); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 340 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 341 | |
| 342 | if (!is_gc_running_ && num_bytes_allocated_ >= concurrent_start_bytes_) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 343 | // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint. |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 344 | SirtRef<Object> ref(obj); |
| 345 | RequestConcurrentGC(); |
| 346 | } |
| 347 | VerifyObject(obj); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 348 | return obj; |
| 349 | } |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 350 | total_bytes_free = GetFreeMemory(); |
| 351 | max_contiguous_allocation = 0; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 352 | // TODO: C++0x auto |
| 353 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 354 | if ((*cur)->IsAllocSpace()) { |
| 355 | (*cur)->AsAllocSpace()->Walk(MSpaceChunkCallback, &max_contiguous_allocation); |
| 356 | } |
| 357 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 358 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 359 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 360 | std::string msg(StringPrintf("Failed to allocate a %zd-byte %s (%lld total bytes free; largest possible contiguous allocation %zd bytes)", |
| 361 | byte_count, |
| 362 | PrettyDescriptor(c).c_str(), |
| 363 | total_bytes_free, max_contiguous_allocation)); |
| 364 | Thread::Current()->ThrowOutOfMemoryError(msg.c_str()); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 365 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 368 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 369 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 370 | // require taking the lock. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 371 | if (obj == NULL) { |
| 372 | return true; |
| 373 | } |
| 374 | if (!IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 375 | return false; |
| 376 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 377 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 378 | if (spaces_[i]->Contains(obj)) { |
| 379 | return true; |
| 380 | } |
| 381 | } |
| 382 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 385 | bool Heap::IsLiveObjectLocked(const Object* obj) { |
| 386 | lock_->AssertHeld(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 387 | return IsHeapAddress(obj) && GetLiveBitmap()->Test(obj); |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 390 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 391 | void Heap::VerifyObject(const Object* obj) { |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 392 | if (this == NULL || !verify_objects_ || Runtime::Current()->IsShuttingDown() || |
Ian Rogers | 141d622 | 2012-04-05 12:23:06 -0700 | [diff] [blame] | 393 | Thread::Current() == NULL || |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 394 | Runtime::Current()->GetThreadList()->GetLockOwner() == Thread::Current()->GetTid()) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 395 | return; |
| 396 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 397 | ScopedHeapLock heap_lock; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 398 | Heap::VerifyObjectLocked(obj); |
| 399 | } |
| 400 | #endif |
| 401 | |
| 402 | void Heap::VerifyObjectLocked(const Object* obj) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 403 | lock_->AssertHeld(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 404 | if (obj != NULL) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 405 | if (!IsAligned<kObjectAlignment>(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 406 | LOG(FATAL) << "Object isn't aligned: " << obj; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 407 | } else if (!GetLiveBitmap()->Test(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 408 | LOG(FATAL) << "Object is dead: " << obj; |
| 409 | } |
| 410 | // Ignore early dawn of the universe verifications |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 411 | if (num_objects_allocated_ > 10) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 412 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 413 | Object::ClassOffset().Int32Value(); |
| 414 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 415 | if (c == NULL) { |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 416 | LOG(FATAL) << "Null class in object: " << obj; |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 417 | } else if (!IsAligned<kObjectAlignment>(c)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 418 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 419 | } else if (!GetLiveBitmap()->Test(c)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 420 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 421 | } |
| 422 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 423 | // Note: we don't use the accessors here as they have internal sanity checks |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 424 | // that we don't want to run |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 425 | raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 426 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 427 | raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 428 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 429 | CHECK_EQ(c_c, c_c_c); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 434 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 435 | DCHECK(obj != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 436 | reinterpret_cast<Heap*>(arg)->VerifyObjectLocked(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void Heap::VerifyHeap() { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 440 | ScopedHeapLock heap_lock; |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 441 | GetLiveBitmap()->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 444 | void Heap::RecordAllocationLocked(AllocSpace* space, const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 445 | #ifndef NDEBUG |
| 446 | if (Runtime::Current()->IsStarted()) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 447 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 448 | } |
| 449 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 450 | size_t size = space->AllocationSize(obj); |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 451 | DCHECK_GT(size, 0u); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 452 | num_bytes_allocated_ += size; |
| 453 | num_objects_allocated_ += 1; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 454 | |
| 455 | if (Runtime::Current()->HasStatsEnabled()) { |
| 456 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 457 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 458 | ++global_stats->allocated_objects; |
| 459 | ++thread_stats->allocated_objects; |
| 460 | global_stats->allocated_bytes += size; |
| 461 | thread_stats->allocated_bytes += size; |
| 462 | } |
| 463 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 464 | live_bitmap_->Set(obj); |
| 465 | } |
| 466 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 467 | void Heap::RecordFreeLocked(size_t freed_objects, size_t freed_bytes) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 468 | lock_->AssertHeld(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 469 | |
| 470 | if (freed_objects < num_objects_allocated_) { |
| 471 | num_objects_allocated_ -= freed_objects; |
| 472 | } else { |
| 473 | num_objects_allocated_ = 0; |
| 474 | } |
| 475 | if (freed_bytes < num_bytes_allocated_) { |
| 476 | num_bytes_allocated_ -= freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 477 | } else { |
| 478 | num_bytes_allocated_ = 0; |
| 479 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 480 | |
| 481 | if (Runtime::Current()->HasStatsEnabled()) { |
| 482 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 483 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 484 | ++global_stats->freed_objects; |
| 485 | ++thread_stats->freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 486 | global_stats->freed_bytes += freed_bytes; |
| 487 | thread_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 488 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 491 | Object* Heap::AllocateLocked(size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 492 | lock_->AssertHeld(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 493 | |
| 494 | // Try the default alloc space first. |
| 495 | Object* obj = AllocateLocked(alloc_space_, size); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 496 | if (obj != NULL) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 497 | RecordAllocationLocked(alloc_space_, obj); |
| 498 | return obj; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 499 | } |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 500 | |
| 501 | // TODO: C++0x auto |
| 502 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 503 | if ((*cur)->IsAllocSpace() && *cur != alloc_space_) { |
| 504 | AllocSpace* space = (*cur)->AsAllocSpace(); |
| 505 | Object* obj = AllocateLocked(space, size); |
| 506 | if (obj != NULL) { |
| 507 | RecordAllocationLocked(space, obj); |
| 508 | // Switch to this alloc space since the old one did not have enough storage. |
| 509 | alloc_space_ = space; |
| 510 | return obj; |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 517 | Object* Heap::AllocateLocked(AllocSpace* space, size_t alloc_size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 518 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 519 | |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 520 | // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are |
| 521 | // done in the runnable state where suspension is expected. |
| 522 | DCHECK_EQ(Thread::Current()->GetState(), kRunnable); |
| 523 | Thread::Current()->AssertThreadSuspensionIsAllowable(); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 524 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 525 | // Fail impossible allocations |
| 526 | if (alloc_size > space->Capacity()) { |
| 527 | // On failure collect soft references |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 528 | WaitForConcurrentGcToComplete(); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 529 | CollectGarbageInternal(false, true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 530 | return NULL; |
| 531 | } |
| 532 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 533 | Object* ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 534 | if (ptr != NULL) { |
| 535 | return ptr; |
| 536 | } |
| 537 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 538 | // The allocation failed. If the GC is running, block until it completes and retry. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 539 | if (is_gc_running_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 540 | // The GC is concurrently tracing the heap. Release the heap lock, wait for the GC to |
| 541 | // complete, and retrying allocating. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 542 | WaitForConcurrentGcToComplete(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 543 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 544 | if (ptr != NULL) { |
| 545 | return ptr; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // Another failure. Our thread was starved or there may be too many |
| 550 | // live objects. Try a foreground GC. This will have no effect if |
| 551 | // the concurrent GC is already running. |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 552 | if (Runtime::Current()->HasStatsEnabled()) { |
| 553 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 554 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 555 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 556 | // We don't need a WaitForConcurrentGcToComplete here since we checked |
| 557 | // is_gc_running_ earlier and we are in a heap lock. |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 558 | CollectGarbageInternal(false, false); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 559 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 560 | if (ptr != NULL) { |
| 561 | return ptr; |
| 562 | } |
| 563 | |
| 564 | // Even that didn't work; this is an exceptional state. |
| 565 | // Try harder, growing the heap if necessary. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 566 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 567 | if (ptr != NULL) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 568 | size_t new_footprint = space->GetFootprintLimit(); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 569 | // OLD-TODO: may want to grow a little bit more so that the amount of |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 570 | // free space is equal to the old free space + the |
| 571 | // utilization slop for the new allocation. |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 572 | VLOG(gc) << "Grow heap (frag case) to " << PrettySize(new_footprint) |
Ian Rogers | 162a31c | 2012-01-31 16:14:31 -0800 | [diff] [blame] | 573 | << " for a " << PrettySize(alloc_size) << " allocation"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 574 | return ptr; |
| 575 | } |
| 576 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 577 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 578 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 579 | // 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] | 580 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 581 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 582 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) << " allocation"; |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 583 | // We don't need a WaitForConcurrentGcToComplete here either. |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 584 | CollectGarbageInternal(false, true); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 585 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 586 | if (ptr != NULL) { |
| 587 | return ptr; |
| 588 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 589 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 590 | return NULL; |
| 591 | } |
| 592 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 593 | int64_t Heap::GetMaxMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 594 | size_t total = 0; |
| 595 | // TODO: C++0x auto |
| 596 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 597 | if ((*cur)->IsAllocSpace()) { |
| 598 | total += (*cur)->AsAllocSpace()->Capacity(); |
| 599 | } |
| 600 | } |
| 601 | return total; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | int64_t Heap::GetTotalMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 605 | return GetMaxMemory(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | int64_t Heap::GetFreeMemory() { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 609 | return GetMaxMemory() - num_bytes_allocated_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 612 | class InstanceCounter { |
| 613 | public: |
| 614 | InstanceCounter(Class* c, bool count_assignable) |
| 615 | : class_(c), count_assignable_(count_assignable), count_(0) { |
| 616 | } |
| 617 | |
| 618 | size_t GetCount() { |
| 619 | return count_; |
| 620 | } |
| 621 | |
| 622 | static void Callback(Object* o, void* arg) { |
| 623 | reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o); |
| 624 | } |
| 625 | |
| 626 | private: |
| 627 | void VisitInstance(Object* o) { |
| 628 | Class* instance_class = o->GetClass(); |
| 629 | if (count_assignable_) { |
| 630 | if (instance_class == class_) { |
| 631 | ++count_; |
| 632 | } |
| 633 | } else { |
| 634 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
| 635 | ++count_; |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | Class* class_; |
| 641 | bool count_assignable_; |
| 642 | size_t count_; |
| 643 | }; |
| 644 | |
| 645 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 646 | ScopedHeapLock heap_lock; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 647 | InstanceCounter counter(c, count_assignable); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 648 | GetLiveBitmap()->Walk(InstanceCounter::Callback, &counter); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 649 | return counter.GetCount(); |
| 650 | } |
| 651 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 652 | void Heap::CollectGarbage(bool clear_soft_references) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 653 | ScopedHeapLock heap_lock; |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 654 | // If we just waited for a GC to complete then we do not need to do another |
| 655 | // GC unless we clear soft references. |
| 656 | if (!WaitForConcurrentGcToComplete() || clear_soft_references) { |
| 657 | CollectGarbageInternal(false, clear_soft_references); |
| 658 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 661 | void Heap::CollectGarbageInternal(bool concurrent, bool clear_soft_references) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 662 | lock_->AssertHeld(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 663 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 664 | CHECK(!is_gc_running_); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 665 | is_gc_running_ = true; |
| 666 | |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 667 | TimingLogger timings("CollectGarbageInternal"); |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 668 | uint64_t t0 = NanoTime(), root_end = 0, dirty_begin = 0, dirty_end = 0; |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 669 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 670 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 671 | thread_list->SuspendAll(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 672 | timings.AddSplit("SuspendAll"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 673 | |
| 674 | size_t initial_size = num_bytes_allocated_; |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 675 | Object* cleared_references = NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 676 | { |
Mathieu Chartier | 5301cd2 | 2012-05-31 12:11:36 -0700 | [diff] [blame] | 677 | MarkSweep mark_sweep(mark_stack_); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 678 | timings.AddSplit("ctor"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 679 | |
| 680 | mark_sweep.Init(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 681 | timings.AddSplit("Init"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 682 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 683 | if (concurrent) { |
| 684 | card_table_->ClearNonImageSpaceCards(this); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 685 | timings.AddSplit("ClearNonImageSpaceCards"); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 688 | // Clear image space cards and keep track of cards we cleared in the mod-union table. |
| 689 | mod_union_table_->ClearCards(); |
| 690 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 691 | mark_sweep.MarkRoots(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 692 | timings.AddSplit("MarkRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 693 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 694 | // Roots are marked on the bitmap and the mark_stack is empty. |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 695 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 696 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 697 | if (concurrent) { |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 698 | // We need to resume before unlocking or else a thread waiting for the |
| 699 | // heap lock would re-suspend since we have not yet called ResumeAll. |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 700 | thread_list->ResumeAll(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 701 | Unlock(); |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 702 | root_end = NanoTime(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 703 | timings.AddSplit("RootEnd"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 704 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 705 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 706 | // Processes the cards we cleared earlier and adds their objects into the mod-union table. |
| 707 | mod_union_table_->Update(&mark_sweep); |
| 708 | timings.AddSplit("UpdateModUnionBitmap"); |
| 709 | |
| 710 | // Scans all objects in the mod-union table. |
| 711 | mod_union_table_->MarkReferences(&mark_sweep); |
| 712 | timings.AddSplit("ProcessModUnionBitmap"); |
| 713 | |
| 714 | // Recursively mark all the non-image bits set in the mark bitmap. |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 715 | mark_sweep.RecursiveMark(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 716 | timings.AddSplit("RecursiveMark"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 717 | |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 718 | if (concurrent) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 719 | dirty_begin = NanoTime(); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 720 | Lock(); |
| 721 | thread_list->SuspendAll(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 722 | timings.AddSplit("ReSuspend"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 723 | |
| 724 | // Re-mark root set. |
| 725 | mark_sweep.ReMarkRoots(); |
| 726 | timings.AddSplit("ReMarkRoots"); |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 727 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 728 | // Scan dirty objects, this is only required if we are not doing concurrent GC. |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 729 | mark_sweep.RecursiveMarkDirtyObjects(); |
| 730 | timings.AddSplit("RecursiveMarkDirtyObjects"); |
| 731 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 732 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 733 | mark_sweep.ProcessReferences(clear_soft_references); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 734 | timings.AddSplit("ProcessReferences"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 735 | |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame^] | 736 | // Swap the live and mark bitmaps for each alloc space. This is needed since sweep re-swaps |
| 737 | // these bitmaps. Doing this enables us to sweep with the heap unlocked since new allocations |
| 738 | // set the live bit, but since we have the bitmaps reversed at this point, this sets the mark bit |
| 739 | // instead, resulting in no new allocated objects being incorrectly freed by sweep. |
| 740 | for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) { |
| 741 | Space* space = *it; |
| 742 | if (space->IsAllocSpace()) { |
| 743 | live_bitmap_->ReplaceBitmap(space->GetLiveBitmap(), space->GetMarkBitmap()); |
| 744 | mark_bitmap_->ReplaceBitmap(space->GetMarkBitmap(), space->GetLiveBitmap()); |
| 745 | space->AsAllocSpace()->SwapBitmaps(); |
| 746 | } |
| 747 | } |
Mathieu Chartier | 262e5ff | 2012-06-01 17:35:38 -0700 | [diff] [blame] | 748 | |
| 749 | // Verify that we only reach marked objects from the image space |
| 750 | mark_sweep.VerifyImageRoots(); |
| 751 | timings.AddSplit("VerifyImageRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 752 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 753 | if (concurrent) { |
| 754 | thread_list->ResumeAll(); |
| 755 | dirty_end = NanoTime(); |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame^] | 756 | Unlock(); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 757 | } |
| 758 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 759 | mark_sweep.Sweep(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 760 | timings.AddSplit("Sweep"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 761 | |
| 762 | cleared_references = mark_sweep.GetClearedReferences(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Mathieu Chartier | 654d3a2 | 2012-07-11 17:54:18 -0700 | [diff] [blame^] | 765 | if (concurrent) { |
| 766 | // Relock since we unlocked earlier. |
| 767 | // TODO: We probably don't need to have the heap locked for all remainder of the function, except for GrowForUtilization. |
| 768 | Lock(); |
| 769 | } |
| 770 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 771 | GrowForUtilization(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 772 | timings.AddSplit("GrowForUtilization"); |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 773 | |
| 774 | if (!concurrent) { |
| 775 | thread_list->ResumeAll(); |
| 776 | dirty_end = NanoTime(); |
| 777 | } |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 778 | |
| 779 | EnqueueClearedReferences(&cleared_references); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 780 | RequestHeapTrim(); |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 781 | timings.AddSplit("Finish"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 782 | |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 783 | if (VLOG_IS_ON(gc)) { |
| 784 | uint64_t t1 = NanoTime(); |
| 785 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 786 | // TODO: somehow make the specific GC implementation (here MarkSweep) responsible for logging. |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 787 | // Reason: For CMS sometimes initial_size < num_bytes_allocated_ results in overflow (3GB freed message). |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 788 | size_t bytes_freed = initial_size - num_bytes_allocated_; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 789 | uint64_t duration_ns = t1 - t0; |
| 790 | duration_ns -= duration_ns % 1000; |
| 791 | |
| 792 | // If the GC was slow, then print timings in the log. |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 793 | if (concurrent) { |
Elliott Hughes | 24edeb5 | 2012-06-18 15:29:46 -0700 | [diff] [blame] | 794 | uint64_t pause_roots_time = (root_end - t0) / 1000 * 1000; |
| 795 | uint64_t pause_dirty_time = (dirty_end - dirty_begin) / 1000 * 1000; |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 796 | if (pause_roots_time > MsToNs(5) || pause_dirty_time > MsToNs(5)) { |
| 797 | LOG(INFO) << "GC freed " << PrettySize(bytes_freed) << ", " << GetPercentFree() << "% free, " |
| 798 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) << ", " |
| 799 | << "paused " << PrettyDuration(pause_roots_time) << "+" << PrettyDuration(pause_dirty_time) |
| 800 | << ", total " << PrettyDuration(duration_ns); |
| 801 | } |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 802 | } else { |
Mathieu Chartier | b43b7d4 | 2012-06-19 13:15:09 -0700 | [diff] [blame] | 803 | if (duration_ns > MsToNs(50)) { |
| 804 | uint64_t markSweepTime = (dirty_end - t0) / 1000 * 1000; |
| 805 | LOG(INFO) << "GC freed " << PrettySize(bytes_freed) << ", " << GetPercentFree() << "% free, " |
| 806 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) << ", " |
| 807 | << "paused " << PrettyDuration(markSweepTime) |
| 808 | << ", total " << PrettyDuration(duration_ns); |
| 809 | } |
Mathieu Chartier | 662618f | 2012-06-06 12:01:47 -0700 | [diff] [blame] | 810 | } |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 811 | } |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 812 | Dbg::GcDidFinish(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 813 | if (VLOG_IS_ON(heap)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 814 | timings.Dump(); |
| 815 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 816 | |
| 817 | is_gc_running_ = false; |
| 818 | |
| 819 | // Wake anyone who may have been waiting for the GC to complete. |
| 820 | condition_->Broadcast(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 823 | bool Heap::WaitForConcurrentGcToComplete() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 824 | lock_->AssertHeld(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 825 | |
| 826 | // Busy wait for GC to finish |
| 827 | if (is_gc_running_) { |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 828 | uint64_t wait_start = NanoTime(); |
Mathieu Chartier | 06f7987 | 2012-06-21 13:51:52 -0700 | [diff] [blame] | 829 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 830 | do { |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 831 | ScopedThreadStateChange tsc(Thread::Current(), kVmWait); |
Mathieu Chartier | 06f7987 | 2012-06-21 13:51:52 -0700 | [diff] [blame] | 832 | ScopedThreadListLockReleaser list_lock_releaser; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 833 | condition_->Wait(*lock_); |
| 834 | } while (is_gc_running_); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 835 | uint64_t wait_time = NanoTime() - wait_start; |
| 836 | if (wait_time > MsToNs(5)) { |
| 837 | LOG(INFO) << "WaitForConcurrentGcToComplete blocked for " << PrettyDuration(wait_time); |
| 838 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 839 | DCHECK(!is_gc_running_); |
| 840 | return true; |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 841 | } |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 842 | return false; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 845 | void Heap::DumpForSigQuit(std::ostream& os) { |
| 846 | os << "Heap: " << GetPercentFree() << "% free, " |
| 847 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 848 | << "; " << num_objects_allocated_ << " objects\n"; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | size_t Heap::GetPercentFree() { |
| 852 | size_t total = GetTotalMemory(); |
| 853 | return 100 - static_cast<size_t>(100.0f * static_cast<float>(num_bytes_allocated_) / total); |
| 854 | } |
| 855 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 856 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 857 | // TODO: C++0x auto |
| 858 | for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) { |
| 859 | if ((*cur)->IsAllocSpace()) { |
| 860 | AllocSpace* alloc_space = (*cur)->AsAllocSpace(); |
| 861 | // TODO: Behavior for multiple alloc spaces? |
| 862 | size_t alloc_space_capacity = alloc_space->Capacity(); |
| 863 | if (max_allowed_footprint > alloc_space_capacity) { |
| 864 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) |
| 865 | << " to " << PrettySize(alloc_space_capacity); |
| 866 | max_allowed_footprint = alloc_space_capacity; |
| 867 | } |
| 868 | alloc_space->SetFootprintLimit(max_allowed_footprint); |
| 869 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 870 | } |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 871 | } |
| 872 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 873 | // kHeapIdealFree is the ideal maximum free size, when we grow the heap for utilization. |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 874 | static const size_t kHeapIdealFree = 2 * MB; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 875 | // kHeapMinFree guarantees that you always have at least 512 KB free, when you grow for utilization, |
| 876 | // regardless of target utilization ratio. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 877 | static const size_t kHeapMinFree = kHeapIdealFree / 4; |
| 878 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 879 | void Heap::GrowForUtilization() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 880 | lock_->AssertHeld(); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 881 | |
| 882 | // We know what our utilization is at this moment. |
| 883 | // This doesn't actually resize any memory. It just lets the heap grow more |
| 884 | // when necessary. |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 885 | size_t target_size(num_bytes_allocated_ / Heap::GetTargetHeapUtilization()); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 886 | |
| 887 | if (target_size > num_bytes_allocated_ + kHeapIdealFree) { |
| 888 | target_size = num_bytes_allocated_ + kHeapIdealFree; |
| 889 | } else if (target_size < num_bytes_allocated_ + kHeapMinFree) { |
| 890 | target_size = num_bytes_allocated_ + kHeapMinFree; |
| 891 | } |
| 892 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 893 | // Calculate when to perform the next ConcurrentGC. |
| 894 | if (GetTotalMemory() - num_bytes_allocated_ < concurrent_min_free_) { |
| 895 | // Not enough free memory to perform concurrent GC. |
| 896 | concurrent_start_bytes_ = std::numeric_limits<size_t>::max(); |
| 897 | } else { |
| 898 | concurrent_start_bytes_ = alloc_space_->GetFootprintLimit() - concurrent_start_size_; |
| 899 | } |
| 900 | |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 901 | SetIdealFootprint(target_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 902 | } |
| 903 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 904 | void Heap::ClearGrowthLimit() { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 905 | ScopedHeapLock heap_lock; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 906 | WaitForConcurrentGcToComplete(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 907 | alloc_space_->ClearGrowthLimit(); |
| 908 | } |
| 909 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 910 | pid_t Heap::GetLockOwner() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 911 | return lock_->GetOwner(); |
| 912 | } |
| 913 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 914 | void Heap::Lock() { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 915 | // Grab the lock, but put ourselves into kVmWait if it looks |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 916 | // like we're going to have to wait on the mutex. This prevents |
| 917 | // deadlock if another thread is calling CollectGarbageInternal, |
| 918 | // since they will have the heap lock and be waiting for mutators to |
| 919 | // suspend. |
| 920 | if (!lock_->TryLock()) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 921 | ScopedThreadStateChange tsc(Thread::Current(), kVmWait); |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 922 | lock_->Lock(); |
| 923 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void Heap::Unlock() { |
| 927 | lock_->Unlock(); |
| 928 | } |
| 929 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 930 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 931 | MemberOffset reference_queue_offset, |
| 932 | MemberOffset reference_queueNext_offset, |
| 933 | MemberOffset reference_pendingNext_offset, |
| 934 | MemberOffset finalizer_reference_zombie_offset) { |
| 935 | reference_referent_offset_ = reference_referent_offset; |
| 936 | reference_queue_offset_ = reference_queue_offset; |
| 937 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 938 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 939 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 940 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 941 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 942 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 943 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 944 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 945 | } |
| 946 | |
| 947 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 948 | DCHECK(reference != NULL); |
| 949 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 950 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 951 | } |
| 952 | |
| 953 | void Heap::ClearReferenceReferent(Object* reference) { |
| 954 | DCHECK(reference != NULL); |
| 955 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 956 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 957 | } |
| 958 | |
| 959 | // Returns true if the reference object has not yet been enqueued. |
| 960 | bool Heap::IsEnqueuable(const Object* ref) { |
| 961 | DCHECK(ref != NULL); |
| 962 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 963 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 964 | return (queue != NULL) && (queue_next == NULL); |
| 965 | } |
| 966 | |
| 967 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 968 | DCHECK(ref != NULL); |
| 969 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 970 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 971 | EnqueuePendingReference(ref, cleared_reference_list); |
| 972 | } |
| 973 | |
| 974 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 975 | DCHECK(ref != NULL); |
| 976 | DCHECK(list != NULL); |
| 977 | |
| 978 | if (*list == NULL) { |
| 979 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 980 | *list = ref; |
| 981 | } else { |
| 982 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 983 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 984 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | Object* Heap::DequeuePendingReference(Object** list) { |
| 989 | DCHECK(list != NULL); |
| 990 | DCHECK(*list != NULL); |
| 991 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 992 | Object* ref; |
| 993 | if (*list == head) { |
| 994 | ref = *list; |
| 995 | *list = NULL; |
| 996 | } else { |
| 997 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 998 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 999 | ref = head; |
| 1000 | } |
| 1001 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 1002 | return ref; |
| 1003 | } |
| 1004 | |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 1005 | void Heap::AddFinalizerReference(Thread* self, Object* object) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1006 | ScopedJniThreadState ts(self); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1007 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1008 | args[0].SetL(object); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1009 | ts.DecodeMethod(WellKnownClasses::java_lang_ref_FinalizerReference_add)->Invoke(self, NULL, args, NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1010 | } |
| 1011 | |
| 1012 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 1013 | DCHECK(cleared != NULL); |
| 1014 | if (*cleared != NULL) { |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1015 | ScopedJniThreadState ts(Thread::Current()); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 1016 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 1017 | args[0].SetL(*cleared); |
Ian Rogers | 365c102 | 2012-06-22 15:05:28 -0700 | [diff] [blame] | 1018 | ts.DecodeMethod(WellKnownClasses::java_lang_ref_ReferenceQueue_add)->Invoke(ts.Self(), NULL, args, NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 1019 | *cleared = NULL; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1023 | void Heap::RequestConcurrentGC() { |
Mathieu Chartier | 069387a | 2012-06-18 12:01:01 -0700 | [diff] [blame] | 1024 | // Make sure that we can do a concurrent GC. |
| 1025 | if (requesting_gc_ || |
| 1026 | !Runtime::Current()->IsFinishedStarting() || |
| 1027 | Runtime::Current()->IsShuttingDown() || |
| 1028 | !Runtime::Current()->IsConcurrentGcEnabled()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1029 | return; |
| 1030 | } |
| 1031 | |
| 1032 | requesting_gc_ = true; |
| 1033 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1034 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1035 | DCHECK(WellKnownClasses::java_lang_Daemons_requestGC != NULL); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1036 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_requestGC); |
| 1037 | CHECK(!env->ExceptionCheck()); |
| 1038 | requesting_gc_ = false; |
| 1039 | } |
| 1040 | |
| 1041 | void Heap::ConcurrentGC() { |
Mathieu Chartier | 2542d66 | 2012-06-21 17:14:11 -0700 | [diff] [blame] | 1042 | if (Runtime::Current()->IsShuttingDown()) { |
| 1043 | return; |
| 1044 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1045 | ScopedHeapLock heap_lock; |
Mathieu Chartier | fc8cfac | 2012-06-19 11:56:36 -0700 | [diff] [blame] | 1046 | // We shouldn't need a WaitForConcurrentGcToComplete here since only |
| 1047 | // concurrent GC resumes threads before the GC is completed and this function |
| 1048 | // is only called within the GC daemon thread. |
| 1049 | CHECK(!is_gc_running_); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1050 | // Current thread needs to be runnable or else we can't suspend all threads. |
| 1051 | ScopedThreadStateChange tsc(Thread::Current(), kRunnable); |
| 1052 | CollectGarbageInternal(true, false); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1055 | void Heap::Trim(AllocSpace* alloc_space) { |
Mathieu Chartier | 5dbf829 | 2012-06-11 13:51:41 -0700 | [diff] [blame] | 1056 | lock_->AssertHeld(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1057 | WaitForConcurrentGcToComplete(); |
Mathieu Chartier | b062fdd | 2012-07-03 09:51:48 -0700 | [diff] [blame] | 1058 | alloc_space->Trim(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1061 | void Heap::RequestHeapTrim() { |
| 1062 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 1063 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 1064 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 1065 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 1066 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 1067 | // not how much use we're making of those pages. |
| 1068 | float utilization = static_cast<float>(num_bytes_allocated_) / alloc_space_->Size(); |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1069 | uint64_t ms_time = NsToMs(NanoTime()); |
| 1070 | if (utilization > 0.75f || ms_time - last_trim_time_ < 2 * 1000) { |
| 1071 | // Don't bother trimming the heap if it's more than 75% utilized, or if a |
| 1072 | // heap trim occurred in the last two seconds. |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1073 | return; |
| 1074 | } |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1075 | if (!Runtime::Current()->IsFinishedStarting() || Runtime::Current()->IsShuttingDown()) { |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1076 | // Heap trimming isn't supported without a Java runtime or Daemons (such as at dex2oat time) |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1077 | // Also: we do not wish to start a heap trim if the runtime is shutting down. |
Ian Rogers | e1d490c | 2012-02-03 09:09:07 -0800 | [diff] [blame] | 1078 | return; |
| 1079 | } |
Mathieu Chartier | 7664f5c | 2012-06-08 18:15:32 -0700 | [diff] [blame] | 1080 | last_trim_time_ = ms_time; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1081 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
Mathieu Chartier | a639903 | 2012-06-11 18:49:50 -0700 | [diff] [blame] | 1082 | DCHECK(WellKnownClasses::java_lang_Daemons != NULL); |
| 1083 | DCHECK(WellKnownClasses::java_lang_Daemons_requestHeapTrim != NULL); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 1084 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_requestHeapTrim); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 1085 | CHECK(!env->ExceptionCheck()); |
| 1086 | } |
| 1087 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1088 | } // namespace art |