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" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 29 | #include "object.h" |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 30 | #include "object_utils.h" |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 31 | #include "os.h" |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 32 | #include "scoped_heap_lock.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 33 | #include "space.h" |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 34 | #include "stl_util.h" |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 35 | #include "thread_list.h" |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 36 | #include "timing_logger.h" |
| 37 | #include "UniquePtr.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 38 | #include "well_known_classes.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 39 | |
| 40 | namespace art { |
| 41 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 42 | static void UpdateFirstAndLastSpace(Space** first_space, Space** last_space, Space* space) { |
| 43 | if (*first_space == NULL) { |
| 44 | *first_space = space; |
| 45 | *last_space = space; |
| 46 | } else { |
| 47 | if ((*first_space)->Begin() > space->Begin()) { |
| 48 | *first_space = space; |
| 49 | } else if (space->Begin() > (*last_space)->Begin()) { |
| 50 | *last_space = space; |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 55 | static bool GenerateImage(const std::string& image_file_name) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 56 | const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString()); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 57 | std::vector<std::string> boot_class_path; |
| 58 | Split(boot_class_path_string, ':', boot_class_path); |
Brian Carlstrom | b279337 | 2012-03-17 18:27:16 -0700 | [diff] [blame] | 59 | if (boot_class_path.empty()) { |
| 60 | LOG(FATAL) << "Failed to generate image because no boot class path specified"; |
| 61 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 62 | |
| 63 | std::vector<char*> arg_vector; |
| 64 | |
| 65 | std::string dex2oat_string(GetAndroidRoot()); |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 66 | dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat"); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 67 | const char* dex2oat = dex2oat_string.c_str(); |
| 68 | arg_vector.push_back(strdup(dex2oat)); |
| 69 | |
| 70 | std::string image_option_string("--image="); |
| 71 | image_option_string += image_file_name; |
| 72 | const char* image_option = image_option_string.c_str(); |
| 73 | arg_vector.push_back(strdup(image_option)); |
| 74 | |
| 75 | arg_vector.push_back(strdup("--runtime-arg")); |
| 76 | arg_vector.push_back(strdup("-Xms64m")); |
| 77 | |
| 78 | arg_vector.push_back(strdup("--runtime-arg")); |
| 79 | arg_vector.push_back(strdup("-Xmx64m")); |
| 80 | |
| 81 | for (size_t i = 0; i < boot_class_path.size(); i++) { |
| 82 | std::string dex_file_option_string("--dex-file="); |
| 83 | dex_file_option_string += boot_class_path[i]; |
| 84 | const char* dex_file_option = dex_file_option_string.c_str(); |
| 85 | arg_vector.push_back(strdup(dex_file_option)); |
| 86 | } |
| 87 | |
| 88 | std::string oat_file_option_string("--oat-file="); |
| 89 | oat_file_option_string += image_file_name; |
| 90 | oat_file_option_string.erase(oat_file_option_string.size() - 3); |
| 91 | oat_file_option_string += "oat"; |
| 92 | const char* oat_file_option = oat_file_option_string.c_str(); |
| 93 | arg_vector.push_back(strdup(oat_file_option)); |
| 94 | |
| 95 | arg_vector.push_back(strdup("--base=0x60000000")); |
| 96 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 97 | std::string command_line(Join(arg_vector, ' ')); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 98 | LOG(INFO) << command_line; |
| 99 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 100 | arg_vector.push_back(NULL); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 101 | char** argv = &arg_vector[0]; |
| 102 | |
| 103 | // fork and exec dex2oat |
| 104 | pid_t pid = fork(); |
| 105 | if (pid == 0) { |
| 106 | // no allocation allowed between fork and exec |
| 107 | |
| 108 | // change process groups, so we don't get reaped by ProcessManager |
| 109 | setpgid(0, 0); |
| 110 | |
| 111 | execv(dex2oat, argv); |
| 112 | |
| 113 | PLOG(FATAL) << "execv(" << dex2oat << ") failed"; |
| 114 | return false; |
| 115 | } else { |
| 116 | STLDeleteElements(&arg_vector); |
| 117 | |
| 118 | // wait for dex2oat to finish |
| 119 | int status; |
| 120 | pid_t got_pid = TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)); |
| 121 | if (got_pid != pid) { |
| 122 | PLOG(ERROR) << "waitpid failed: wanted " << pid << ", got " << got_pid; |
| 123 | return false; |
| 124 | } |
| 125 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 126 | LOG(ERROR) << dex2oat << " failed: " << command_line; |
| 127 | return false; |
| 128 | } |
| 129 | } |
| 130 | return true; |
| 131 | } |
| 132 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 133 | Heap::Heap(size_t initial_size, size_t growth_limit, size_t capacity, |
| 134 | const std::string& original_image_file_name) |
| 135 | : lock_(NULL), |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 136 | image_space_(NULL), |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 137 | alloc_space_(NULL), |
| 138 | mark_bitmap_(NULL), |
| 139 | live_bitmap_(NULL), |
| 140 | card_table_(NULL), |
| 141 | card_marking_disabled_(false), |
| 142 | is_gc_running_(false), |
| 143 | num_bytes_allocated_(0), |
| 144 | num_objects_allocated_(0), |
| 145 | java_lang_ref_FinalizerReference_(NULL), |
| 146 | java_lang_ref_ReferenceQueue_(NULL), |
| 147 | reference_referent_offset_(0), |
| 148 | reference_queue_offset_(0), |
| 149 | reference_queueNext_offset_(0), |
| 150 | reference_pendingNext_offset_(0), |
| 151 | finalizer_reference_zombie_offset_(0), |
| 152 | target_utilization_(0.5), |
Elliott Hughes | b25c3f6 | 2012-03-26 16:35:06 -0700 | [diff] [blame] | 153 | verify_objects_(false) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 154 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 155 | LOG(INFO) << "Heap() entering"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 158 | // Compute the bounds of all spaces for allocating live and mark bitmaps |
| 159 | // there will be at least one space (the alloc space) |
| 160 | Space* first_space = NULL; |
| 161 | Space* last_space = NULL; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 162 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 163 | // Requested begin for the alloc space, to follow the mapped image and oat files |
| 164 | byte* requested_begin = NULL; |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 165 | std::string image_file_name(original_image_file_name); |
| 166 | if (!image_file_name.empty()) { |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 167 | if (OS::FileExists(image_file_name.c_str())) { |
| 168 | // If the /system file exists, it should be up-to-date, don't try to generate |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 169 | image_space_ = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 170 | } else { |
| 171 | // If the /system file didn't exist, we need to use one from the art-cache. |
| 172 | // If the cache file exists, try to open, but if it fails, regenerate. |
| 173 | // If it does not exist, generate. |
| 174 | image_file_name = GetArtCacheFilenameOrDie(image_file_name); |
| 175 | if (OS::FileExists(image_file_name.c_str())) { |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 176 | image_space_ = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 177 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 178 | if (image_space_ == NULL) { |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 179 | if (!GenerateImage(image_file_name)) { |
| 180 | LOG(FATAL) << "Failed to generate image: " << image_file_name; |
| 181 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 182 | image_space_ = Space::CreateImageSpace(image_file_name); |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 183 | } |
| 184 | } |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 185 | if (image_space_ == NULL) { |
Brian Carlstrom | 223f20f | 2012-02-04 23:06:55 -0800 | [diff] [blame] | 186 | LOG(FATAL) << "Failed to create space from " << image_file_name; |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 187 | } |
Brian Carlstrom | 5643b78 | 2012-02-05 12:32:53 -0800 | [diff] [blame] | 188 | |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 189 | AddSpace(image_space_); |
| 190 | UpdateFirstAndLastSpace(&first_space, &last_space, image_space_); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 191 | // Oat files referenced by image files immediately follow them in memory, ensure alloc space |
| 192 | // isn't going to get in the middle |
Brian Carlstrom | fddf6f6 | 2012-03-15 16:56:45 -0700 | [diff] [blame] | 193 | byte* oat_end_addr = image_space_->GetImageHeader().GetOatEnd(); |
| 194 | CHECK(oat_end_addr > image_space_->End()); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 195 | if (oat_end_addr > requested_begin) { |
| 196 | requested_begin = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr), |
| 197 | kPageSize)); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 198 | } |
Brian Carlstrom | 69b15fb | 2011-09-03 12:25:21 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 201 | alloc_space_ = Space::CreateAllocSpace("alloc space", initial_size, growth_limit, capacity, |
| 202 | requested_begin); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 203 | if (alloc_space_ == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 204 | LOG(FATAL) << "Failed to create alloc space"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 205 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 206 | AddSpace(alloc_space_); |
| 207 | UpdateFirstAndLastSpace(&first_space, &last_space, alloc_space_); |
| 208 | byte* heap_begin = first_space->Begin(); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 209 | 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] | 210 | |
| 211 | // Allocate the initial live bitmap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 212 | UniquePtr<HeapBitmap> live_bitmap(HeapBitmap::Create("dalvik-bitmap-1", heap_begin, heap_capacity)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 213 | if (live_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 214 | LOG(FATAL) << "Failed to create live bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 217 | // Mark image objects in the live bitmap |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 218 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 219 | Space* space = spaces_[i]; |
| 220 | if (space->IsImageSpace()) { |
| 221 | space->AsImageSpace()->RecordImageAllocations(live_bitmap.get()); |
| 222 | } |
| 223 | } |
| 224 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 225 | // Allocate the initial mark bitmap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 226 | UniquePtr<HeapBitmap> mark_bitmap(HeapBitmap::Create("dalvik-bitmap-2", heap_begin, heap_capacity)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 227 | if (mark_bitmap.get() == NULL) { |
Elliott Hughes | be759c6 | 2011-09-08 19:38:21 -0700 | [diff] [blame] | 228 | LOG(FATAL) << "Failed to create mark bitmap"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 231 | // Allocate the card table. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 232 | UniquePtr<CardTable> card_table(CardTable::Create(heap_begin, heap_capacity)); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 233 | if (card_table.get() == NULL) { |
| 234 | LOG(FATAL) << "Failed to create card table"; |
| 235 | } |
| 236 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 237 | live_bitmap_ = live_bitmap.release(); |
| 238 | mark_bitmap_ = mark_bitmap.release(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 239 | card_table_ = card_table.release(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 240 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 241 | num_bytes_allocated_ = 0; |
| 242 | num_objects_allocated_ = 0; |
| 243 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 244 | // 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] | 245 | // but we can create the heap lock now. We don't create it earlier to |
| 246 | // make it clear that you can't use locks during heap initialization. |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 247 | lock_ = new Mutex("Heap lock", kHeapLock); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 248 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 249 | if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) { |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 250 | LOG(INFO) << "Heap() exiting"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 251 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 254 | void Heap::AddSpace(Space* space) { |
| 255 | spaces_.push_back(space); |
| 256 | } |
| 257 | |
| 258 | Heap::~Heap() { |
| 259 | VLOG(heap) << "~Heap()"; |
Elliott Hughes | b3e66df | 2012-01-12 14:49:18 -0800 | [diff] [blame] | 260 | // We can't take the heap lock here because there might be a daemon thread suspended with the |
| 261 | // heap lock held. We know though that no non-daemon threads are executing, and we know that |
| 262 | // all daemon threads are suspended, and we also know that the threads list have been deleted, so |
| 263 | // 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] | 264 | STLDeleteElements(&spaces_); |
Elliott Hughes | 4d6850c | 2012-01-18 15:55:06 -0800 | [diff] [blame] | 265 | delete mark_bitmap_; |
| 266 | delete live_bitmap_; |
| 267 | delete card_table_; |
| 268 | delete lock_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 271 | static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) { |
| 272 | size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg); |
| 273 | |
| 274 | size_t chunk_size = static_cast<size_t>(reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start)); |
| 275 | size_t chunk_free_bytes = 0; |
| 276 | if (used_bytes < chunk_size) { |
| 277 | chunk_free_bytes = chunk_size - used_bytes; |
| 278 | } |
| 279 | |
| 280 | if (chunk_free_bytes > max_contiguous_allocation) { |
| 281 | max_contiguous_allocation = chunk_free_bytes; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | Object* Heap::AllocObject(Class* c, size_t byte_count) { |
| 286 | // Used in the detail message if we throw an OOME. |
| 287 | int64_t total_bytes_free; |
| 288 | size_t max_contiguous_allocation; |
| 289 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 290 | { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 291 | ScopedHeapLock heap_lock; |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 292 | DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(Class)) || |
| 293 | (c->IsVariableSize() || c->GetObjectSize() == byte_count) || |
| 294 | strlen(ClassHelper(c).GetDescriptor()) == 0); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 295 | DCHECK_GE(byte_count, sizeof(Object)); |
| 296 | Object* obj = AllocateLocked(byte_count); |
| 297 | if (obj != NULL) { |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 298 | obj->SetClass(c); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 299 | if (Dbg::IsAllocTrackingEnabled()) { |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 300 | Dbg::RecordAllocation(c, byte_count); |
Elliott Hughes | 545a064 | 2011-11-08 19:10:03 -0800 | [diff] [blame] | 301 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 302 | return obj; |
| 303 | } |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 304 | total_bytes_free = GetFreeMemory(); |
| 305 | max_contiguous_allocation = 0; |
| 306 | GetAllocSpace()->Walk(MSpaceChunkCallback, &max_contiguous_allocation); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 307 | } |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 308 | |
Elliott Hughes | 8a8b9cb | 2012-04-13 18:29:22 -0700 | [diff] [blame] | 309 | std::string msg(StringPrintf("Failed to allocate a %zd-byte %s (%lld total bytes free; largest possible contiguous allocation %zd bytes)", |
| 310 | byte_count, |
| 311 | PrettyDescriptor(c).c_str(), |
| 312 | total_bytes_free, max_contiguous_allocation)); |
| 313 | Thread::Current()->ThrowOutOfMemoryError(msg.c_str()); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 314 | return NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 317 | bool Heap::IsHeapAddress(const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 318 | // Note: we deliberately don't take the lock here, and mustn't test anything that would |
| 319 | // require taking the lock. |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 320 | if (obj == NULL) { |
| 321 | return true; |
| 322 | } |
| 323 | if (!IsAligned<kObjectAlignment>(obj)) { |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 324 | return false; |
| 325 | } |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 326 | for (size_t i = 0; i < spaces_.size(); ++i) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 327 | if (spaces_[i]->Contains(obj)) { |
| 328 | return true; |
| 329 | } |
| 330 | } |
| 331 | return false; |
Elliott Hughes | a250199 | 2011-08-26 19:39:54 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 334 | bool Heap::IsLiveObjectLocked(const Object* obj) { |
| 335 | lock_->AssertHeld(); |
| 336 | return IsHeapAddress(obj) && live_bitmap_->Test(obj); |
| 337 | } |
| 338 | |
Elliott Hughes | 3e465b1 | 2011-09-02 18:26:12 -0700 | [diff] [blame] | 339 | #if VERIFY_OBJECT_ENABLED |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 340 | void Heap::VerifyObject(const Object* obj) { |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 341 | if (this == NULL || !verify_objects_ || Runtime::Current()->IsShuttingDown() || |
Ian Rogers | 141d622 | 2012-04-05 12:23:06 -0700 | [diff] [blame] | 342 | Thread::Current() == NULL || |
jeffhao | 2504552 | 2012-03-13 19:34:37 -0700 | [diff] [blame] | 343 | Runtime::Current()->GetThreadList()->GetLockOwner() == Thread::Current()->GetTid()) { |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 344 | return; |
| 345 | } |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 346 | ScopedHeapLock heap_lock; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 347 | Heap::VerifyObjectLocked(obj); |
| 348 | } |
| 349 | #endif |
| 350 | |
| 351 | void Heap::VerifyObjectLocked(const Object* obj) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 352 | lock_->AssertHeld(); |
Elliott Hughes | 85d1545 | 2011-09-16 17:33:01 -0700 | [diff] [blame] | 353 | if (obj != NULL) { |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 354 | if (!IsAligned<kObjectAlignment>(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 355 | LOG(FATAL) << "Object isn't aligned: " << obj; |
| 356 | } else if (!live_bitmap_->Test(obj)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 357 | LOG(FATAL) << "Object is dead: " << obj; |
| 358 | } |
| 359 | // Ignore early dawn of the universe verifications |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 360 | if (num_objects_allocated_ > 10) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 361 | const byte* raw_addr = reinterpret_cast<const byte*>(obj) + |
| 362 | Object::ClassOffset().Int32Value(); |
| 363 | const Class* c = *reinterpret_cast<Class* const *>(raw_addr); |
| 364 | if (c == NULL) { |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 365 | LOG(FATAL) << "Null class in object: " << obj; |
Elliott Hughes | 06b37d9 | 2011-10-16 11:51:29 -0700 | [diff] [blame] | 366 | } else if (!IsAligned<kObjectAlignment>(c)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 367 | LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj; |
| 368 | } else if (!live_bitmap_->Test(c)) { |
| 369 | LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj; |
| 370 | } |
| 371 | // Check obj.getClass().getClass() == obj.getClass().getClass().getClass() |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 372 | // 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] | 373 | // that we don't want to run |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 374 | raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 375 | const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 376 | raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 377 | const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr); |
| 378 | CHECK_EQ(c_c, c_c_c); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 383 | void Heap::VerificationCallback(Object* obj, void* arg) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 384 | DCHECK(obj != NULL); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 385 | reinterpret_cast<Heap*>(arg)->VerifyObjectLocked(obj); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | void Heap::VerifyHeap() { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 389 | ScopedHeapLock heap_lock; |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 390 | live_bitmap_->Walk(Heap::VerificationCallback, this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 393 | void Heap::RecordAllocationLocked(AllocSpace* space, const Object* obj) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 394 | #ifndef NDEBUG |
| 395 | if (Runtime::Current()->IsStarted()) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 396 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 397 | } |
| 398 | #endif |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 399 | size_t size = space->AllocationSize(obj); |
Elliott Hughes | 5d78d39 | 2011-12-13 16:53:05 -0800 | [diff] [blame] | 400 | DCHECK_GT(size, 0u); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 401 | num_bytes_allocated_ += size; |
| 402 | num_objects_allocated_ += 1; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 403 | |
| 404 | if (Runtime::Current()->HasStatsEnabled()) { |
| 405 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 406 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 407 | ++global_stats->allocated_objects; |
| 408 | ++thread_stats->allocated_objects; |
| 409 | global_stats->allocated_bytes += size; |
| 410 | thread_stats->allocated_bytes += size; |
| 411 | } |
| 412 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 413 | live_bitmap_->Set(obj); |
| 414 | } |
| 415 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 416 | void Heap::RecordFreeLocked(size_t freed_objects, size_t freed_bytes) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 417 | lock_->AssertHeld(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 418 | |
| 419 | if (freed_objects < num_objects_allocated_) { |
| 420 | num_objects_allocated_ -= freed_objects; |
| 421 | } else { |
| 422 | num_objects_allocated_ = 0; |
| 423 | } |
| 424 | if (freed_bytes < num_bytes_allocated_) { |
| 425 | num_bytes_allocated_ -= freed_bytes; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 426 | } else { |
| 427 | num_bytes_allocated_ = 0; |
| 428 | } |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 429 | |
| 430 | if (Runtime::Current()->HasStatsEnabled()) { |
| 431 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 432 | RuntimeStats* thread_stats = Thread::Current()->GetStats(); |
| 433 | ++global_stats->freed_objects; |
| 434 | ++thread_stats->freed_objects; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 435 | global_stats->freed_bytes += freed_bytes; |
| 436 | thread_stats->freed_bytes += freed_bytes; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 437 | } |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 440 | Object* Heap::AllocateLocked(size_t size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 441 | lock_->AssertHeld(); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 442 | DCHECK(alloc_space_ != NULL); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 443 | AllocSpace* space = alloc_space_; |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 444 | Object* obj = AllocateLocked(space, size); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 445 | if (obj != NULL) { |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 446 | RecordAllocationLocked(space, obj); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 447 | } |
| 448 | return obj; |
| 449 | } |
| 450 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 451 | Object* Heap::AllocateLocked(AllocSpace* space, size_t alloc_size) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 452 | lock_->AssertHeld(); |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 453 | |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 454 | // Since allocation can cause a GC which will need to SuspendAll, |
| 455 | // make sure all allocators are in the kRunnable state. |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 456 | CHECK_EQ(Thread::Current()->GetState(), kRunnable); |
Brian Carlstrom | b82b687 | 2011-10-26 17:18:07 -0700 | [diff] [blame] | 457 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 458 | // Fail impossible allocations |
| 459 | if (alloc_size > space->Capacity()) { |
| 460 | // On failure collect soft references |
| 461 | CollectGarbageInternal(true); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 462 | return NULL; |
| 463 | } |
| 464 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 465 | Object* ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 466 | if (ptr != NULL) { |
| 467 | return ptr; |
| 468 | } |
| 469 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 470 | // 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] | 471 | if (is_gc_running_) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 472 | // The GC is concurrently tracing the heap. Release the heap lock, wait for the GC to |
| 473 | // complete, and retrying allocating. |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 474 | WaitForConcurrentGcToComplete(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 475 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 476 | if (ptr != NULL) { |
| 477 | return ptr; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Another failure. Our thread was starved or there may be too many |
| 482 | // live objects. Try a foreground GC. This will have no effect if |
| 483 | // the concurrent GC is already running. |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 484 | if (Runtime::Current()->HasStatsEnabled()) { |
| 485 | ++Runtime::Current()->GetStats()->gc_for_alloc_count; |
| 486 | ++Thread::Current()->GetStats()->gc_for_alloc_count; |
| 487 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 488 | CollectGarbageInternal(false); |
| 489 | ptr = space->AllocWithoutGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 490 | if (ptr != NULL) { |
| 491 | return ptr; |
| 492 | } |
| 493 | |
| 494 | // Even that didn't work; this is an exceptional state. |
| 495 | // Try harder, growing the heap if necessary. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 496 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 497 | if (ptr != NULL) { |
| 498 | //size_t new_footprint = dvmHeapSourceGetIdealFootprint(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 499 | size_t new_footprint = space->GetFootprintLimit(); |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 500 | // 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] | 501 | // free space is equal to the old free space + the |
| 502 | // utilization slop for the new allocation. |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 503 | VLOG(gc) << "Grow heap (frag case) to " << PrettySize(new_footprint) |
Ian Rogers | 162a31c | 2012-01-31 16:14:31 -0800 | [diff] [blame] | 504 | << " for a " << PrettySize(alloc_size) << " allocation"; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 505 | return ptr; |
| 506 | } |
| 507 | |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 508 | // Most allocations should have succeeded by now, so the heap is really full, really fragmented, |
| 509 | // or the requested size is really big. Do another GC, collecting SoftReferences this time. The |
| 510 | // 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] | 511 | |
Elliott Hughes | 418dfe7 | 2011-10-06 18:56:27 -0700 | [diff] [blame] | 512 | // OLD-TODO: wait for the finalizers from the previous GC to finish |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 513 | VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) << " allocation"; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 514 | CollectGarbageInternal(true); |
| 515 | ptr = space->AllocWithGrowth(alloc_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 516 | if (ptr != NULL) { |
| 517 | return ptr; |
| 518 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 519 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 520 | return NULL; |
| 521 | } |
| 522 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 523 | int64_t Heap::GetMaxMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 524 | return alloc_space_->Capacity(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | int64_t Heap::GetTotalMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 528 | return alloc_space_->Capacity(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | int64_t Heap::GetFreeMemory() { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 532 | return alloc_space_->Capacity() - num_bytes_allocated_; |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 535 | class InstanceCounter { |
| 536 | public: |
| 537 | InstanceCounter(Class* c, bool count_assignable) |
| 538 | : class_(c), count_assignable_(count_assignable), count_(0) { |
| 539 | } |
| 540 | |
| 541 | size_t GetCount() { |
| 542 | return count_; |
| 543 | } |
| 544 | |
| 545 | static void Callback(Object* o, void* arg) { |
| 546 | reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o); |
| 547 | } |
| 548 | |
| 549 | private: |
| 550 | void VisitInstance(Object* o) { |
| 551 | Class* instance_class = o->GetClass(); |
| 552 | if (count_assignable_) { |
| 553 | if (instance_class == class_) { |
| 554 | ++count_; |
| 555 | } |
| 556 | } else { |
| 557 | if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) { |
| 558 | ++count_; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | Class* class_; |
| 564 | bool count_assignable_; |
| 565 | size_t count_; |
| 566 | }; |
| 567 | |
| 568 | int64_t Heap::CountInstances(Class* c, bool count_assignable) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 569 | ScopedHeapLock heap_lock; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 570 | InstanceCounter counter(c, count_assignable); |
| 571 | live_bitmap_->Walk(InstanceCounter::Callback, &counter); |
| 572 | return counter.GetCount(); |
| 573 | } |
| 574 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 575 | void Heap::CollectGarbage(bool clear_soft_references) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 576 | ScopedHeapLock heap_lock; |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 577 | CollectGarbageInternal(clear_soft_references); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 578 | } |
| 579 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 580 | void Heap::CollectGarbageInternal(bool clear_soft_references) { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 581 | lock_->AssertHeld(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 582 | |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 583 | ThreadList* thread_list = Runtime::Current()->GetThreadList(); |
| 584 | thread_list->SuspendAll(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 585 | |
| 586 | size_t initial_size = num_bytes_allocated_; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 587 | TimingLogger timings("CollectGarbageInternal"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 588 | uint64_t t0 = NanoTime(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 589 | Object* cleared_references = NULL; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 590 | { |
| 591 | MarkSweep mark_sweep; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 592 | timings.AddSplit("ctor"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 593 | |
| 594 | mark_sweep.Init(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 595 | timings.AddSplit("Init"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 596 | |
| 597 | mark_sweep.MarkRoots(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 598 | timings.AddSplit("MarkRoots"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 599 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 600 | mark_sweep.ScanDirtyImageRoots(); |
| 601 | timings.AddSplit("DirtyImageRoots"); |
| 602 | |
| 603 | // Roots are marked on the bitmap and the mark_stack is empty |
| 604 | DCHECK(mark_sweep.IsMarkStackEmpty()); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 605 | |
| 606 | // TODO: if concurrent |
| 607 | // unlock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 608 | // thread_list->ResumeAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 609 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 610 | // Recursively mark all bits set in the non-image mark bitmap |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 611 | mark_sweep.RecursiveMark(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 612 | timings.AddSplit("RecursiveMark"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 613 | |
| 614 | // TODO: if concurrent |
| 615 | // lock heap |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 616 | // thread_list->SuspendAll(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 617 | // re-mark root set |
| 618 | // scan dirty objects |
| 619 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 620 | mark_sweep.ProcessReferences(clear_soft_references); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 621 | timings.AddSplit("ProcessReferences"); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 622 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 623 | // TODO: if concurrent |
| 624 | // swap bitmaps |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 625 | |
| 626 | mark_sweep.Sweep(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 627 | timings.AddSplit("Sweep"); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 628 | |
| 629 | cleared_references = mark_sweep.GetClearedReferences(); |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | GrowForUtilization(); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 633 | timings.AddSplit("GrowForUtilization"); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 634 | uint64_t t1 = NanoTime(); |
Elliott Hughes | 8d768a9 | 2011-09-14 16:35:25 -0700 | [diff] [blame] | 635 | thread_list->ResumeAll(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 636 | |
| 637 | EnqueueClearedReferences(&cleared_references); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 638 | RequestHeapTrim(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 639 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 640 | uint64_t duration_ns = t1 - t0; |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 641 | bool gc_was_particularly_slow = duration_ns > MsToNs(50); // TODO: crank this down for concurrent. |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 642 | if (VLOG_IS_ON(gc) || gc_was_particularly_slow) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 643 | // TODO: somehow make the specific GC implementation (here MarkSweep) responsible for logging. |
| 644 | size_t bytes_freed = initial_size - num_bytes_allocated_; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 645 | // lose low nanoseconds in duration. TODO: make this part of PrettyDuration |
| 646 | duration_ns = (duration_ns / 1000) * 1000; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 647 | LOG(INFO) << "GC freed " << PrettySize(bytes_freed) << ", " << GetPercentFree() << "% free, " |
| 648 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) << ", " |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 649 | << "paused " << PrettyDuration(duration_ns); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 650 | } |
Elliott Hughes | 767a147 | 2011-10-26 18:49:02 -0700 | [diff] [blame] | 651 | Dbg::GcDidFinish(); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 652 | if (VLOG_IS_ON(heap)) { |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 653 | timings.Dump(); |
| 654 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void Heap::WaitForConcurrentGcToComplete() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 658 | lock_->AssertHeld(); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 661 | void Heap::DumpForSigQuit(std::ostream& os) { |
| 662 | os << "Heap: " << GetPercentFree() << "% free, " |
| 663 | << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory()) |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 664 | << "; " << num_objects_allocated_ << " objects\n"; |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | size_t Heap::GetPercentFree() { |
| 668 | size_t total = GetTotalMemory(); |
| 669 | return 100 - static_cast<size_t>(100.0f * static_cast<float>(num_bytes_allocated_) / total); |
| 670 | } |
| 671 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 672 | void Heap::SetIdealFootprint(size_t max_allowed_footprint) { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 673 | size_t alloc_space_capacity = alloc_space_->Capacity(); |
| 674 | if (max_allowed_footprint > alloc_space_capacity) { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 675 | VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint) |
| 676 | << " to " << PrettySize(alloc_space_capacity); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 677 | max_allowed_footprint = alloc_space_capacity; |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 678 | } |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 679 | alloc_space_->SetFootprintLimit(max_allowed_footprint); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 682 | // 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] | 683 | static const size_t kHeapIdealFree = 2 * MB; |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 684 | // kHeapMinFree guarantees that you always have at least 512 KB free, when you grow for utilization, |
| 685 | // regardless of target utilization ratio. |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 686 | static const size_t kHeapMinFree = kHeapIdealFree / 4; |
| 687 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 688 | void Heap::GrowForUtilization() { |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 689 | lock_->AssertHeld(); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 690 | |
| 691 | // We know what our utilization is at this moment. |
| 692 | // This doesn't actually resize any memory. It just lets the heap grow more |
| 693 | // when necessary. |
Elliott Hughes | 362f9bc | 2011-10-17 18:56:41 -0700 | [diff] [blame] | 694 | size_t target_size(num_bytes_allocated_ / Heap::GetTargetHeapUtilization()); |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 695 | |
| 696 | if (target_size > num_bytes_allocated_ + kHeapIdealFree) { |
| 697 | target_size = num_bytes_allocated_ + kHeapIdealFree; |
| 698 | } else if (target_size < num_bytes_allocated_ + kHeapMinFree) { |
| 699 | target_size = num_bytes_allocated_ + kHeapMinFree; |
| 700 | } |
| 701 | |
| 702 | SetIdealFootprint(target_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 703 | } |
| 704 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 705 | void Heap::ClearGrowthLimit() { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 706 | ScopedHeapLock heap_lock; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 707 | WaitForConcurrentGcToComplete(); |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 708 | alloc_space_->ClearGrowthLimit(); |
| 709 | } |
| 710 | |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 711 | pid_t Heap::GetLockOwner() { |
Elliott Hughes | accd83d | 2011-10-17 14:25:58 -0700 | [diff] [blame] | 712 | return lock_->GetOwner(); |
| 713 | } |
| 714 | |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 715 | void Heap::Lock() { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 716 | // Grab the lock, but put ourselves into kVmWait if it looks |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 717 | // like we're going to have to wait on the mutex. This prevents |
| 718 | // deadlock if another thread is calling CollectGarbageInternal, |
| 719 | // since they will have the heap lock and be waiting for mutators to |
| 720 | // suspend. |
| 721 | if (!lock_->TryLock()) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 722 | ScopedThreadStateChange tsc(Thread::Current(), kVmWait); |
Brian Carlstrom | fad7143 | 2011-10-16 20:25:10 -0700 | [diff] [blame] | 723 | lock_->Lock(); |
| 724 | } |
Elliott Hughes | 92b3b56 | 2011-09-08 16:32:26 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | void Heap::Unlock() { |
| 728 | lock_->Unlock(); |
| 729 | } |
| 730 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 731 | void Heap::SetWellKnownClasses(Class* java_lang_ref_FinalizerReference, |
| 732 | Class* java_lang_ref_ReferenceQueue) { |
| 733 | java_lang_ref_FinalizerReference_ = java_lang_ref_FinalizerReference; |
| 734 | java_lang_ref_ReferenceQueue_ = java_lang_ref_ReferenceQueue; |
| 735 | CHECK(java_lang_ref_FinalizerReference_ != NULL); |
| 736 | CHECK(java_lang_ref_ReferenceQueue_ != NULL); |
| 737 | } |
| 738 | |
| 739 | void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset, |
| 740 | MemberOffset reference_queue_offset, |
| 741 | MemberOffset reference_queueNext_offset, |
| 742 | MemberOffset reference_pendingNext_offset, |
| 743 | MemberOffset finalizer_reference_zombie_offset) { |
| 744 | reference_referent_offset_ = reference_referent_offset; |
| 745 | reference_queue_offset_ = reference_queue_offset; |
| 746 | reference_queueNext_offset_ = reference_queueNext_offset; |
| 747 | reference_pendingNext_offset_ = reference_pendingNext_offset; |
| 748 | finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset; |
| 749 | CHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 750 | CHECK_NE(reference_queue_offset_.Uint32Value(), 0U); |
| 751 | CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U); |
| 752 | CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U); |
| 753 | CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U); |
| 754 | } |
| 755 | |
| 756 | Object* Heap::GetReferenceReferent(Object* reference) { |
| 757 | DCHECK(reference != NULL); |
| 758 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 759 | return reference->GetFieldObject<Object*>(reference_referent_offset_, true); |
| 760 | } |
| 761 | |
| 762 | void Heap::ClearReferenceReferent(Object* reference) { |
| 763 | DCHECK(reference != NULL); |
| 764 | DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U); |
| 765 | reference->SetFieldObject(reference_referent_offset_, NULL, true); |
| 766 | } |
| 767 | |
| 768 | // Returns true if the reference object has not yet been enqueued. |
| 769 | bool Heap::IsEnqueuable(const Object* ref) { |
| 770 | DCHECK(ref != NULL); |
| 771 | const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false); |
| 772 | const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false); |
| 773 | return (queue != NULL) && (queue_next == NULL); |
| 774 | } |
| 775 | |
| 776 | void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) { |
| 777 | DCHECK(ref != NULL); |
| 778 | CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL); |
| 779 | CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL); |
| 780 | EnqueuePendingReference(ref, cleared_reference_list); |
| 781 | } |
| 782 | |
| 783 | void Heap::EnqueuePendingReference(Object* ref, Object** list) { |
| 784 | DCHECK(ref != NULL); |
| 785 | DCHECK(list != NULL); |
| 786 | |
| 787 | if (*list == NULL) { |
| 788 | ref->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 789 | *list = ref; |
| 790 | } else { |
| 791 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 792 | ref->SetFieldObject(reference_pendingNext_offset_, head, false); |
| 793 | (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false); |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | Object* Heap::DequeuePendingReference(Object** list) { |
| 798 | DCHECK(list != NULL); |
| 799 | DCHECK(*list != NULL); |
| 800 | Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 801 | Object* ref; |
| 802 | if (*list == head) { |
| 803 | ref = *list; |
| 804 | *list = NULL; |
| 805 | } else { |
| 806 | Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false); |
| 807 | (*list)->SetFieldObject(reference_pendingNext_offset_, next, false); |
| 808 | ref = head; |
| 809 | } |
| 810 | ref->SetFieldObject(reference_pendingNext_offset_, NULL, false); |
| 811 | return ref; |
| 812 | } |
| 813 | |
Ian Rogers | 5d4bdc2 | 2011-11-02 22:15:43 -0700 | [diff] [blame] | 814 | void Heap::AddFinalizerReference(Thread* self, Object* object) { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 815 | ScopedThreadStateChange tsc(self, kRunnable); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 816 | static Method* FinalizerReference_add = |
| 817 | java_lang_ref_FinalizerReference_->FindDirectMethod("add", "(Ljava/lang/Object;)V"); |
| 818 | DCHECK(FinalizerReference_add != NULL); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 819 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 820 | args[0].SetL(object); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 821 | FinalizerReference_add->Invoke(self, NULL, args, NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | void Heap::EnqueueClearedReferences(Object** cleared) { |
| 825 | DCHECK(cleared != NULL); |
| 826 | if (*cleared != NULL) { |
| 827 | static Method* ReferenceQueue_add = |
| 828 | java_lang_ref_ReferenceQueue_->FindDirectMethod("add", "(Ljava/lang/ref/Reference;)V"); |
| 829 | DCHECK(ReferenceQueue_add != NULL); |
| 830 | |
| 831 | Thread* self = Thread::Current(); |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 832 | ScopedThreadStateChange tsc(self, kRunnable); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 833 | JValue args[1]; |
Elliott Hughes | f24d3ce | 2012-04-11 17:43:37 -0700 | [diff] [blame] | 834 | args[0].SetL(*cleared); |
Elliott Hughes | 7740579 | 2012-03-15 15:22:12 -0700 | [diff] [blame] | 835 | ReferenceQueue_add->Invoke(self, NULL, args, NULL); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 836 | *cleared = NULL; |
| 837 | } |
| 838 | } |
| 839 | |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 840 | void Heap::RequestHeapTrim() { |
| 841 | // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap |
| 842 | // because that only marks object heads, so a large array looks like lots of empty space. We |
| 843 | // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional |
| 844 | // to utilization (which is probably inversely proportional to how much benefit we can expect). |
| 845 | // We could try mincore(2) but that's only a measure of how many pages we haven't given away, |
| 846 | // not how much use we're making of those pages. |
| 847 | float utilization = static_cast<float>(num_bytes_allocated_) / alloc_space_->Size(); |
| 848 | if (utilization > 0.75f) { |
| 849 | // Don't bother trimming the heap if it's more than 75% utilized. |
| 850 | // (This percentage was picked arbitrarily.) |
| 851 | return; |
| 852 | } |
Ian Rogers | e1d490c | 2012-02-03 09:09:07 -0800 | [diff] [blame] | 853 | if (!Runtime::Current()->IsStarted()) { |
| 854 | // Heap trimming isn't supported without a Java runtime (such as at dex2oat time) |
| 855 | return; |
| 856 | } |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 857 | JNIEnv* env = Thread::Current()->GetJniEnv(); |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame^] | 858 | env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons, WellKnownClasses::java_lang_Daemons_requestHeapTrim); |
Elliott Hughes | 8cf5bc0 | 2012-02-02 16:32:16 -0800 | [diff] [blame] | 859 | CHECK(!env->ExceptionCheck()); |
| 860 | } |
| 861 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 862 | } // namespace art |