blob: 4576a9025a1998890c6dffbd4fcd9d0df1acf939 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 Shapiro69759ea2011-07-21 18:13:35 -070016
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070017#include "heap.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070018
Brian Carlstrom5643b782012-02-05 12:32:53 -080019#include <sys/types.h>
20#include <sys/wait.h>
21
Brian Carlstrom58ae9412011-10-04 00:56:06 -070022#include <limits>
Carl Shapiro58551df2011-07-24 03:09:51 -070023#include <vector>
24
Mathieu Chartier637e3482012-08-17 10:41:32 -070025#include "atomic.h"
Ian Rogers5d76c432011-10-31 21:42:49 -070026#include "card_table.h"
Elliott Hughes767a1472011-10-26 18:49:02 -070027#include "debugger.h"
Mathieu Chartiercc236d72012-07-20 10:29:05 -070028#include "heap_bitmap.h"
Brian Carlstrom9cff8e12011-08-18 16:47:29 -070029#include "image.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070030#include "mark_sweep.h"
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070031#include "mod_union_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070032#include "object.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080033#include "object_utils.h"
Brian Carlstrom5643b782012-02-05 12:32:53 -080034#include "os.h"
Mathieu Chartier7664f5c2012-06-08 18:15:32 -070035#include "ScopedLocalRef.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036#include "scoped_thread_state_change.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070037#include "space.h"
Carl Shapiro58551df2011-07-24 03:09:51 -070038#include "stl_util.h"
Elliott Hughes8d768a92011-09-14 16:35:25 -070039#include "thread_list.h"
Elliott Hughes767a1472011-10-26 18:49:02 -070040#include "timing_logger.h"
41#include "UniquePtr.h"
Elliott Hugheseac76672012-05-24 21:56:51 -070042#include "well_known_classes.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -070043
44namespace art {
45
Ian Rogers30fab402012-01-23 15:43:46 -080046static 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 Hughesae80b492012-04-24 10:43:17 -070059static bool GenerateImage(const std::string& image_file_name) {
Brian Carlstroma004aa92012-02-08 18:05:09 -080060 const std::string boot_class_path_string(Runtime::Current()->GetBootClassPathString());
Brian Carlstrom5643b782012-02-05 12:32:53 -080061 std::vector<std::string> boot_class_path;
62 Split(boot_class_path_string, ':', boot_class_path);
Brian Carlstromb2793372012-03-17 18:27:16 -070063 if (boot_class_path.empty()) {
64 LOG(FATAL) << "Failed to generate image because no boot class path specified";
65 }
Brian Carlstrom5643b782012-02-05 12:32:53 -080066
67 std::vector<char*> arg_vector;
68
69 std::string dex2oat_string(GetAndroidRoot());
Elliott Hughes67d92002012-03-26 15:08:51 -070070 dex2oat_string += (kIsDebugBuild ? "/bin/dex2oatd" : "/bin/dex2oat");
Brian Carlstrom5643b782012-02-05 12:32:53 -080071 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 Hughes48436bb2012-02-07 15:23:28 -0800101 std::string command_line(Join(arg_vector, ' '));
Brian Carlstrom5643b782012-02-05 12:32:53 -0800102 LOG(INFO) << command_line;
103
Elliott Hughes48436bb2012-02-07 15:23:28 -0800104 arg_vector.push_back(NULL);
Brian Carlstrom5643b782012-02-05 12:32:53 -0800105 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 Hughesb3bd5f02012-03-08 21:05:27 -0800137Heap::Heap(size_t initial_size, size_t growth_limit, size_t capacity,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 const std::string& original_image_file_name, bool concurrent_gc)
139 : alloc_space_(NULL),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800140 card_table_(NULL),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700141 concurrent_gc_(concurrent_gc),
142 have_zygote_space_(false),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800143 card_marking_disabled_(false),
144 is_gc_running_(false),
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700145 concurrent_start_bytes_(std::numeric_limits<size_t>::max()),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700146 concurrent_start_size_(128 * KB),
147 concurrent_min_free_(256 * KB),
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700148 sticky_gc_count_(0),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800149 num_bytes_allocated_(0),
150 num_objects_allocated_(0),
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700151 last_trim_time_(0),
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700152 try_running_gc_(false),
153 requesting_gc_(false),
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800154 reference_referent_offset_(0),
155 reference_queue_offset_(0),
156 reference_queueNext_offset_(0),
157 reference_pendingNext_offset_(0),
158 finalizer_reference_zombie_offset_(0),
159 target_utilization_(0.5),
Elliott Hughesb25c3f62012-03-26 16:35:06 -0700160 verify_objects_(false) {
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800161 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800162 LOG(INFO) << "Heap() entering";
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700163 }
164
Ian Rogers30fab402012-01-23 15:43:46 -0800165 // Compute the bounds of all spaces for allocating live and mark bitmaps
166 // there will be at least one space (the alloc space)
167 Space* first_space = NULL;
168 Space* last_space = NULL;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700169
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700170 live_bitmap_.reset(new HeapBitmap(this));
171 mark_bitmap_.reset(new HeapBitmap(this));
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700172
Ian Rogers30fab402012-01-23 15:43:46 -0800173 // Requested begin for the alloc space, to follow the mapped image and oat files
174 byte* requested_begin = NULL;
Brian Carlstrom5643b782012-02-05 12:32:53 -0800175 std::string image_file_name(original_image_file_name);
176 if (!image_file_name.empty()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700177 Space* image_space = NULL;
178
Brian Carlstrom5643b782012-02-05 12:32:53 -0800179 if (OS::FileExists(image_file_name.c_str())) {
180 // If the /system file exists, it should be up-to-date, don't try to generate
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700181 image_space = Space::CreateImageSpace(image_file_name);
Brian Carlstrom5643b782012-02-05 12:32:53 -0800182 } else {
183 // If the /system file didn't exist, we need to use one from the art-cache.
184 // If the cache file exists, try to open, but if it fails, regenerate.
185 // If it does not exist, generate.
186 image_file_name = GetArtCacheFilenameOrDie(image_file_name);
187 if (OS::FileExists(image_file_name.c_str())) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700188 image_space = Space::CreateImageSpace(image_file_name);
Brian Carlstrom5643b782012-02-05 12:32:53 -0800189 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700190 if (image_space == NULL) {
Brian Carlstrom5643b782012-02-05 12:32:53 -0800191 if (!GenerateImage(image_file_name)) {
192 LOG(FATAL) << "Failed to generate image: " << image_file_name;
193 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700194 image_space = Space::CreateImageSpace(image_file_name);
Brian Carlstrom5643b782012-02-05 12:32:53 -0800195 }
196 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700197 if (image_space == NULL) {
Brian Carlstrom223f20f2012-02-04 23:06:55 -0800198 LOG(FATAL) << "Failed to create space from " << image_file_name;
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700199 }
Brian Carlstrom5643b782012-02-05 12:32:53 -0800200
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700201 AddSpace(image_space);
202 UpdateFirstAndLastSpace(&first_space, &last_space, image_space);
Ian Rogers30fab402012-01-23 15:43:46 -0800203 // Oat files referenced by image files immediately follow them in memory, ensure alloc space
204 // isn't going to get in the middle
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700205 byte* oat_end_addr = GetImageSpace()->GetImageHeader().GetOatEnd();
206 CHECK(oat_end_addr > GetImageSpace()->End());
Ian Rogers30fab402012-01-23 15:43:46 -0800207 if (oat_end_addr > requested_begin) {
208 requested_begin = reinterpret_cast<byte*>(RoundUp(reinterpret_cast<uintptr_t>(oat_end_addr),
209 kPageSize));
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700210 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700211 }
212
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700213 UniquePtr<AllocSpace> alloc_space(Space::CreateAllocSpace(
214 "alloc space", initial_size, growth_limit, capacity, requested_begin));
215 alloc_space_ = alloc_space.release();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700216 CHECK(alloc_space_ != NULL) << "Failed to create alloc space";
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700217 AddSpace(alloc_space_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700218
Ian Rogers30fab402012-01-23 15:43:46 -0800219 UpdateFirstAndLastSpace(&first_space, &last_space, alloc_space_);
220 byte* heap_begin = first_space->Begin();
Ian Rogers3bb17a62012-01-27 23:56:44 -0800221 size_t heap_capacity = (last_space->Begin() - first_space->Begin()) + last_space->NonGrowthLimitCapacity();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700222
Ian Rogers30fab402012-01-23 15:43:46 -0800223 // Mark image objects in the live bitmap
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800224 for (size_t i = 0; i < spaces_.size(); ++i) {
Ian Rogers30fab402012-01-23 15:43:46 -0800225 Space* space = spaces_[i];
226 if (space->IsImageSpace()) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700227 space->AsImageSpace()->RecordImageAllocations(space->GetLiveBitmap());
Ian Rogers30fab402012-01-23 15:43:46 -0800228 }
229 }
230
Elliott Hughes6c9c06d2011-11-07 16:43:47 -0800231 // Allocate the card table.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700232 card_table_.reset(CardTable::Create(heap_begin, heap_capacity));
233 CHECK(card_table_.get() != NULL) << "Failed to create card table";
Ian Rogers5d76c432011-10-31 21:42:49 -0700234
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700235 mod_union_table_.reset(new ModUnionTableToZygoteAllocspace<ModUnionTableReferenceCache>(this));
236 CHECK(mod_union_table_.get() != NULL) << "Failed to create mod-union table";
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700237
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700238 zygote_mod_union_table_.reset(new ModUnionTableCardCache(this));
239 CHECK(zygote_mod_union_table_.get() != NULL) << "Failed to create Zygote mod-union table";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700240
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700241 num_bytes_allocated_ = 0;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700242 for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
243 if ((*it)->IsImageSpace()) {
244 num_bytes_allocated_ += (*it)->AsImageSpace()->Size();
245 }
246 }
247
248 // TODO: Count objects in the image space here.
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700249 num_objects_allocated_ = 0;
250
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700251 // Max stack size in bytes.
252 static const size_t max_stack_size = capacity / SpaceBitmap::kAlignment * kWordSize;
253
254 // TODO: Rename MarkStack to a more generic name?
255 mark_stack_.reset(MarkStack::Create("dalvik-mark-stack", max_stack_size));
256 allocation_stack_.reset(MarkStack::Create("dalvik-allocation-stack", max_stack_size));
257 live_stack_.reset(MarkStack::Create("dalvik-live-stack", max_stack_size));
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700258
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800259 // It's still too early to take a lock because there are no threads yet,
Elliott Hughes92b3b562011-09-08 16:32:26 -0700260 // but we can create the heap lock now. We don't create it earlier to
261 // make it clear that you can't use locks during heap initialization.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700262 gc_complete_lock_ = new Mutex("GC complete lock");
263 gc_complete_cond_.reset(new ConditionVariable("GC complete condition variable"));
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700264
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -0800265 if (VLOG_IS_ON(heap) || VLOG_IS_ON(startup)) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800266 LOG(INFO) << "Heap() exiting";
Brian Carlstrom0a5b14d2011-09-27 13:29:15 -0700267 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700268}
269
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700270// Sort spaces based on begin address
271class SpaceSorter {
272 public:
273 bool operator () (const Space* a, const Space* b) const {
274 return a->Begin() < b->Begin();
275 }
276};
277
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800278void Heap::AddSpace(Space* space) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700280 DCHECK(space != NULL);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700281 DCHECK(space->GetLiveBitmap() != NULL);
282 live_bitmap_->AddSpaceBitmap(space->GetLiveBitmap());
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700283 DCHECK(space->GetMarkBitmap() != NULL);
284 mark_bitmap_->AddSpaceBitmap(space->GetMarkBitmap());
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800285 spaces_.push_back(space);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700286 if (space->IsAllocSpace()) {
287 alloc_space_ = space->AsAllocSpace();
288 }
289
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700290 // Ensure that spaces remain sorted in increasing order of start address (required for CMS finger)
291 std::sort(spaces_.begin(), spaces_.end(), SpaceSorter());
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700292
293 // Ensure that ImageSpaces < ZygoteSpaces < AllocSpaces so that we can do address based checks to
294 // avoid redundant marking.
295 bool seen_zygote = false, seen_alloc = false;
296 for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
297 Space* space = *it;
298 if (space->IsImageSpace()) {
299 DCHECK(!seen_zygote);
300 DCHECK(!seen_alloc);
301 } if (space->IsZygoteSpace()) {
302 DCHECK(!seen_alloc);
303 seen_zygote = true;
304 } else if (space->IsAllocSpace()) {
305 seen_alloc = true;
306 }
307 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800308}
309
310Heap::~Heap() {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700311 // If we don't reset then the mark stack complains in it's destructor.
312 allocation_stack_->Reset();
313 live_stack_->Reset();
314
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800315 VLOG(heap) << "~Heap()";
Elliott Hughesb3e66df2012-01-12 14:49:18 -0800316 // We can't take the heap lock here because there might be a daemon thread suspended with the
317 // heap lock held. We know though that no non-daemon threads are executing, and we know that
318 // all daemon threads are suspended, and we also know that the threads list have been deleted, so
319 // those threads can't resume. We're the only running thread, and we can do whatever we like...
Carl Shapiro58551df2011-07-24 03:09:51 -0700320 STLDeleteElements(&spaces_);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 delete gc_complete_lock_;
322
Carl Shapiro69759ea2011-07-21 18:13:35 -0700323}
324
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700325Space* Heap::FindSpaceFromObject(const Object* obj) const {
326 // TODO: C++0x auto
327 for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) {
328 if ((*cur)->Contains(obj)) {
329 return *cur;
330 }
331 }
332 LOG(FATAL) << "object " << reinterpret_cast<const void*>(obj) << " not inside any spaces!";
333 return NULL;
334}
335
336ImageSpace* Heap::GetImageSpace() {
337 // TODO: C++0x auto
338 for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) {
339 if ((*cur)->IsImageSpace()) {
340 return (*cur)->AsImageSpace();
341 }
342 }
343 return NULL;
344}
345
346AllocSpace* Heap::GetAllocSpace() {
347 return alloc_space_;
348}
349
Elliott Hughes8a8b9cb2012-04-13 18:29:22 -0700350static void MSpaceChunkCallback(void* start, void* end, size_t used_bytes, void* arg) {
351 size_t& max_contiguous_allocation = *reinterpret_cast<size_t*>(arg);
352
353 size_t chunk_size = static_cast<size_t>(reinterpret_cast<uint8_t*>(end) - reinterpret_cast<uint8_t*>(start));
354 size_t chunk_free_bytes = 0;
355 if (used_bytes < chunk_size) {
356 chunk_free_bytes = chunk_size - used_bytes;
357 }
358
359 if (chunk_free_bytes > max_contiguous_allocation) {
360 max_contiguous_allocation = chunk_free_bytes;
361 }
362}
363
364Object* Heap::AllocObject(Class* c, size_t byte_count) {
365 // Used in the detail message if we throw an OOME.
366 int64_t total_bytes_free;
367 size_t max_contiguous_allocation;
368
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 DCHECK(c == NULL || (c->IsClassClass() && byte_count >= sizeof(Class)) ||
370 (c->IsVariableSize() || c->GetObjectSize() == byte_count) ||
371 strlen(ClassHelper(c).GetDescriptor()) == 0);
372 DCHECK_GE(byte_count, sizeof(Object));
373 Object* obj = Allocate(byte_count);
374 if (obj != NULL) {
375 obj->SetClass(c);
376 if (Dbg::IsAllocTrackingEnabled()) {
377 Dbg::RecordAllocation(c, byte_count);
Elliott Hughes418dfe72011-10-06 18:56:27 -0700378 }
Mathieu Chartier637e3482012-08-17 10:41:32 -0700379 const bool request_concurrent_gc = num_bytes_allocated_ >= concurrent_start_bytes_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700380 if (request_concurrent_gc) {
381 // The SirtRef is necessary since the calls in RequestConcurrentGC are a safepoint.
382 SirtRef<Object> ref(obj);
383 RequestConcurrentGC();
384 }
385 VerifyObject(obj);
386
387 // Additional verification to ensure that we did not allocate into a zygote space.
388 DCHECK(!have_zygote_space_ || !FindSpaceFromObject(obj)->IsZygoteSpace());
389
390 return obj;
391 }
392 total_bytes_free = GetFreeMemory();
393 max_contiguous_allocation = 0;
394 // TODO: C++0x auto
395 for (Spaces::const_iterator cur = spaces_.begin(); cur != spaces_.end(); ++cur) {
396 if ((*cur)->IsAllocSpace()) {
397 (*cur)->AsAllocSpace()->Walk(MSpaceChunkCallback, &max_contiguous_allocation);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700398 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700399 }
Elliott Hughes418dfe72011-10-06 18:56:27 -0700400
Elliott Hughes8a8b9cb2012-04-13 18:29:22 -0700401 std::string msg(StringPrintf("Failed to allocate a %zd-byte %s (%lld total bytes free; largest possible contiguous allocation %zd bytes)",
402 byte_count,
403 PrettyDescriptor(c).c_str(),
404 total_bytes_free, max_contiguous_allocation));
405 Thread::Current()->ThrowOutOfMemoryError(msg.c_str());
Elliott Hughes418dfe72011-10-06 18:56:27 -0700406 return NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -0700407}
408
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700409bool Heap::IsHeapAddress(const Object* obj) {
Elliott Hughes92b3b562011-09-08 16:32:26 -0700410 // Note: we deliberately don't take the lock here, and mustn't test anything that would
411 // require taking the lock.
Elliott Hughes88c5c352012-03-15 18:49:48 -0700412 if (obj == NULL) {
413 return true;
414 }
415 if (!IsAligned<kObjectAlignment>(obj)) {
Elliott Hughesa2501992011-08-26 19:39:54 -0700416 return false;
417 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800418 for (size_t i = 0; i < spaces_.size(); ++i) {
Ian Rogers30fab402012-01-23 15:43:46 -0800419 if (spaces_[i]->Contains(obj)) {
420 return true;
421 }
422 }
423 return false;
Elliott Hughesa2501992011-08-26 19:39:54 -0700424}
425
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700426bool Heap::IsLiveObjectLocked(const Object* obj) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700427 GlobalSynchronization::heap_bitmap_lock_->AssertReaderHeld();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700428 return IsHeapAddress(obj) && GetLiveBitmap()->Test(obj);
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700429}
430
Elliott Hughes3e465b12011-09-02 18:26:12 -0700431#if VERIFY_OBJECT_ENABLED
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700432void Heap::VerifyObject(const Object* obj) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700433 if (obj == NULL || this == NULL || !verify_objects_ || Runtime::Current()->IsShuttingDown() ||
Ian Rogers141d6222012-04-05 12:23:06 -0700434 Thread::Current() == NULL ||
jeffhao25045522012-03-13 19:34:37 -0700435 Runtime::Current()->GetThreadList()->GetLockOwner() == Thread::Current()->GetTid()) {
Elliott Hughes85d15452011-09-16 17:33:01 -0700436 return;
437 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700438 VerifyObjectBody(obj);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700439}
440#endif
441
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700442void Heap::DumpSpaces() {
443 // TODO: C++0x auto
444 for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700445 Space* space = *it;
446 LOG(INFO) << *space;
447 LOG(INFO) << *space->GetLiveBitmap();
448 LOG(INFO) << *space->GetMarkBitmap();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700449 }
450}
451
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700452// We want to avoid bit rotting.
453void Heap::VerifyObjectBody(const Object* obj) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700454 if (!IsAligned<kObjectAlignment>(obj)) {
455 LOG(FATAL) << "Object isn't aligned: " << obj;
456 } else if (!GetLiveBitmap()->Test(obj)) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700457 DumpSpaces();
458 LOG(FATAL) << "Object is dead: " << obj;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700459 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700460
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700461 // Ignore early dawn of the universe verifications
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700462 if (!VERIFY_OBJECT_FAST && num_objects_allocated_ > 10) {
Mathieu Chartierdcf8d722012-08-02 14:55:54 -0700463 const byte* raw_addr = reinterpret_cast<const byte*>(obj) +
464 Object::ClassOffset().Int32Value();
465 const Class* c = *reinterpret_cast<Class* const *>(raw_addr);
466 if (c == NULL) {
467 LOG(FATAL) << "Null class in object: " << obj;
468 } else if (!IsAligned<kObjectAlignment>(c)) {
469 LOG(FATAL) << "Class isn't aligned: " << c << " in object: " << obj;
470 } else if (!GetLiveBitmap()->Test(c)) {
471 LOG(FATAL) << "Class of object is dead: " << c << " in object: " << obj;
472 }
473 // Check obj.getClass().getClass() == obj.getClass().getClass().getClass()
474 // Note: we don't use the accessors here as they have internal sanity checks
475 // that we don't want to run
476 raw_addr = reinterpret_cast<const byte*>(c) + Object::ClassOffset().Int32Value();
477 const Class* c_c = *reinterpret_cast<Class* const *>(raw_addr);
478 raw_addr = reinterpret_cast<const byte*>(c_c) + Object::ClassOffset().Int32Value();
479 const Class* c_c_c = *reinterpret_cast<Class* const *>(raw_addr);
480 CHECK_EQ(c_c, c_c_c);
481 }
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700482}
483
Brian Carlstrom78128a62011-09-15 17:21:19 -0700484void Heap::VerificationCallback(Object* obj, void* arg) {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700485 DCHECK(obj != NULL);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700486 reinterpret_cast<Heap*>(arg)->VerifyObjectBody(obj);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700487}
488
489void Heap::VerifyHeap() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700490 ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700491 GetLiveBitmap()->Walk(Heap::VerificationCallback, this);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700492}
493
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700494void Heap::RecordAllocation(AllocSpace* space, const Object* obj) {
495 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700496 size_t size = space->AllocationSize(obj);
497 DCHECK_GT(size, 0u);
Mathieu Chartier637e3482012-08-17 10:41:32 -0700498 COMPILE_ASSERT(sizeof(size_t) == sizeof(int32_t),
499 int32_t_must_be_same_size_as_size_t_for_used_atomic_operations);
Mathieu Chartier556fad32012-08-20 16:13:20 -0700500 android_atomic_add(size, reinterpret_cast<volatile int32_t*>(
501 reinterpret_cast<size_t>(&num_bytes_allocated_)));
502 android_atomic_add(1, reinterpret_cast<volatile int32_t*>(
503 reinterpret_cast<size_t>(&num_objects_allocated_)));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700504
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700505 if (Runtime::Current()->HasStatsEnabled()) {
506 RuntimeStats* global_stats = Runtime::Current()->GetStats();
507 RuntimeStats* thread_stats = Thread::Current()->GetStats();
508 ++global_stats->allocated_objects;
509 ++thread_stats->allocated_objects;
510 global_stats->allocated_bytes += size;
511 thread_stats->allocated_bytes += size;
512 }
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700513 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700514
515 DCHECK(obj);
516
517 allocation_stack_->AtomicPush(obj);
518#if VERIFY_OBJECT_ENABLED
519 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
520 // Verify objects doesn't like objects in allocation stack not being marked as live.
521 live_bitmap_->Set(obj);
522#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700523}
524
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700525void Heap::RecordFree(size_t freed_objects, size_t freed_bytes) {
Mathieu Chartier637e3482012-08-17 10:41:32 -0700526 COMPILE_ASSERT(sizeof(size_t) == sizeof(int32_t),
527 int32_t_must_be_same_size_as_size_t_for_used_atomic_operations);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700528 DCHECK_LE(freed_objects, num_objects_allocated_);
Mathieu Chartier637e3482012-08-17 10:41:32 -0700529 android_atomic_add(-static_cast<int32_t>(freed_objects),
Mathieu Chartier556fad32012-08-20 16:13:20 -0700530 reinterpret_cast<volatile int32_t*>(
531 reinterpret_cast<size_t>(&num_objects_allocated_)));
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700532
533 DCHECK_LE(freed_bytes, num_bytes_allocated_);
Mathieu Chartier637e3482012-08-17 10:41:32 -0700534 android_atomic_add(-static_cast<int32_t>(freed_bytes),
Mathieu Chartier556fad32012-08-20 16:13:20 -0700535 reinterpret_cast<volatile int32_t*>(
536 reinterpret_cast<size_t>(&num_bytes_allocated_)));
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700537
538 if (Runtime::Current()->HasStatsEnabled()) {
539 RuntimeStats* global_stats = Runtime::Current()->GetStats();
540 RuntimeStats* thread_stats = Thread::Current()->GetStats();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700541 global_stats->freed_objects += freed_objects;
542 thread_stats->freed_objects += freed_objects;
Elliott Hughes307f75d2011-10-12 18:04:40 -0700543 global_stats->freed_bytes += freed_bytes;
544 thread_stats->freed_bytes += freed_bytes;
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700545 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700546}
547
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700548Object* Heap::Allocate(size_t size) {
549 Object* obj = Allocate(alloc_space_, size);
Carl Shapiro58551df2011-07-24 03:09:51 -0700550 if (obj != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700551 RecordAllocation(alloc_space_, obj);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700552 return obj;
Carl Shapiro58551df2011-07-24 03:09:51 -0700553 }
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700554
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700555 return NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -0700556}
557
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700558Object* Heap::Allocate(AllocSpace* space, size_t alloc_size) {
559 Thread* self = Thread::Current();
Ian Rogers0399dde2012-06-06 17:09:28 -0700560 // Since allocation can cause a GC which will need to SuspendAll, make sure all allocations are
561 // done in the runnable state where suspension is expected.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700562#ifndef NDEBUG
563 {
564 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
565 CHECK_EQ(self->GetState(), kRunnable);
566 }
567 self->AssertThreadSuspensionIsAllowable();
568#endif
Brian Carlstromb82b6872011-10-26 17:18:07 -0700569
Ian Rogers30fab402012-01-23 15:43:46 -0800570 Object* ptr = space->AllocWithoutGrowth(alloc_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700571 if (ptr != NULL) {
572 return ptr;
573 }
574
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700575 // The allocation failed. If the GC is running, block until it completes else request a
576 // foreground partial collection.
577 if (!WaitForConcurrentGcToComplete()) {
578 // No concurrent GC so perform a foreground collection.
579 if (Runtime::Current()->HasStatsEnabled()) {
580 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
581 ++Thread::Current()->GetStats()->gc_for_alloc_count;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700582 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700583 self->TransitionFromRunnableToSuspended(kWaitingPerformingGc);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700584 CollectGarbageInternal(have_zygote_space_ ? GC_PARTIAL : GC_FULL, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700585 self->TransitionFromSuspendedToRunnable();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700586 }
587
Ian Rogers30fab402012-01-23 15:43:46 -0800588 ptr = space->AllocWithoutGrowth(alloc_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700589 if (ptr != NULL) {
590 return ptr;
591 }
592
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700593 const size_t alloc_space_size = alloc_space_->Size();
594 if (alloc_space_size > kMinAllocSpaceSizeForStickyGC &&
595 alloc_space_->Capacity() - alloc_space_size < kMinRemainingSpaceForStickyGC) {
596 // Partial GC didn't free enough memory, try a full GC.
597 if (Runtime::Current()->HasStatsEnabled()) {
598 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
599 ++Thread::Current()->GetStats()->gc_for_alloc_count;
600 }
601
602 // Don't bother trying a young GC unless we have a few MB AllocSpace.
603 self->TransitionFromRunnableToSuspended(kWaitingPerformingGc);
604 CollectGarbageInternal(GC_STICKY, false);
605 self->TransitionFromSuspendedToRunnable();
606
607 ptr = space->AllocWithoutGrowth(alloc_size);
608 if (ptr != NULL) {
609 return ptr;
610 }
611 }
612
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700613 if (!have_zygote_space_) {
614 // Partial GC didn't free enough memory, try a full GC.
615 if (Runtime::Current()->HasStatsEnabled()) {
616 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
617 ++Thread::Current()->GetStats()->gc_for_alloc_count;
618 }
619 self->TransitionFromRunnableToSuspended(kWaitingPerformingGc);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700620 CollectGarbageInternal(GC_PARTIAL, false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700621 self->TransitionFromSuspendedToRunnable();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700622
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700623 ptr = space->AllocWithoutGrowth(alloc_size);
624 if (ptr != NULL) {
625 return ptr;
626 }
627 }
628
629 // Allocations have failed after GCs; this is an exceptional state.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700630 // Try harder, growing the heap if necessary.
Ian Rogers30fab402012-01-23 15:43:46 -0800631 ptr = space->AllocWithGrowth(alloc_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700632 if (ptr != NULL) {
Ian Rogers30fab402012-01-23 15:43:46 -0800633 size_t new_footprint = space->GetFootprintLimit();
Elliott Hughes418dfe72011-10-06 18:56:27 -0700634 // OLD-TODO: may want to grow a little bit more so that the amount of
Carl Shapiro58551df2011-07-24 03:09:51 -0700635 // free space is equal to the old free space + the
636 // utilization slop for the new allocation.
Ian Rogers3bb17a62012-01-27 23:56:44 -0800637 VLOG(gc) << "Grow heap (frag case) to " << PrettySize(new_footprint)
Ian Rogers162a31c2012-01-31 16:14:31 -0800638 << " for a " << PrettySize(alloc_size) << " allocation";
Carl Shapiro69759ea2011-07-21 18:13:35 -0700639 return ptr;
640 }
641
Elliott Hughes81ff3182012-03-23 20:35:56 -0700642 // Most allocations should have succeeded by now, so the heap is really full, really fragmented,
643 // or the requested size is really big. Do another GC, collecting SoftReferences this time. The
644 // VM spec requires that all SoftReferences have been collected and cleared before throwing OOME.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700645
Elliott Hughes418dfe72011-10-06 18:56:27 -0700646 // OLD-TODO: wait for the finalizers from the previous GC to finish
Ian Rogers3bb17a62012-01-27 23:56:44 -0800647 VLOG(gc) << "Forcing collection of SoftReferences for " << PrettySize(alloc_size) << " allocation";
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648
649 if (Runtime::Current()->HasStatsEnabled()) {
650 ++Runtime::Current()->GetStats()->gc_for_alloc_count;
651 ++Thread::Current()->GetStats()->gc_for_alloc_count;
652 }
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700653 // We don't need a WaitForConcurrentGcToComplete here either.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700654 self->TransitionFromRunnableToSuspended(kWaitingPerformingGc);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700655 CollectGarbageInternal(GC_FULL, true);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 self->TransitionFromSuspendedToRunnable();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700657 return space->AllocWithGrowth(alloc_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700658}
659
Elliott Hughesbf86d042011-08-31 17:53:14 -0700660int64_t Heap::GetMaxMemory() {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700661 size_t total = 0;
662 // TODO: C++0x auto
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700663 for (Spaces::const_iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
664 Space* space = *it;
665 if (space->IsAllocSpace()) {
666 total += space->AsAllocSpace()->Capacity();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700667 }
668 }
669 return total;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700670}
671
672int64_t Heap::GetTotalMemory() {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700673 return GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700674}
675
676int64_t Heap::GetFreeMemory() {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700677 return GetMaxMemory() - num_bytes_allocated_;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700678}
679
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700680class InstanceCounter {
681 public:
682 InstanceCounter(Class* c, bool count_assignable)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700683 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_)
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700684 : class_(c), count_assignable_(count_assignable), count_(0) {
685 }
686
687 size_t GetCount() {
688 return count_;
689 }
690
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700691 static void Callback(Object* o, void* arg)
692 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700693 reinterpret_cast<InstanceCounter*>(arg)->VisitInstance(o);
694 }
695
696 private:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700697 void VisitInstance(Object* o) SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700698 Class* instance_class = o->GetClass();
699 if (count_assignable_) {
700 if (instance_class == class_) {
701 ++count_;
702 }
703 } else {
704 if (instance_class != NULL && class_->IsAssignableFrom(instance_class)) {
705 ++count_;
706 }
707 }
708 }
709
710 Class* class_;
711 bool count_assignable_;
712 size_t count_;
713};
714
715int64_t Heap::CountInstances(Class* c, bool count_assignable) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700716 ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700717 InstanceCounter counter(c, count_assignable);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700718 GetLiveBitmap()->Walk(InstanceCounter::Callback, &counter);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700719 return counter.GetCount();
720}
721
Ian Rogers30fab402012-01-23 15:43:46 -0800722void Heap::CollectGarbage(bool clear_soft_references) {
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700723 // If we just waited for a GC to complete then we do not need to do another
724 // GC unless we clear soft references.
725 if (!WaitForConcurrentGcToComplete() || clear_soft_references) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700726 ScopedThreadStateChange tsc(Thread::Current(), kWaitingPerformingGc);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700727 CollectGarbageInternal(have_zygote_space_ ? GC_PARTIAL : GC_FULL, clear_soft_references);
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700728 }
Carl Shapiro69759ea2011-07-21 18:13:35 -0700729}
730
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700731void Heap::PreZygoteFork() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700732 static Mutex zygote_creation_lock_("zygote creation lock", kZygoteCreationLock);
733 MutexLock mu(zygote_creation_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700734
735 // Try to see if we have any Zygote spaces.
736 if (have_zygote_space_) {
737 return;
738 }
739
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700740 VLOG(heap) << "Starting PreZygoteFork with alloc space size " << PrettySize(alloc_space_->Size());
741
742 {
743 // Flush the alloc stack.
744 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
745 FlushAllocStack();
746 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700747
748 // Replace the first alloc space we find with a zygote space.
749 // TODO: C++0x auto
750 for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
751 if ((*it)->IsAllocSpace()) {
752 AllocSpace* zygote_space = (*it)->AsAllocSpace();
753
754 // Turns the current alloc space into a Zygote space and obtain the new alloc space composed
755 // of the remaining available heap memory.
756 alloc_space_ = zygote_space->CreateZygoteSpace();
757
758 // Change the GC retention policy of the zygote space to only collect when full.
759 zygote_space->SetGcRetentionPolicy(GCRP_FULL_COLLECT);
760 AddSpace(alloc_space_);
761 have_zygote_space_ = true;
762 break;
763 }
764 }
765}
766
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700767void Heap::FlushAllocStack() {
768 MarkStackAsLive(allocation_stack_.get());
769 allocation_stack_->Reset();
770}
771
772void Heap::MarkStackAsLive(MarkStack* alloc_stack) {
773 // We can just assume everything is inside the alloc_space_'s bitmap since we should only have
774 // fresh allocations.
775 SpaceBitmap* live_bitmap = alloc_space_->GetLiveBitmap();
776
777 // Empty the allocation stack.
778 const size_t count = alloc_stack->Size();
779 for (size_t i = 0; i < count; ++i) {
780 const Object* obj = alloc_stack->Get(i);
781 DCHECK(obj != NULL);
782 live_bitmap->Set(obj);
783 }
784}
785
786void Heap::UnMarkStack(MarkStack* alloc_stack) {
787 SpaceBitmap* mark_bitmap = alloc_space_->GetMarkBitmap();
788
789 // Clear all of the things in the AllocStack.
790 size_t count = alloc_stack->Size();
791 for (size_t i = 0;i < count;++i) {
792 const Object* obj = alloc_stack->Get(i);
793 DCHECK(obj != NULL);
794 if (mark_bitmap->Test(obj)) {
795 mark_bitmap->Clear(obj);
796 }
797 }
798}
799
800void Heap::CollectGarbageInternal(GcType gc_type, bool clear_soft_references) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700801 GlobalSynchronization::mutator_lock_->AssertNotHeld();
802#ifndef NDEBUG
803 {
804 MutexLock mu(*GlobalSynchronization::thread_suspend_count_lock_);
805 CHECK_EQ(Thread::Current()->GetState(), kWaitingPerformingGc);
806 }
807#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700808
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700809 // Ensure there is only one GC at a time.
810 bool start_collect = false;
811 while (!start_collect) {
812 {
813 MutexLock mu(*gc_complete_lock_);
814 if (!is_gc_running_) {
815 is_gc_running_ = true;
816 start_collect = true;
817 }
818 }
819 if (!start_collect) {
820 WaitForConcurrentGcToComplete();
821 // TODO: if another thread beat this one to do the GC, perhaps we should just return here?
822 // Not doing at the moment to ensure soft references are cleared.
823 }
824 }
825 gc_complete_lock_->AssertNotHeld();
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700826
827 // We need to do partial GCs every now and then to avoid the heap growing too much and
828 // fragmenting.
829 if (gc_type == GC_STICKY && ++sticky_gc_count_ > kPartialGCFrequency) {
830 gc_type = GC_PARTIAL;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700831 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700832 if (gc_type != GC_STICKY) {
833 sticky_gc_count_ = 0;
834 }
835
Mathieu Chartier637e3482012-08-17 10:41:32 -0700836 if (concurrent_gc_) {
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700837 CollectGarbageConcurrentMarkSweepPlan(gc_type, clear_soft_references);
838 } else {
839 CollectGarbageMarkSweepPlan(gc_type, clear_soft_references);
840 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700841
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700842 gc_complete_lock_->AssertNotHeld();
843 MutexLock mu(*gc_complete_lock_);
844 is_gc_running_ = false;
845 // Wake anyone who may have been waiting for the GC to complete.
846 gc_complete_cond_->Broadcast();
847}
Mathieu Chartiera6399032012-06-11 18:49:50 -0700848
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700849void Heap::CollectGarbageMarkSweepPlan(GcType gc_type, bool clear_soft_references) {
850 TimingLogger timings("CollectGarbageInternal", true);
Mathieu Chartier662618f2012-06-06 12:01:47 -0700851
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700852 // Suspend all threads are get exclusive access to the heap.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700853 uint64_t start_time = NanoTime();
Elliott Hughes8d768a92011-09-14 16:35:25 -0700854 ThreadList* thread_list = Runtime::Current()->GetThreadList();
855 thread_list->SuspendAll();
Mathieu Chartier662618f2012-06-06 12:01:47 -0700856 timings.AddSplit("SuspendAll");
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700857 GlobalSynchronization::mutator_lock_->AssertExclusiveHeld();
Elliott Hughes83df2ac2011-10-11 16:37:54 -0700858
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700859 size_t bytes_freed = 0;
Elliott Hughesadb460d2011-10-05 17:02:34 -0700860 Object* cleared_references = NULL;
Carl Shapiro58551df2011-07-24 03:09:51 -0700861 {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700862 MarkSweep mark_sweep(mark_stack_.get());
Carl Shapiro58551df2011-07-24 03:09:51 -0700863
864 mark_sweep.Init();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700865 timings.AddSplit("Init");
Carl Shapiro58551df2011-07-24 03:09:51 -0700866
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700867 // Make sure that the tables have the correct pointer for the mark sweep.
868 mod_union_table_->Init(&mark_sweep);
869 zygote_mod_union_table_->Init(&mark_sweep);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700870
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700871 // Swap allocation stack and live stack, enabling us to have new allocations during this GC.
872 MarkStack* temp = allocation_stack_.release();
873 allocation_stack_.reset(live_stack_.release());
874 live_stack_.reset(temp);
875
876 // We will need to know which cards were dirty for doing concurrent processing of dirty cards.
877 // TODO: Investigate using a mark stack instead of a vector.
878 std::vector<byte*> dirty_cards;
879 if (gc_type == GC_STICKY) {
880 for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
881 card_table_->GetDirtyCards(*it, dirty_cards);
882 }
883 }
884
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700885 // Clear image space cards and keep track of cards we cleared in the mod-union table.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700886 for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
887 Space* space = *it;
888 if (space->IsImageSpace()) {
889 mod_union_table_->ClearCards(*it);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700890 timings.AddSplit("ClearModUnionCards");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700891 } else if (space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) {
892 zygote_mod_union_table_->ClearCards(space);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700893 timings.AddSplit("ClearZygoteCards");
894 } else {
895 card_table_->ClearSpaceCards(space);
896 timings.AddSplit("ClearCards");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700897 }
898 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700899
900#if VERIFY_MOD_UNION
901 mod_union_table_->Verify();
902 zygote_mod_union_table_->Verify();
903#endif
904
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700905 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700906 if (gc_type == GC_PARTIAL) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700907 // Copy the mark bits over from the live bits, do this as early as possible or else we can
908 // accidentally un-mark roots.
909 // Needed for scanning dirty objects.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700910 for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) {
911 if ((*it)->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) {
912 mark_sweep.CopyMarkBits(*it);
913 }
914 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700915 timings.AddSplit("CopyMarkBits");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700916
917 // We can assume that everything < alloc_space_ start is marked at this point.
918 mark_sweep.SetCondemned(reinterpret_cast<Object*>(alloc_space_->Begin()));
919 } else if (gc_type == GC_STICKY) {
920 for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) {
921 if ((*it)->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) {
922 mark_sweep.CopyMarkBits(*it);
923 }
924 }
925 timings.AddSplit("CopyMarkBits");
926
927 if (VERIFY_OBJECT_ENABLED) {
928 UnMarkStack(live_stack_.get());
929 }
930
931 mark_sweep.SetCondemned(reinterpret_cast<Object*>(alloc_space_->Begin()));
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700932 }
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700933
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700934 MarkStackAsLive(live_stack_.get());
935
Carl Shapiro58551df2011-07-24 03:09:51 -0700936 mark_sweep.MarkRoots();
Elliott Hughes307f75d2011-10-12 18:04:40 -0700937 timings.AddSplit("MarkRoots");
Carl Shapiro58551df2011-07-24 03:09:51 -0700938
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700939 // Roots are marked on the bitmap and the mark_stack is empty.
Ian Rogers5d76c432011-10-31 21:42:49 -0700940 DCHECK(mark_sweep.IsMarkStackEmpty());
Carl Shapiro58551df2011-07-24 03:09:51 -0700941
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700942 // Update zygote mod union table.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700943 zygote_mod_union_table_->Update();
944 timings.AddSplit("UpdateZygoteModUnionTable");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700945
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700946 zygote_mod_union_table_->MarkReferences();
947 timings.AddSplit("ZygoteMarkReferences");
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700948
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700949 // Processes the cards we cleared earlier and adds their objects into the mod-union table.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700950 mod_union_table_->Update();
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700951 timings.AddSplit("UpdateModUnionTable");
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700952
953 // Scans all objects in the mod-union table.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700954 mod_union_table_->MarkReferences();
Mathieu Chartiere6e06512012-06-26 15:00:26 -0700955 timings.AddSplit("MarkImageToAllocSpaceReferences");
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700956
957 // Recursively mark all the non-image bits set in the mark bitmap.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700958 if (gc_type != GC_STICKY) {
959 live_stack_->Reset();
960 mark_sweep.RecursiveMark(gc_type == GC_PARTIAL, timings);
961 } else {
962 mark_sweep.RecursiveMarkCards(card_table_.get(), dirty_cards, timings);
963 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700964
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700965 // Need to process references the swap since it uses IsMarked.
Ian Rogers30fab402012-01-23 15:43:46 -0800966 mark_sweep.ProcessReferences(clear_soft_references);
Elliott Hughes307f75d2011-10-12 18:04:40 -0700967 timings.AddSplit("ProcessReferences");
Carl Shapiro58551df2011-07-24 03:09:51 -0700968
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700969 // This doesn't work with mutators unpaused for some reason, TODO: Fix.
970 mark_sweep.SweepSystemWeaks(false);
971 timings.AddSplit("SweepSystemWeaks");
972
973 // Need to swap for VERIFY_OBJECT_ENABLED since we put things in the live bitmap after they
974 // have been allocated.
975 const bool swap = true;
976
977 if (swap) {
978 // Swap the live and mark bitmaps for each alloc space. This is needed since sweep re-swaps
979 // these bitmaps. Doing this enables us to sweep with the heap unlocked since new allocations
980 // set the live bit, but since we have the bitmaps reversed at this point, this sets the mark bit
981 // instead, resulting in no new allocated objects being incorrectly freed by sweep.
982 for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
983 Space* space = *it;
984 // We only allocate into AllocSpace, so we only need to swap AllocSpaces.
985 if (space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT) {
986 live_bitmap_->ReplaceBitmap(space->GetLiveBitmap(), space->GetMarkBitmap());
987 mark_bitmap_->ReplaceBitmap(space->GetMarkBitmap(), space->GetLiveBitmap());
988 space->AsAllocSpace()->SwapBitmaps();
989 }
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700990 }
991 }
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700992
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700993#ifndef NDEBUG
Mathieu Chartier262e5ff2012-06-01 17:35:38 -0700994 // Verify that we only reach marked objects from the image space
995 mark_sweep.VerifyImageRoots();
996 timings.AddSplit("VerifyImageRoots");
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700997#endif
Carl Shapiro58551df2011-07-24 03:09:51 -0700998
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700999 if (gc_type != GC_STICKY) {
1000 mark_sweep.Sweep(gc_type == GC_PARTIAL, swap);
1001 } else {
1002 mark_sweep.SweepArray(timings, live_stack_.get(), swap);
1003 }
Elliott Hughes307f75d2011-10-12 18:04:40 -07001004 timings.AddSplit("Sweep");
Elliott Hughesadb460d2011-10-05 17:02:34 -07001005
1006 cleared_references = mark_sweep.GetClearedReferences();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001007 bytes_freed = mark_sweep.GetFreedBytes();
Carl Shapiro58551df2011-07-24 03:09:51 -07001008 }
1009
1010 GrowForUtilization();
Elliott Hughes307f75d2011-10-12 18:04:40 -07001011 timings.AddSplit("GrowForUtilization");
Mathieu Chartierb43b7d42012-06-19 13:15:09 -07001012
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001013 thread_list->ResumeAll();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001014 timings.AddSplit("ResumeAll");
Elliott Hughesadb460d2011-10-05 17:02:34 -07001015
1016 EnqueueClearedReferences(&cleared_references);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -08001017 RequestHeapTrim();
Mathieu Chartier662618f2012-06-06 12:01:47 -07001018 timings.AddSplit("Finish");
Elliott Hughes83df2ac2011-10-11 16:37:54 -07001019
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001020 // If the GC was slow, then print timings in the log.
1021 uint64_t duration = (NanoTime() - start_time) / 1000 * 1000;
1022 if (duration > MsToNs(50)) {
Mathieu Chartier637e3482012-08-17 10:41:32 -07001023 const size_t percent_free = GetPercentFree();
1024 const size_t num_bytes_allocated = num_bytes_allocated_;
1025 const size_t total_memory = GetTotalMemory();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001026 LOG(INFO) << (gc_type == GC_PARTIAL ? "Partial " : (gc_type == GC_STICKY ? "Sticky " : ""))
Mathieu Chartier637e3482012-08-17 10:41:32 -07001027 << "GC freed " << PrettySize(bytes_freed) << ", " << percent_free << "% free, "
1028 << PrettySize(num_bytes_allocated) << "/" << PrettySize(total_memory) << ", "
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001029 << "paused " << PrettyDuration(duration);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001030 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001031
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001032 if (VLOG_IS_ON(heap)) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -07001033 timings.Dump();
1034 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001035}
Mathieu Chartiera6399032012-06-11 18:49:50 -07001036
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001037void Heap::CollectGarbageConcurrentMarkSweepPlan(GcType gc_type, bool clear_soft_references) {
1038 TimingLogger timings("ConcurrentCollectGarbageInternal", true);
1039 uint64_t root_begin = NanoTime(), root_end = 0, dirty_begin = 0, dirty_end = 0;
Mathieu Chartiera6399032012-06-11 18:49:50 -07001040
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001041 // Suspend all threads are get exclusive access to the heap.
1042 ThreadList* thread_list = Runtime::Current()->GetThreadList();
1043 thread_list->SuspendAll();
1044 timings.AddSplit("SuspendAll");
1045 GlobalSynchronization::mutator_lock_->AssertExclusiveHeld();
1046
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001047 size_t bytes_freed = 0;
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001048 Object* cleared_references = NULL;
1049 {
1050 MarkSweep mark_sweep(mark_stack_.get());
1051 timings.AddSplit("ctor");
1052
1053 mark_sweep.Init();
1054 timings.AddSplit("Init");
1055
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001056 // Swap the stacks, this is safe sunce all the mutators are suspended at this point.
1057 MarkStack* temp = allocation_stack_.release();
1058 allocation_stack_.reset(live_stack_.release());
1059 live_stack_.reset(temp);
1060
1061 // We will need to know which cards were dirty for doing concurrent processing of dirty cards.
1062 // TODO: Investigate using a mark stack instead of a vector.
1063 std::vector<byte*> dirty_cards;
1064 if (gc_type == GC_STICKY) {
1065 for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
1066 card_table_->GetDirtyCards(*it, dirty_cards);
1067 }
1068 }
1069
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001070 // Make sure that the tables have the correct pointer for the mark sweep.
1071 mod_union_table_->Init(&mark_sweep);
1072 zygote_mod_union_table_->Init(&mark_sweep);
1073
1074 // Clear image space cards and keep track of cards we cleared in the mod-union table.
1075 for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
1076 Space* space = *it;
1077 if (space->IsImageSpace()) {
1078 mod_union_table_->ClearCards(*it);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001079 timings.AddSplit("ModUnionClearCards");
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001080 } else if (space->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) {
1081 zygote_mod_union_table_->ClearCards(space);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001082 timings.AddSplit("ZygoteModUnionClearCards");
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001083 } else {
1084 card_table_->ClearSpaceCards(space);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001085 timings.AddSplit("ClearCards");
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001086 }
1087 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001088
1089#if VERIFY_MOD_UNION
1090 mod_union_table_->Verify();
1091 zygote_mod_union_table_->Verify();
1092#endif
1093
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001094
1095 {
1096 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001097
1098 if (gc_type == GC_PARTIAL) {
1099 // Copy the mark bits over from the live bits, do this as early as possible or else we can
1100 // accidentally un-mark roots.
1101 // Needed for scanning dirty objects.
1102 for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) {
1103 if ((*it)->GetGcRetentionPolicy() == GCRP_FULL_COLLECT) {
1104 mark_sweep.CopyMarkBits(*it);
1105 }
1106 }
1107 timings.AddSplit("CopyMarkBits");
1108 mark_sweep.SetCondemned(reinterpret_cast<Object*>(alloc_space_->Begin()));
1109 } else if (gc_type == GC_STICKY) {
1110 for (Spaces::iterator it = spaces_.begin();it != spaces_.end(); ++it) {
1111 if ((*it)->GetGcRetentionPolicy() != GCRP_NEVER_COLLECT) {
1112 mark_sweep.CopyMarkBits(*it);
1113 }
1114 }
1115 timings.AddSplit("CopyMarkBits");
1116 // We need to unmark the new objects since we marked them as live earlier to avoid verify
1117 // objects failing.
1118 if (VERIFY_OBJECT_ENABLED) {
1119 UnMarkStack(live_stack_.get());
1120 }
1121 mark_sweep.SetCondemned(reinterpret_cast<Object*>(alloc_space_->Begin()));
1122 }
1123
1124 // TODO: Investigate whether or not this is really necessary for sticky mark bits.
1125 MarkStackAsLive(live_stack_.get());
1126
1127 if (gc_type != GC_STICKY) {
1128 live_stack_->Reset();
1129 mark_sweep.MarkRoots();
1130 timings.AddSplit("MarkRoots");
1131 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001132 }
1133
1134 // Roots are marked on the bitmap and the mark_stack is empty.
1135 DCHECK(mark_sweep.IsMarkStackEmpty());
1136
1137 // Allow mutators to go again, acquire share on mutator_lock_ to continue.
1138 thread_list->ResumeAll();
1139 {
1140 ReaderMutexLock reader_lock(*GlobalSynchronization::mutator_lock_);
1141 root_end = NanoTime();
1142 timings.AddSplit("RootEnd");
1143
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001144 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
1145 if (gc_type != GC_STICKY) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001146 // Update zygote mod union table.
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001147 if (gc_type == GC_PARTIAL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001148 zygote_mod_union_table_->Update();
1149 timings.AddSplit("UpdateZygoteModUnionTable");
1150
1151 zygote_mod_union_table_->MarkReferences();
1152 timings.AddSplit("ZygoteMarkReferences");
1153 }
1154
1155 // Processes the cards we cleared earlier and adds their objects into the mod-union table.
1156 mod_union_table_->Update();
1157 timings.AddSplit("UpdateModUnionTable");
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001158
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001159 // Scans all objects in the mod-union table.
1160 mod_union_table_->MarkReferences();
1161 timings.AddSplit("MarkImageToAllocSpaceReferences");
1162
1163 // Recursively mark all the non-image bits set in the mark bitmap.
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001164 mark_sweep.RecursiveMark(gc_type == GC_PARTIAL, timings);
1165 } else {
1166 mark_sweep.RecursiveMarkCards(card_table_.get(), dirty_cards, timings);
1167 mark_sweep.DisableFinger();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001168 }
1169 }
1170 // Release share on mutator_lock_ and then get exclusive access.
1171 dirty_begin = NanoTime();
1172 thread_list->SuspendAll();
1173 timings.AddSplit("ReSuspend");
1174 GlobalSynchronization::mutator_lock_->AssertExclusiveHeld();
1175
1176 {
1177 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001178
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001179 // Re-mark root set.
1180 mark_sweep.ReMarkRoots();
1181 timings.AddSplit("ReMarkRoots");
1182
1183 // Scan dirty objects, this is only required if we are not doing concurrent GC.
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001184 mark_sweep.RecursiveMarkDirtyObjects(false);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001185 timings.AddSplit("RecursiveMarkDirtyObjects");
1186 }
1187 {
1188 ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
1189 mark_sweep.ProcessReferences(clear_soft_references);
1190 timings.AddSplit("ProcessReferences");
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001191
1192 // This doesn't work with mutators unpaused for some reason, TODO: Fix.
1193 mark_sweep.SweepSystemWeaks(false);
1194 timings.AddSplit("SweepSystemWeaks");
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001195 }
1196 // Swap the live and mark bitmaps for each alloc space. This is needed since sweep re-swaps
1197 // these bitmaps. Doing this enables us to sweep with the heap unlocked since new allocations
1198 // set the live bit, but since we have the bitmaps reversed at this point, this sets the mark
1199 // bit instead, resulting in no new allocated objects being incorrectly freed by sweep.
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001200 bool swap = true;
1201 if (swap) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001202 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
1203 for (Spaces::iterator it = spaces_.begin(); it != spaces_.end(); ++it) {
1204 Space* space = *it;
1205 // We never allocate into zygote spaces.
1206 if (space->GetGcRetentionPolicy() == GCRP_ALWAYS_COLLECT) {
1207 live_bitmap_->ReplaceBitmap(space->GetLiveBitmap(), space->GetMarkBitmap());
1208 mark_bitmap_->ReplaceBitmap(space->GetMarkBitmap(), space->GetLiveBitmap());
1209 space->AsAllocSpace()->SwapBitmaps();
1210 }
1211 }
1212 }
1213
1214 if (kIsDebugBuild) {
1215 // Verify that we only reach marked objects from the image space.
1216 ReaderMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
1217 mark_sweep.VerifyImageRoots();
1218 timings.AddSplit("VerifyImageRoots");
1219 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001220
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001221 thread_list->ResumeAll();
1222 dirty_end = NanoTime();
1223 GlobalSynchronization::mutator_lock_->AssertNotHeld();
1224
1225 {
1226 // TODO: this lock shouldn't be necessary (it's why we did the bitmap flip above).
1227 WriterMutexLock mu(*GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001228 if (gc_type != GC_STICKY) {
1229 mark_sweep.Sweep(gc_type == GC_PARTIAL, swap);
1230 } else {
1231 mark_sweep.SweepArray(timings, live_stack_.get(), swap);
1232 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001233 timings.AddSplit("Sweep");
1234 }
1235
1236 cleared_references = mark_sweep.GetClearedReferences();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001237 bytes_freed = mark_sweep.GetFreedBytes();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001238 }
1239
1240 GrowForUtilization();
1241 timings.AddSplit("GrowForUtilization");
1242
1243 EnqueueClearedReferences(&cleared_references);
1244 RequestHeapTrim();
1245 timings.AddSplit("Finish");
1246
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001247 // If the GC was slow, then print timings in the log.
1248 uint64_t pause_roots = (root_end - root_begin) / 1000 * 1000;
1249 uint64_t pause_dirty = (dirty_end - dirty_begin) / 1000 * 1000;
Mathieu Chartier637e3482012-08-17 10:41:32 -07001250 uint64_t duration = (NanoTime() - root_begin) / 1000 * 1000;
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001251 if (pause_roots > MsToNs(5) || pause_dirty > MsToNs(5)) {
Mathieu Chartier637e3482012-08-17 10:41:32 -07001252 const size_t percent_free = GetPercentFree();
1253 const size_t num_bytes_allocated = num_bytes_allocated_;
1254 const size_t total_memory = GetTotalMemory();
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001255 LOG(INFO) << (gc_type == GC_PARTIAL ? "Partial " : (gc_type == GC_STICKY ? "Sticky " : ""))
Mathieu Chartier637e3482012-08-17 10:41:32 -07001256 << "Concurrent GC freed " << PrettySize(bytes_freed) << ", " << percent_free
1257 << "% free, " << PrettySize(num_bytes_allocated) << "/"
1258 << PrettySize(total_memory) << ", " << "paused " << PrettyDuration(pause_roots)
1259 << "+" << PrettyDuration(pause_dirty) << " total " << PrettyDuration(duration);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001260 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001261
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001262 if (VLOG_IS_ON(heap)) {
1263 timings.Dump();
1264 }
Carl Shapiro69759ea2011-07-21 18:13:35 -07001265}
1266
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -07001267bool Heap::WaitForConcurrentGcToComplete() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001268 if (concurrent_gc_) {
1269 bool do_wait = false;
1270 uint64_t wait_start;
1271 {
1272 // Check if GC is running holding gc_complete_lock_.
1273 MutexLock mu(*gc_complete_lock_);
1274 if (is_gc_running_) {
1275 wait_start = NanoTime();
1276 do_wait = true;
1277 }
Mathieu Chartiera6399032012-06-11 18:49:50 -07001278 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001279 if (do_wait) {
1280 // We must wait, change thread state then sleep on gc_complete_cond_;
1281 ScopedThreadStateChange tsc(Thread::Current(), kWaitingForGcToComplete);
1282 {
1283 MutexLock mu(*gc_complete_lock_);
1284 while (is_gc_running_) {
1285 gc_complete_cond_->Wait(*gc_complete_lock_);
1286 }
1287 }
1288 uint64_t wait_time = NanoTime() - wait_start;
1289 if (wait_time > MsToNs(5)) {
1290 LOG(INFO) << "WaitForConcurrentGcToComplete blocked for " << PrettyDuration(wait_time);
1291 }
1292 return true;
1293 }
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001294 }
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -07001295 return false;
Carl Shapiro69759ea2011-07-21 18:13:35 -07001296}
1297
Elliott Hughesc967f782012-04-16 10:23:15 -07001298void Heap::DumpForSigQuit(std::ostream& os) {
1299 os << "Heap: " << GetPercentFree() << "% free, "
1300 << PrettySize(num_bytes_allocated_) << "/" << PrettySize(GetTotalMemory())
Elliott Hughesae80b492012-04-24 10:43:17 -07001301 << "; " << num_objects_allocated_ << " objects\n";
Elliott Hughesc967f782012-04-16 10:23:15 -07001302}
1303
1304size_t Heap::GetPercentFree() {
1305 size_t total = GetTotalMemory();
1306 return 100 - static_cast<size_t>(100.0f * static_cast<float>(num_bytes_allocated_) / total);
1307}
1308
Elliott Hughes4dd9b4d2011-12-12 18:29:24 -08001309void Heap::SetIdealFootprint(size_t max_allowed_footprint) {
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001310 AllocSpace* alloc_space = alloc_space_;
1311 // TODO: Behavior for multiple alloc spaces?
1312 size_t alloc_space_capacity = alloc_space->Capacity();
1313 if (max_allowed_footprint > alloc_space_capacity) {
1314 VLOG(gc) << "Clamp target GC heap from " << PrettySize(max_allowed_footprint)
1315 << " to " << PrettySize(alloc_space_capacity);
1316 max_allowed_footprint = alloc_space_capacity;
Shih-wei Liao8c2f6412011-10-03 22:58:14 -07001317 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001318 alloc_space->SetFootprintLimit(max_allowed_footprint);
Shih-wei Liao8c2f6412011-10-03 22:58:14 -07001319}
1320
Ian Rogers3bb17a62012-01-27 23:56:44 -08001321// kHeapIdealFree is the ideal maximum free size, when we grow the heap for utilization.
Shih-wei Liao7f1caab2011-10-06 12:11:04 -07001322static const size_t kHeapIdealFree = 2 * MB;
Ian Rogers3bb17a62012-01-27 23:56:44 -08001323// kHeapMinFree guarantees that you always have at least 512 KB free, when you grow for utilization,
1324// regardless of target utilization ratio.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -07001325static const size_t kHeapMinFree = kHeapIdealFree / 4;
1326
Carl Shapiro69759ea2011-07-21 18:13:35 -07001327void Heap::GrowForUtilization() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001328 size_t target_size;
1329 bool use_footprint_limit = false;
1330 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001331 // We know what our utilization is at this moment.
1332 // This doesn't actually resize any memory. It just lets the heap grow more when necessary.
1333 target_size = num_bytes_allocated_ / Heap::GetTargetHeapUtilization();
Shih-wei Liao8c2f6412011-10-03 22:58:14 -07001334
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001335 if (target_size > num_bytes_allocated_ + kHeapIdealFree) {
1336 target_size = num_bytes_allocated_ + kHeapIdealFree;
1337 } else if (target_size < num_bytes_allocated_ + kHeapMinFree) {
1338 target_size = num_bytes_allocated_ + kHeapMinFree;
1339 }
Shih-wei Liao8c2f6412011-10-03 22:58:14 -07001340
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001341 // Calculate when to perform the next ConcurrentGC.
1342 if (GetTotalMemory() - num_bytes_allocated_ < concurrent_min_free_) {
1343 // Not enough free memory to perform concurrent GC.
1344 concurrent_start_bytes_ = std::numeric_limits<size_t>::max();
1345 } else {
1346 // Compute below to avoid holding both the statistics and the alloc space lock
1347 use_footprint_limit = true;
1348 }
Shih-wei Liao8c2f6412011-10-03 22:58:14 -07001349 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001350
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001351 if (use_footprint_limit) {
1352 size_t foot_print_limit = alloc_space_->GetFootprintLimit();
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001353 concurrent_start_bytes_ = foot_print_limit - concurrent_start_size_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001354 }
Shih-wei Liao8c2f6412011-10-03 22:58:14 -07001355 SetIdealFootprint(target_size);
Carl Shapiro69759ea2011-07-21 18:13:35 -07001356}
1357
jeffhaoc1160702011-10-27 15:48:45 -07001358void Heap::ClearGrowthLimit() {
jeffhaoc1160702011-10-27 15:48:45 -07001359 WaitForConcurrentGcToComplete();
jeffhaoc1160702011-10-27 15:48:45 -07001360 alloc_space_->ClearGrowthLimit();
1361}
1362
Elliott Hughesadb460d2011-10-05 17:02:34 -07001363void Heap::SetReferenceOffsets(MemberOffset reference_referent_offset,
1364 MemberOffset reference_queue_offset,
1365 MemberOffset reference_queueNext_offset,
1366 MemberOffset reference_pendingNext_offset,
1367 MemberOffset finalizer_reference_zombie_offset) {
1368 reference_referent_offset_ = reference_referent_offset;
1369 reference_queue_offset_ = reference_queue_offset;
1370 reference_queueNext_offset_ = reference_queueNext_offset;
1371 reference_pendingNext_offset_ = reference_pendingNext_offset;
1372 finalizer_reference_zombie_offset_ = finalizer_reference_zombie_offset;
1373 CHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
1374 CHECK_NE(reference_queue_offset_.Uint32Value(), 0U);
1375 CHECK_NE(reference_queueNext_offset_.Uint32Value(), 0U);
1376 CHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
1377 CHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
1378}
1379
1380Object* Heap::GetReferenceReferent(Object* reference) {
1381 DCHECK(reference != NULL);
1382 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
1383 return reference->GetFieldObject<Object*>(reference_referent_offset_, true);
1384}
1385
1386void Heap::ClearReferenceReferent(Object* reference) {
1387 DCHECK(reference != NULL);
1388 DCHECK_NE(reference_referent_offset_.Uint32Value(), 0U);
1389 reference->SetFieldObject(reference_referent_offset_, NULL, true);
1390}
1391
1392// Returns true if the reference object has not yet been enqueued.
1393bool Heap::IsEnqueuable(const Object* ref) {
1394 DCHECK(ref != NULL);
1395 const Object* queue = ref->GetFieldObject<Object*>(reference_queue_offset_, false);
1396 const Object* queue_next = ref->GetFieldObject<Object*>(reference_queueNext_offset_, false);
1397 return (queue != NULL) && (queue_next == NULL);
1398}
1399
1400void Heap::EnqueueReference(Object* ref, Object** cleared_reference_list) {
1401 DCHECK(ref != NULL);
1402 CHECK(ref->GetFieldObject<Object*>(reference_queue_offset_, false) != NULL);
1403 CHECK(ref->GetFieldObject<Object*>(reference_queueNext_offset_, false) == NULL);
1404 EnqueuePendingReference(ref, cleared_reference_list);
1405}
1406
1407void Heap::EnqueuePendingReference(Object* ref, Object** list) {
1408 DCHECK(ref != NULL);
1409 DCHECK(list != NULL);
1410
1411 if (*list == NULL) {
1412 ref->SetFieldObject(reference_pendingNext_offset_, ref, false);
1413 *list = ref;
1414 } else {
1415 Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
1416 ref->SetFieldObject(reference_pendingNext_offset_, head, false);
1417 (*list)->SetFieldObject(reference_pendingNext_offset_, ref, false);
1418 }
1419}
1420
1421Object* Heap::DequeuePendingReference(Object** list) {
1422 DCHECK(list != NULL);
1423 DCHECK(*list != NULL);
1424 Object* head = (*list)->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
1425 Object* ref;
1426 if (*list == head) {
1427 ref = *list;
1428 *list = NULL;
1429 } else {
1430 Object* next = head->GetFieldObject<Object*>(reference_pendingNext_offset_, false);
1431 (*list)->SetFieldObject(reference_pendingNext_offset_, next, false);
1432 ref = head;
1433 }
1434 ref->SetFieldObject(reference_pendingNext_offset_, NULL, false);
1435 return ref;
1436}
1437
Ian Rogers5d4bdc22011-11-02 22:15:43 -07001438void Heap::AddFinalizerReference(Thread* self, Object* object) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001439 ScopedObjectAccess soa(self);
Elliott Hughes77405792012-03-15 15:22:12 -07001440 JValue args[1];
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001441 args[0].SetL(object);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001442 soa.DecodeMethod(WellKnownClasses::java_lang_ref_FinalizerReference_add)->Invoke(self,
1443 NULL, args, NULL);
1444}
1445
1446size_t Heap::GetBytesAllocated() const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001447 return num_bytes_allocated_;
1448}
1449
1450size_t Heap::GetObjectsAllocated() const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001451 return num_objects_allocated_;
1452}
1453
1454size_t Heap::GetConcurrentStartSize() const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001455 return concurrent_start_size_;
1456}
1457
1458size_t Heap::GetConcurrentMinFree() const {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001459 return concurrent_min_free_;
Elliott Hughesadb460d2011-10-05 17:02:34 -07001460}
1461
1462void Heap::EnqueueClearedReferences(Object** cleared) {
1463 DCHECK(cleared != NULL);
1464 if (*cleared != NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001465 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes77405792012-03-15 15:22:12 -07001466 JValue args[1];
Elliott Hughesf24d3ce2012-04-11 17:43:37 -07001467 args[0].SetL(*cleared);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001468 soa.DecodeMethod(WellKnownClasses::java_lang_ref_ReferenceQueue_add)->Invoke(soa.Self(),
1469 NULL, args, NULL);
Elliott Hughesadb460d2011-10-05 17:02:34 -07001470 *cleared = NULL;
1471 }
1472}
1473
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001474void Heap::RequestConcurrentGC() {
Mathieu Chartier069387a2012-06-18 12:01:01 -07001475 // Make sure that we can do a concurrent GC.
1476 if (requesting_gc_ ||
1477 !Runtime::Current()->IsFinishedStarting() ||
1478 Runtime::Current()->IsShuttingDown() ||
1479 !Runtime::Current()->IsConcurrentGcEnabled()) {
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001480 return;
1481 }
1482
1483 requesting_gc_ = true;
1484 JNIEnv* env = Thread::Current()->GetJniEnv();
Mathieu Chartiera6399032012-06-11 18:49:50 -07001485 DCHECK(WellKnownClasses::java_lang_Daemons != NULL);
1486 DCHECK(WellKnownClasses::java_lang_Daemons_requestGC != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001487 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons,
1488 WellKnownClasses::java_lang_Daemons_requestGC);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001489 CHECK(!env->ExceptionCheck());
1490 requesting_gc_ = false;
1491}
1492
1493void Heap::ConcurrentGC() {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001494 if (Runtime::Current()->IsShuttingDown() || !concurrent_gc_) {
Mathieu Chartier2542d662012-06-21 17:14:11 -07001495 return;
1496 }
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001497
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001498 // TODO: We shouldn't need a WaitForConcurrentGcToComplete here since only
1499 // concurrent GC resumes threads before the GC is completed and this function
1500 // is only called within the GC daemon thread.
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001501 if (!WaitForConcurrentGcToComplete()) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001502 // Start a concurrent GC as one wasn't in progress
1503 ScopedThreadStateChange tsc(Thread::Current(), kWaitingPerformingGc);
Mathieu Chartier357e9be2012-08-01 11:00:14 -07001504 if (alloc_space_->Size() > kMinAllocSpaceSizeForStickyGC) {
1505 CollectGarbageInternal(GC_STICKY, false);
1506 } else {
1507 CollectGarbageInternal(GC_PARTIAL, false);
1508 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -07001509 }
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001510}
1511
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001512void Heap::Trim(AllocSpace* alloc_space) {
Mathieu Chartiera6399032012-06-11 18:49:50 -07001513 WaitForConcurrentGcToComplete();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -07001514 alloc_space->Trim();
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001515}
1516
Elliott Hughes8cf5bc02012-02-02 16:32:16 -08001517void Heap::RequestHeapTrim() {
1518 // We don't have a good measure of how worthwhile a trim might be. We can't use the live bitmap
1519 // because that only marks object heads, so a large array looks like lots of empty space. We
1520 // don't just call dlmalloc all the time, because the cost of an _attempted_ trim is proportional
1521 // to utilization (which is probably inversely proportional to how much benefit we can expect).
1522 // We could try mincore(2) but that's only a measure of how many pages we haven't given away,
1523 // not how much use we're making of those pages.
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001524 uint64_t ms_time = NsToMs(NanoTime());
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001525 {
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001526 float utilization = static_cast<float>(num_bytes_allocated_) / alloc_space_->Size();
1527 if ((utilization > 0.75f) || ((ms_time - last_trim_time_) < 2 * 1000)) {
1528 // Don't bother trimming the heap if it's more than 75% utilized, or if a
1529 // heap trim occurred in the last two seconds.
1530 return;
1531 }
Elliott Hughes8cf5bc02012-02-02 16:32:16 -08001532 }
Mathieu Chartiera6399032012-06-11 18:49:50 -07001533 if (!Runtime::Current()->IsFinishedStarting() || Runtime::Current()->IsShuttingDown()) {
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001534 // Heap trimming isn't supported without a Java runtime or Daemons (such as at dex2oat time)
Mathieu Chartiera6399032012-06-11 18:49:50 -07001535 // Also: we do not wish to start a heap trim if the runtime is shutting down.
Ian Rogerse1d490c2012-02-03 09:09:07 -08001536 return;
1537 }
Mathieu Chartier7664f5c2012-06-08 18:15:32 -07001538 last_trim_time_ = ms_time;
Elliott Hughes8cf5bc02012-02-02 16:32:16 -08001539 JNIEnv* env = Thread::Current()->GetJniEnv();
Mathieu Chartiera6399032012-06-11 18:49:50 -07001540 DCHECK(WellKnownClasses::java_lang_Daemons != NULL);
1541 DCHECK(WellKnownClasses::java_lang_Daemons_requestHeapTrim != NULL);
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001542 env->CallStaticVoidMethod(WellKnownClasses::java_lang_Daemons,
1543 WellKnownClasses::java_lang_Daemons_requestHeapTrim);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -08001544 CHECK(!env->ExceptionCheck());
1545}
1546
Carl Shapiro69759ea2011-07-21 18:13:35 -07001547} // namespace art