blob: a2c2d9867b0d3aec11d33950de6409cfdae8b0e7 [file] [log] [blame]
Elliott Hughes9d5ccec2011-09-19 13:19:50 -07001/*
2 * Copyright (C) 2008 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 Shapiro1fb86202011-06-27 17:43:13 -070016
17#ifndef ART_SRC_HEAP_H_
18#define ART_SRC_HEAP_H_
19
Elliott Hughesc967f782012-04-16 10:23:15 -070020#include <iosfwd>
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080021#include <string>
Carl Shapiro58551df2011-07-24 03:09:51 -070022#include <vector>
23
Mathieu Chartier2fde5332012-09-14 14:51:54 -070024#include "atomic_integer.h"
Sameer Abu Asala8439542013-02-14 16:06:42 -080025#include "base/timing_logger.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070026#include "gc/atomic_stack.h"
27#include "gc/card_table.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080028#include "gc/gc_type.h"
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070029#include "gc/heap_bitmap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030#include "globals.h"
Ian Rogers30fab402012-01-23 15:43:46 -080031#include "gtest/gtest.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070032#include "locks.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070033#include "offsets.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070034#include "safe_map.h"
Mathieu Chartier02b6a782012-10-26 13:51:26 -070035#include "thread_pool.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070036
37namespace art {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038namespace mirror {
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070039class Class;
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080040class Object;
41} // namespace mirror
42class AllocSpace;
Ian Rogers81d425b2012-09-27 16:03:43 -070043class ConditionVariable;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -070044class DlMallocSpace;
Mathieu Chartier2b82db42012-11-14 17:29:05 -080045class GarbageCollector;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070046class HeapBitmap;
Brian Carlstromfddf6f62012-03-15 16:56:45 -070047class ImageSpace;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070048class LargeObjectSpace;
Mathieu Chartier7469ebf2012-09-24 16:28:36 -070049class MarkSweep;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070050class ModUnionTable;
Ian Rogers81d425b2012-09-27 16:03:43 -070051class Mutex;
Carl Shapiro69759ea2011-07-21 18:13:35 -070052class Space;
Ian Rogers30fab402012-01-23 15:43:46 -080053class SpaceTest;
Ian Rogers40e3bac2012-11-20 00:09:14 -080054class StackVisitor;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070055class Thread;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070056class TimingLogger;
Carl Shapiro69759ea2011-07-21 18:13:35 -070057
Mathieu Chartier2fde5332012-09-14 14:51:54 -070058typedef std::vector<ContinuousSpace*> Spaces;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070059
Mathieu Chartierd22d5482012-11-06 17:14:12 -080060class AgeCardVisitor {
61 public:
62 byte operator ()(byte card) const {
63 if (card == CardTable::kCardDirty) {
64 return card - 1;
65 } else {
66 return 0;
67 }
68 }
69};
70
Mathieu Chartier2fde5332012-09-14 14:51:54 -070071enum GcCause {
72 kGcCauseForAlloc,
73 kGcCauseBackground,
74 kGcCauseExplicit,
75};
76std::ostream& operator<<(std::ostream& os, const GcCause& policy);
77
Ian Rogers04d7aa92013-03-16 14:29:17 -070078// How we want to sanity check the heap's correctness.
79enum HeapVerificationMode {
80 kHeapVerificationNotPermitted, // Too early in runtime start-up for heap to be verified.
81 kNoHeapVerification, // Production default.
82 kVerifyAllFast, // Sanity check all heap accesses with quick(er) tests.
83 kVerifyAll // Sanity check all heap accesses.
84};
85const HeapVerificationMode kDesiredHeapVerification = kNoHeapVerification;
86
Ian Rogers50b35e22012-10-04 10:09:15 -070087class Heap {
Carl Shapiro1fb86202011-06-27 17:43:13 -070088 public:
Mathieu Chartier0051be62012-10-12 17:47:11 -070089 static const size_t kDefaultInitialSize = 2 * MB;
90 static const size_t kDefaultMaximumSize = 32 * MB;
91 static const size_t kDefaultMaxFree = 2 * MB;
92 static const size_t kDefaultMinFree = kDefaultMaxFree / 4;
93
94 // Default target utilization.
95 static const double kDefaultTargetUtilization;
96
Mathieu Chartier155dfe92012-10-09 14:24:49 -070097 // Used so that we don't overflow the allocation time atomic integer.
98 static const size_t kTimeAdjust = 1024;
Carl Shapiro69759ea2011-07-21 18:13:35 -070099
Brian Carlstrom58ae9412011-10-04 00:56:06 -0700100 // Create a heap with the requested sizes. The possible empty
101 // image_file_names names specify Spaces to load based on
102 // ImageWriter output.
Mathieu Chartier0051be62012-10-12 17:47:11 -0700103 explicit Heap(size_t initial_size, size_t growth_limit, size_t min_free,
104 size_t max_free, double target_utilization, size_t capacity,
105 const std::string& original_image_file_name, bool concurrent_gc);
Carl Shapiro61e019d2011-07-14 16:53:09 -0700106
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800107 ~Heap();
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700108
Brian Carlstroma40f9bc2011-07-26 21:26:07 -0700109 // Allocates and initializes storage for an object instance.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mirror::Object* AllocObject(Thread* self, mirror::Class* klass, size_t num_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700112
Ian Rogers04d7aa92013-03-16 14:29:17 -0700113 // The given reference is believed to be to an object in the Java heap, check the soundness of it.
114 void VerifyObjectImpl(const mirror::Object* o);
115 void VerifyObject(const mirror::Object* o) {
116 if (o != NULL && this != NULL && verify_object_mode_ > kNoHeapVerification) {
117 VerifyObjectImpl(o);
118 }
119 }
Ian Rogers408f79a2011-08-23 18:22:33 -0700120
Ian Rogers04d7aa92013-03-16 14:29:17 -0700121 // Check sanity of all live references.
Ian Rogersf0bbeab2012-10-10 18:26:27 -0700122 void VerifyHeap() LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 static void RootMatchesObjectVisitor(const mirror::Object* root, void* arg);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700124 bool VerifyHeapReferences()
125 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
126 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
127 bool VerifyMissingCardMarks()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700128 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
129 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700131 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -0700132 // and doesn't abort on error, allowing the caller to report more
133 // meaningful diagnostics.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800134 bool IsHeapAddress(const mirror::Object* obj);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800135
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700136 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
137 // Requires the heap lock to be held.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800138 bool IsLiveObjectLocked(const mirror::Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700139 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700140
Carl Shapiro69759ea2011-07-21 18:13:35 -0700141 // Initiates an explicit garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142 void CollectGarbage(bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700143 LOCKS_EXCLUDED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700144
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700145 // Does a concurrent GC, should only be called by the GC daemon thread
146 // through runtime.
Ian Rogersf0bbeab2012-10-10 18:26:27 -0700147 void ConcurrentGC(Thread* self) LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700148
Elliott Hughesbf86d042011-08-31 17:53:14 -0700149 // Implements java.lang.Runtime.maxMemory.
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700150 int64_t GetMaxMemory() const;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700151 // Implements java.lang.Runtime.totalMemory.
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700152 int64_t GetTotalMemory() const;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700153 // Implements java.lang.Runtime.freeMemory.
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700154 int64_t GetFreeMemory() const;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700155
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800156 // Implements VMDebug.countInstancesOfClass and JDWP VM_InstanceCount.
157 // The boolean decides whether to use IsAssignableFrom or == when comparing classes.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800158 void CountInstances(const std::vector<mirror::Class*>& classes, bool use_is_assignable_from,
Elliott Hughesec0f83d2013-01-15 16:54:08 -0800159 uint64_t* counts)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700160 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
161 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes3b78c942013-01-15 17:35:41 -0800162 // Implements JDWP RT_Instances.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800163 void GetInstances(mirror::Class* c, int32_t max_count, std::vector<mirror::Object*>& instances)
Elliott Hughes3b78c942013-01-15 17:35:41 -0800164 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
165 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800166 // Implements JDWP OR_ReferringObjects.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800167 void GetReferringObjects(mirror::Object* o, int32_t max_count, std::vector<mirror::Object*>& referring_objects)
Elliott Hughes0cbaff52013-01-16 15:28:01 -0800168 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
169 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700170
Ian Rogers3bb17a62012-01-27 23:56:44 -0800171 // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
172 // implement dalvik.system.VMRuntime.clearGrowthLimit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800173 void ClearGrowthLimit();
jeffhaoc1160702011-10-27 15:48:45 -0700174
Ian Rogers30fab402012-01-23 15:43:46 -0800175 // Target ideal heap utilization ratio, implements
176 // dalvik.system.VMRuntime.getTargetHeapUtilization.
Mathieu Chartier0051be62012-10-12 17:47:11 -0700177 double GetTargetHeapUtilization() const {
178 return target_utilization_;
179 }
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700180
Ian Rogers30fab402012-01-23 15:43:46 -0800181 // Set target ideal heap utilization ratio, implements
182 // dalvik.system.VMRuntime.setTargetHeapUtilization.
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700183 void SetTargetHeapUtilization(float target);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800184
185 // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
186 // from the system. Doesn't allow the space to exceed its growth limit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800187 void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700188
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700189 // Blocks the caller until the garbage collector becomes idle and returns
190 // true if we waited for the GC to complete.
Ian Rogersf0bbeab2012-10-10 18:26:27 -0700191 GcType WaitForConcurrentGcToComplete(Thread* self) LOCKS_EXCLUDED(gc_complete_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700192
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800193 const Spaces& GetSpaces() const {
194 return spaces_;
195 }
196
197 Spaces& GetSpaces() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700198 return spaces_;
199 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700200
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800201 void SetReferenceOffsets(MemberOffset reference_referent_offset,
202 MemberOffset reference_queue_offset,
203 MemberOffset reference_queueNext_offset,
204 MemberOffset reference_pendingNext_offset,
205 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700206
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800207 mirror::Object* GetReferenceReferent(mirror::Object* reference);
208 void ClearReferenceReferent(mirror::Object* reference) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700209
Elliott Hughesadb460d2011-10-05 17:02:34 -0700210 // Returns true if the reference object has not yet been enqueued.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800211 bool IsEnqueuable(const mirror::Object* ref);
212 void EnqueueReference(mirror::Object* ref, mirror::Object** list) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
213 void EnqueuePendingReference(mirror::Object* ref, mirror::Object** list)
Ian Rogers23435d02012-09-24 11:23:12 -0700214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 mirror::Object* DequeuePendingReference(mirror::Object** list) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700216
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800217 MemberOffset GetReferencePendingNextOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700218 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700219 return reference_pendingNext_offset_;
220 }
221
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800222 MemberOffset GetFinalizerReferenceZombieOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700223 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700224 return finalizer_reference_zombie_offset_;
225 }
226
Ian Rogers04d7aa92013-03-16 14:29:17 -0700227 // Enable verification of object references when the runtime is sufficiently initialized.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800228 void EnableObjectValidation() {
Ian Rogers04d7aa92013-03-16 14:29:17 -0700229 verify_object_mode_ = kDesiredHeapVerification;
230 if (verify_object_mode_ > kNoHeapVerification) {
231 VerifyHeap();
232 }
Elliott Hughes85d15452011-09-16 17:33:01 -0700233 }
234
Ian Rogers04d7aa92013-03-16 14:29:17 -0700235 // Disable object reference verification for image writing.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800236 void DisableObjectValidation() {
Ian Rogers04d7aa92013-03-16 14:29:17 -0700237 verify_object_mode_ = kHeapVerificationNotPermitted;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700238 }
239
Ian Rogers04d7aa92013-03-16 14:29:17 -0700240 // Other checks may be performed if we know the heap should be in a sane state.
Ian Rogers23435d02012-09-24 11:23:12 -0700241 bool IsObjectValidationEnabled() const {
Ian Rogers04d7aa92013-03-16 14:29:17 -0700242 return kDesiredHeapVerification > kNoHeapVerification &&
243 verify_object_mode_ > kHeapVerificationNotPermitted;
Ian Rogers23435d02012-09-24 11:23:12 -0700244 }
245
Mathieu Chartier037813d2012-08-23 16:44:59 -0700246 void RecordFree(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700247
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700248 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
249 // The call is not needed if NULL is stored in the field.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800250 void WriteBarrierField(const mirror::Object* dst, MemberOffset /*offset*/, const mirror::Object* /*new_value*/) {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700251 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700252 }
253
254 // Write barrier for array operations that update many field positions
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800255 void WriteBarrierArray(const mirror::Object* dst, int /*start_offset*/,
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700256 size_t /*length TODO: element_count or byte_count?*/) {
Mathieu Chartiere35517a2012-10-30 18:49:55 -0700257 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700258 }
259
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800260 CardTable* GetCardTable() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700261 return card_table_.get();
Ian Rogers5d76c432011-10-31 21:42:49 -0700262 }
263
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800264 void AddFinalizerReference(Thread* self, mirror::Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700265
Mathieu Chartier037813d2012-08-23 16:44:59 -0700266 size_t GetBytesAllocated() const;
267 size_t GetObjectsAllocated() const;
268 size_t GetConcurrentStartSize() const;
269 size_t GetConcurrentMinFree() const;
270 size_t GetUsedMemorySize() const;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700271
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700272 // Returns the total number of objects allocated since the heap was created.
273 size_t GetTotalObjectsAllocated() const;
274
275 // Returns the total number of bytes allocated since the heap was created.
276 size_t GetTotalBytesAllocated() const;
277
278 // Returns the total number of objects freed since the heap was created.
279 size_t GetTotalObjectsFreed() const;
280
281 // Returns the total number of bytes freed since the heap was created.
282 size_t GetTotalBytesFreed() const;
283
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700284 // Functions for getting the bitmap which corresponds to an object's address.
285 // This is probably slow, TODO: use better data structure like binary tree .
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 ContinuousSpace* FindSpaceFromObject(const mirror::Object*) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700287
Mathieu Chartier037813d2012-08-23 16:44:59 -0700288 void DumpForSigQuit(std::ostream& os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700289
Ian Rogers48931882013-01-22 14:35:16 -0800290 size_t Trim();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700291
Ian Rogersb726dcb2012-09-05 08:57:23 -0700292 HeapBitmap* GetLiveBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700293 return live_bitmap_.get();
294 }
295
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 HeapBitmap* GetMarkBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700297 return mark_bitmap_.get();
298 }
299
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800300 ObjectStack* GetLiveStack() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
301 return live_stack_.get();
302 }
303
Ian Rogersf0bbeab2012-10-10 18:26:27 -0700304 void PreZygoteFork() LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700305
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700306 // Mark and empty stack.
307 void FlushAllocStack()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700308 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700309
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700310 // Mark all the objects in the allocation stack in the specified bitmap.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700311 void MarkAllocStack(SpaceBitmap* bitmap, SpaceSetMap* large_objects, ObjectStack* stack)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700312 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700313
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700314 // Unmark all the objects in the allocation stack in the specified bitmap.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700315 void UnMarkAllocStack(SpaceBitmap* bitmap, SpaceSetMap* large_objects, ObjectStack* stack)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700316 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700317
318 // Update and mark mod union table based on gc type.
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700319 void UpdateAndMarkModUnion(MarkSweep* mark_sweep, TimingLogger& timings, GcType gc_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700320 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700321
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700322 // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700323 // Assumes there is only one image space.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700324 ImageSpace* GetImageSpace();
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700325 DlMallocSpace* GetAllocSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700326 LargeObjectSpace* GetLargeObjectsSpace() {
327 return large_object_space_.get();
328 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700329 void DumpSpaces();
Elliott Hughesf8349362012-06-18 15:00:06 -0700330
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700331 // UnReserve the address range where the oat file will be placed.
332 void UnReserveOatFileAddressRange();
333
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700334 // GC performance measuring
335 void DumpGcPerformanceInfo();
336
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700337 // Thread pool.
338 void CreateThreadPool();
339 void DeleteThreadPool();
340 ThreadPool* GetThreadPool() {
341 return thread_pool_.get();
342 }
343
Carl Shapiro58551df2011-07-24 03:09:51 -0700344 private:
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700345 // Allocates uninitialized storage. Passing in a null space tries to place the object in the
346 // large object space.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800347 mirror::Object* Allocate(Thread* self, AllocSpace* space, size_t num_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700348 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
349 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700350
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700351 // Try to allocate a number of bytes, this function never does any GCs.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800352 mirror::Object* TryToAllocate(Thread* self, AllocSpace* space, size_t alloc_size, bool grow)
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700353 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
354 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
355
Elliott Hughesadb460d2011-10-05 17:02:34 -0700356 // Pushes a list of cleared references out to the managed heap.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800357 void EnqueueClearedReferences(mirror::Object** cleared_references);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700358
Ian Rogersf0bbeab2012-10-10 18:26:27 -0700359 void RequestHeapTrim() LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_);
360 void RequestConcurrentGC(Thread* self) LOCKS_EXCLUDED(Locks::runtime_shutdown_lock_);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800361
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800362 void RecordAllocation(size_t size, mirror::Object* object)
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700363 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_)
364 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700365
Mathieu Chartier866fb2a2012-09-10 10:47:49 -0700366 // Sometimes CollectGarbageInternal decides to run a different Gc than you requested. Returns
367 // which type of Gc was actually ran.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700368 GcType CollectGarbageInternal(GcType gc_plan, GcCause gc_cause, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700369 LOCKS_EXCLUDED(gc_complete_lock_,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700370 Locks::heap_bitmap_lock_,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700371 Locks::thread_suspend_count_lock_);
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800372
373 void PreGcVerification(GarbageCollector* gc);
374 void PreSweepingGcVerification(GarbageCollector* gc);
375 void PostGcVerification(GarbageCollector* gc);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700376
Ian Rogers3bb17a62012-01-27 23:56:44 -0800377 // Given the current contents of the alloc space, increase the allowed heap footprint to match
378 // the target utilization ratio. This should only be called immediately after a full garbage
379 // collection.
Mathieu Chartier65db8802012-11-20 12:36:46 -0800380 void GrowForUtilization(uint64_t gc_duration);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700381
Mathieu Chartier637e3482012-08-17 10:41:32 -0700382 size_t GetPercentFree();
Elliott Hughesc967f782012-04-16 10:23:15 -0700383
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700384 void AddSpace(ContinuousSpace* space) LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700385
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700386 // No thread saftey analysis since we call this everywhere and it is impossible to find a proper
387 // lock ordering for it.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800388 void VerifyObjectBody(const mirror::Object *obj) NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700389
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 static void VerificationCallback(mirror::Object* obj, void* arg)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700391 SHARED_LOCKS_REQUIRED(GlobalSychronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700392
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700393 // Swap the allocation stack with the live stack.
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700394 void SwapStacks();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700395
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700396 // Clear cards and update the mod union table.
Mathieu Chartierd22d5482012-11-06 17:14:12 -0800397 void ProcessCards(TimingLogger& timings);
Mathieu Chartier7469ebf2012-09-24 16:28:36 -0700398
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700399 Spaces spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700400
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700401 // A map that we use to temporarily reserve address range for the oat file.
402 UniquePtr<MemMap> oat_file_map_;
403
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700404 // The alloc space which we are currently allocating into.
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700405 DlMallocSpace* alloc_space_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700406
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700407 // The mod-union table remembers all of the references from the image space to the alloc /
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700408 // zygote spaces.
409 UniquePtr<ModUnionTable> mod_union_table_;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700410
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700411 // This table holds all of the references from the zygote space to the alloc space.
412 UniquePtr<ModUnionTable> zygote_mod_union_table_;
413
414 UniquePtr<CardTable> card_table_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700415
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416 // True for concurrent mark sweep GC, false for mark sweep.
417 const bool concurrent_gc_;
418
419 // If we have a zygote space.
420 bool have_zygote_space_;
421
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700422 // Guards access to the state of GC, associated conditional variable is used to signal when a GC
423 // completes.
424 Mutex* gc_complete_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
425 UniquePtr<ConditionVariable> gc_complete_cond_ GUARDED_BY(gc_complete_lock_);
426
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700427 // Reference queue lock
428 UniquePtr<Mutex> reference_queue_lock_;
429
Carl Shapiro58551df2011-07-24 03:09:51 -0700430 // True while the garbage collector is running.
Mathieu Chartier866fb2a2012-09-10 10:47:49 -0700431 volatile bool is_gc_running_ GUARDED_BY(gc_complete_lock_);
432
433 // Last Gc type we ran. Used by WaitForConcurrentGc to know which Gc was waited on.
434 volatile GcType last_gc_type_ GUARDED_BY(gc_complete_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700435
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700436 // If enabled, causes Gc for alloc when heap size reaches the current footprint limit before the
437 // Gc updates it.
438 const bool enforce_heap_growth_rate_;
439
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700440 // Maximum size that the heap can reach.
Mathieu Chartier80de7a62012-11-27 17:21:50 -0800441 size_t capacity_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700442 size_t growth_limit_;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700443 size_t max_allowed_footprint_;
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700444
Mathieu Chartier65db8802012-11-20 12:36:46 -0800445 // Minimum bytes before concurrent GC starts.
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700446 size_t concurrent_start_size_;
447 size_t concurrent_min_free_;
Mathieu Chartier0051be62012-10-12 17:47:11 -0700448 size_t concurrent_start_bytes_;
Mathieu Chartier1c23e1e2012-10-12 14:14:11 -0700449
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700450 // Number of bytes allocated since the last Gc, we use this to help determine when to schedule concurrent GCs.
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700451 size_t sticky_gc_count_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700452
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700453 size_t total_bytes_freed_;
454 size_t total_objects_freed_;
455
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700456 // Primitive objects larger than this size are put in the large object space.
457 size_t large_object_threshold_;
458
459 // Large object space.
460 UniquePtr<LargeObjectSpace> large_object_space_;
461
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700462 // Number of bytes allocated. Adjusted after each allocation and free.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700463 AtomicInteger num_bytes_allocated_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700464
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700465 // Heap verification flags.
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700466 const bool verify_missing_card_marks_;
467 const bool verify_system_weaks_;
468 const bool verify_pre_gc_heap_;
469 const bool verify_post_gc_heap_;
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700470 const bool verify_mod_union_table_;
471
Mathieu Chartier02b6a782012-10-26 13:51:26 -0700472 // Parallel GC data structures.
473 UniquePtr<ThreadPool> thread_pool_;
474
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700475 // After how many GCs we force to do a partial GC instead of sticky mark bits GC.
476 const size_t partial_gc_frequency_;
477
478 // Sticky mark bits GC has some overhead, so if we have less a few megabytes of AllocSpace then
479 // it's probably better to just do a partial GC.
480 const size_t min_alloc_space_size_for_sticky_gc_;
481
482 // Minimum remaining size for sticky GC. Since sticky GC doesn't free up as much memory as a
483 // normal GC, it is important to not use it when we are almost out of memory.
484 const size_t min_remaining_space_for_sticky_gc_;
485
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700486 // Last trim time
487 uint64_t last_trim_time_;
488
Mathieu Chartier65db8802012-11-20 12:36:46 -0800489 // The time at which the last GC ended.
490 uint64_t last_gc_time_;
491
492 // How many bytes were allocated at the end of the last GC.
493 uint64_t last_gc_size_;
494
495 // Estimated allocation rate (bytes / second).
496 uint64_t allocation_rate_;
497
Ian Rogersb726dcb2012-09-05 08:57:23 -0700498 UniquePtr<HeapBitmap> live_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
499 UniquePtr<HeapBitmap> mark_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700500
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700501 // Mark stack that we reuse to avoid re-allocating the mark stack.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700502 UniquePtr<ObjectStack> mark_stack_;
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700503
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700504 // Allocation stack, new allocations go here so that we can do sticky mark bits. This enables us
505 // to use the live bitmap as the old mark bitmap.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700506 const size_t max_allocation_stack_size_;
507 UniquePtr<ObjectStack> allocation_stack_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700508
509 // Second allocation stack so that we can process allocation with the heap unlocked.
Mathieu Chartierd8195f12012-10-05 12:21:28 -0700510 UniquePtr<ObjectStack> live_stack_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700511
Brian Carlstrom1f870082011-08-23 16:02:11 -0700512 // offset of java.lang.ref.Reference.referent
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800513 MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700514
515 // offset of java.lang.ref.Reference.queue
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800516 MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700517
518 // offset of java.lang.ref.Reference.queueNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800519 MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700520
521 // offset of java.lang.ref.Reference.pendingNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800522 MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700523
524 // offset of java.lang.ref.FinalizerReference.zombie
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800525 MemberOffset finalizer_reference_zombie_offset_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700526
Mathieu Chartier0051be62012-10-12 17:47:11 -0700527 // Minimum free guarantees that you always have at least min_free_ free bytes after growing for
528 // utilization, regardless of target utilization ratio.
529 size_t min_free_;
530
531 // The ideal maximum free size, when we grow the heap for utilization.
532 size_t max_free_;
533
Brian Carlstrom395520e2011-09-25 19:35:00 -0700534 // Target ideal heap utilization ratio
Mathieu Chartier0051be62012-10-12 17:47:11 -0700535 double target_utilization_;
Brian Carlstrom395520e2011-09-25 19:35:00 -0700536
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700537 // Total time which mutators are paused or waiting for GC to complete.
Mathieu Chartier155dfe92012-10-09 14:24:49 -0700538 uint64_t total_wait_time_;
539
540 // Total number of objects allocated in microseconds.
541 const bool measure_allocation_time_;
542 AtomicInteger total_allocation_time_;
543
Ian Rogers04d7aa92013-03-16 14:29:17 -0700544 // The current state of heap verification, may be enabled or disabled.
545 HeapVerificationMode verify_object_mode_;
546
Mathieu Chartier2b82db42012-11-14 17:29:05 -0800547 typedef std::vector<MarkSweep*> Collectors;
548 Collectors mark_sweep_collectors_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700549
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700550 friend class MarkSweep;
551 friend class VerifyReferenceCardVisitor;
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700552 friend class VerifyReferenceVisitor;
553 friend class VerifyObjectVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700554 friend class ScopedHeapLock;
Ian Rogers30fab402012-01-23 15:43:46 -0800555 FRIEND_TEST(SpaceTest, AllocAndFree);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800556 FRIEND_TEST(SpaceTest, AllocAndFreeList);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700557 FRIEND_TEST(SpaceTest, ZygoteSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800558 friend class SpaceTest;
Ian Rogers30fab402012-01-23 15:43:46 -0800559
Carl Shapiro69759ea2011-07-21 18:13:35 -0700560 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
561};
562
Carl Shapiro1fb86202011-06-27 17:43:13 -0700563} // namespace art
564
565#endif // ART_SRC_HEAP_H_