blob: b6536d0b77df213df49384524b2471978088750a [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
Ian Rogers5d76c432011-10-31 21:42:49 -070024#include "card_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070025#include "globals.h"
Ian Rogers30fab402012-01-23 15:43:46 -080026#include "gtest/gtest.h"
Elliott Hughes5e71b522011-10-20 13:12:32 -070027#include "heap_bitmap.h"
Brian Carlstromcd74c4b2012-01-23 13:21:00 -080028#include "mutex.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070029#include "offsets.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070030#include "safe_map.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070031
Elliott Hughes3e465b12011-09-02 18:26:12 -070032#define VERIFY_OBJECT_ENABLED 0
33
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070034// Fast verification means we do not verify the classes of objects.
35#define VERIFY_OBJECT_FAST 1
36
Carl Shapiro1fb86202011-06-27 17:43:13 -070037namespace art {
38
Ian Rogers30fab402012-01-23 15:43:46 -080039class AllocSpace;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070040class Class;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070041class HeapBitmap;
Brian Carlstromfddf6f62012-03-15 16:56:45 -070042class ImageSpace;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070043class MarkStack;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070044class ModUnionTable;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070045class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070046class Space;
Ian Rogers30fab402012-01-23 15:43:46 -080047class SpaceTest;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070048class Thread;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070049class TimingLogger;
Carl Shapiro69759ea2011-07-21 18:13:35 -070050
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070051typedef std::vector<Space*> Spaces;
52
Mathieu Chartier357e9be2012-08-01 11:00:14 -070053enum GcType {
54 // Full GC
55 GC_FULL,
56 // Sticky mark bits "generational" GC.
57 GC_STICKY,
58 // Partial GC, over only the alloc space
59 GC_PARTIAL,
60};
61
Elliott Hughesf8349362012-06-18 15:00:06 -070062class LOCKABLE Heap {
Carl Shapiro1fb86202011-06-27 17:43:13 -070063 public:
Ian Rogers30fab402012-01-23 15:43:46 -080064 static const size_t kInitialSize = 2 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070065
Ian Rogers30fab402012-01-23 15:43:46 -080066 static const size_t kMaximumSize = 32 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070067
Mathieu Chartier357e9be2012-08-01 11:00:14 -070068 // After how many GCs we force to do a partial GC instead of sticky mark bits GC.
69 static const size_t kPartialGCFrequency = 10;
70
71 // Sticky mark bits GC has some overhead, so if we have less a few megabytes of AllocSpace then
72 // it's probably better to just do a partial GC.
73 static const size_t kMinAllocSpaceSizeForStickyGC = 6 * MB;
74
75 // Minimum remaining size fo sticky GC. Since sticky GC doesn't free up as much memory as a
76 // normal GC, it is important to not use it when we are almost out of memory.
77 static const size_t kMinRemainingSpaceForStickyGC = 1 * MB;
78
Elliott Hughes410c0c82011-09-01 17:58:25 -070079 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070080 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070081
Brian Carlstrom58ae9412011-10-04 00:56:06 -070082 // Create a heap with the requested sizes. The possible empty
83 // image_file_names names specify Spaces to load based on
84 // ImageWriter output.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080085 explicit Heap(size_t starting_size, size_t growth_limit, size_t capacity,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070086 const std::string& image_file_name, bool concurrent_gc);
Carl Shapiro61e019d2011-07-14 16:53:09 -070087
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080088 ~Heap();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070089
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070090 // Allocates and initializes storage for an object instance.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070091 Object* AllocObject(Class* klass, size_t num_bytes)
92 LOCKS_EXCLUDED(statistics_lock_)
93 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070094
Elliott Hughesa2501992011-08-26 19:39:54 -070095 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070096#if VERIFY_OBJECT_ENABLED
Elliott Hughes1bac54f2012-03-16 12:48:31 -070097 void VerifyObject(const Object* o);
Elliott Hughes3e465b12011-09-02 18:26:12 -070098#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -070099 void VerifyObject(const Object*) {}
Elliott Hughes3e465b12011-09-02 18:26:12 -0700100#endif
Ian Rogers408f79a2011-08-23 18:22:33 -0700101
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700102 // Check sanity of all live references. Requires the heap lock.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800103 void VerifyHeap();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700104
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700105 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -0700106 // and doesn't abort on error, allowing the caller to report more
107 // meaningful diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800108 bool IsHeapAddress(const Object* obj);
109
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700110 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
111 // Requires the heap lock to be held.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 bool IsLiveObjectLocked(const Object* obj)
113 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700114
Carl Shapiro69759ea2011-07-21 18:13:35 -0700115 // Initiates an explicit garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 void CollectGarbage(bool clear_soft_references)
117 LOCKS_EXCLUDED(GlobalSynchronization::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700118
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700119 // Does a concurrent GC, should only be called by the GC daemon thread
120 // through runtime.
121 void ConcurrentGC();
122
Elliott Hughesbf86d042011-08-31 17:53:14 -0700123 // Implements java.lang.Runtime.maxMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800124 int64_t GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700125 // Implements java.lang.Runtime.totalMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800126 int64_t GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700127 // Implements java.lang.Runtime.freeMemory.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 int64_t GetFreeMemory() LOCKS_EXCLUDED(statistics_lock_);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700129
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700130 // Implements VMDebug.countInstancesOfClass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700131 int64_t CountInstances(Class* c, bool count_assignable)
132 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_)
133 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700134
Ian Rogers3bb17a62012-01-27 23:56:44 -0800135 // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
136 // implement dalvik.system.VMRuntime.clearGrowthLimit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800137 void ClearGrowthLimit();
jeffhaoc1160702011-10-27 15:48:45 -0700138
Ian Rogers30fab402012-01-23 15:43:46 -0800139 // Target ideal heap utilization ratio, implements
140 // dalvik.system.VMRuntime.getTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800141 float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700142 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700143 }
Ian Rogers30fab402012-01-23 15:43:46 -0800144 // Set target ideal heap utilization ratio, implements
145 // dalvik.system.VMRuntime.setTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800146 void SetTargetHeapUtilization(float target) {
Ian Rogers30fab402012-01-23 15:43:46 -0800147 DCHECK_GT(target, 0.0f); // asserted in Java code
148 DCHECK_LT(target, 1.0f);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700149 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700150 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800151
152 // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
153 // from the system. Doesn't allow the space to exceed its growth limit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800154 void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700155
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700156 // Blocks the caller until the garbage collector becomes idle and returns
157 // true if we waited for the GC to complete.
158 bool WaitForConcurrentGcToComplete();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700159
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700160 const Spaces& GetSpaces() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700161 return spaces_;
162 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700163
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800164 void SetReferenceOffsets(MemberOffset reference_referent_offset,
165 MemberOffset reference_queue_offset,
166 MemberOffset reference_queueNext_offset,
167 MemberOffset reference_pendingNext_offset,
168 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700169
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800170 Object* GetReferenceReferent(Object* reference);
171 void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700172
Elliott Hughesadb460d2011-10-05 17:02:34 -0700173 // Returns true if the reference object has not yet been enqueued.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800174 bool IsEnqueuable(const Object* ref);
175 void EnqueueReference(Object* ref, Object** list);
176 void EnqueuePendingReference(Object* ref, Object** list);
177 Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700178
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800179 MemberOffset GetReferencePendingNextOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700180 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700181 return reference_pendingNext_offset_;
182 }
183
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800184 MemberOffset GetFinalizerReferenceZombieOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700185 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700186 return finalizer_reference_zombie_offset_;
187 }
188
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800189 void EnableObjectValidation() {
Ian Rogers30fab402012-01-23 15:43:46 -0800190#if VERIFY_OBJECT_ENABLED
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800191 VerifyHeap();
Ian Rogers30fab402012-01-23 15:43:46 -0800192#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700193 verify_objects_ = true;
194 }
195
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800196 void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700197 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700198 }
199
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700200 void RecordFree(size_t freed_objects, size_t freed_bytes) LOCKS_EXCLUDED(statistics_lock_);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700201
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700202 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
203 // The call is not needed if NULL is stored in the field.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700204 void WriteBarrierField(const Object* dst, MemberOffset /*offset*/, const Object* /*new_value*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700205 if (!card_marking_disabled_) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700206 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700207 }
208 }
209
210 // Write barrier for array operations that update many field positions
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700211 void WriteBarrierArray(const Object* dst, int /*start_offset*/,
212 size_t /*length TODO: element_count or byte_count?*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700213 if (UNLIKELY(!card_marking_disabled_)) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700214 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700215 }
216 }
217
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800218 CardTable* GetCardTable() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700219 return card_table_.get();
Ian Rogers5d76c432011-10-31 21:42:49 -0700220 }
221
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800222 void DisableCardMarking() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700223 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
224 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700225 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700226
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800227 void AddFinalizerReference(Thread* self, Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700228
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 size_t GetBytesAllocated() const LOCKS_EXCLUDED(statistics_lock_);
230 size_t GetObjectsAllocated() const LOCKS_EXCLUDED(statistics_lock_);
231 size_t GetConcurrentStartSize() const LOCKS_EXCLUDED(statistics_lock_);
232 size_t GetConcurrentMinFree() const LOCKS_EXCLUDED(statistics_lock_);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700233
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700234 // Functions for getting the bitmap which corresponds to an object's address.
235 // This is probably slow, TODO: use better data structure like binary tree .
236 Space* FindSpaceFromObject(const Object*) const;
237
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700238 void DumpForSigQuit(std::ostream& os) LOCKS_EXCLUDED(statistics_lock_);
Elliott Hughesc967f782012-04-16 10:23:15 -0700239
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700240 void Trim(AllocSpace* alloc_space);
241
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 HeapBitmap* GetLiveBitmap() SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700243 return live_bitmap_.get();
244 }
245
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700246 HeapBitmap* GetMarkBitmap() SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700247 return mark_bitmap_.get();
248 }
249
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700250 void PreZygoteFork();
251
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700252 // Mark and empty stack.
253 void FlushAllocStack()
254 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
255
256 // Mark all the objects in the allocation stack as live.
257 void MarkStackAsLive(MarkStack* alloc_stack);
258
259 // Un-mark all the objects in the allocation stack.
260 void UnMarkStack(MarkStack* alloc_stack);
261
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700262 // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700263 // Assumes there is only one image space.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700264 ImageSpace* GetImageSpace();
265 AllocSpace* GetAllocSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700266 void DumpSpaces();
Elliott Hughesf8349362012-06-18 15:00:06 -0700267
Carl Shapiro58551df2011-07-24 03:09:51 -0700268 private:
269 // Allocates uninitialized storage.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700270 Object* Allocate(size_t num_bytes)
271 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
272 Object* Allocate(AllocSpace* space, size_t num_bytes)
273 LOCKS_EXCLUDED(GlobalSynchronization::thread_suspend_count_lock_)
274 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700275
Elliott Hughesadb460d2011-10-05 17:02:34 -0700276 // Pushes a list of cleared references out to the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800277 void EnqueueClearedReferences(Object** cleared_references);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700278
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800279 void RequestHeapTrim();
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700280 void RequestConcurrentGC();
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800281
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 void RecordAllocation(AllocSpace* space, const Object* object)
283 LOCKS_EXCLUDED(statistics_lock_, GlobalSynchronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700284
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700285 void CollectGarbageInternal(GcType gc_plan, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700286 LOCKS_EXCLUDED(gc_complete_lock_,
287 GlobalSynchronization::heap_bitmap_lock_,
288 GlobalSynchronization::mutator_lock_,
289 GlobalSynchronization::thread_suspend_count_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700290 void CollectGarbageMarkSweepPlan(GcType gc_plan, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700291 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_,
292 GlobalSynchronization::mutator_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700293 void CollectGarbageConcurrentMarkSweepPlan(GcType gc_plan, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700294 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_,
295 GlobalSynchronization::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700296
Ian Rogers3bb17a62012-01-27 23:56:44 -0800297 // Given the current contents of the alloc space, increase the allowed heap footprint to match
298 // the target utilization ratio. This should only be called immediately after a full garbage
299 // collection.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800300 void GrowForUtilization();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700301
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700302 size_t GetPercentFree() EXCLUSIVE_LOCKS_REQUIRED(statistics_lock_);
Elliott Hughesc967f782012-04-16 10:23:15 -0700303
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 void AddSpace(Space* space) LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800305
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700306 // No thread saftey analysis since we call this everywhere and it is impossible to find a proper
307 // lock ordering for it.
308 void VerifyObjectBody(const Object *obj)
309 NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700310
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700311 static void VerificationCallback(Object* obj, void* arg)
312 SHARED_LOCKS_REQUIRED(GlobalSychronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700313
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700314 Spaces spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700315
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700316 // The alloc space which we are currently allocating into.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800317 AllocSpace* alloc_space_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700318
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700319 // The mod-union table remembers all of the references from the image space to the alloc /
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700320 // zygote spaces.
321 UniquePtr<ModUnionTable> mod_union_table_;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700322
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700323 // This table holds all of the references from the zygote space to the alloc space.
324 UniquePtr<ModUnionTable> zygote_mod_union_table_;
325
326 UniquePtr<CardTable> card_table_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700327
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700328 // True for concurrent mark sweep GC, false for mark sweep.
329 const bool concurrent_gc_;
330
331 // If we have a zygote space.
332 bool have_zygote_space_;
333
Ian Rogers5d76c432011-10-31 21:42:49 -0700334 // Used by the image writer to disable card marking on copied objects
335 // TODO: remove
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800336 bool card_marking_disabled_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700337
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700338 // Guards access to the state of GC, associated conditional variable is used to signal when a GC
339 // completes.
340 Mutex* gc_complete_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
341 UniquePtr<ConditionVariable> gc_complete_cond_ GUARDED_BY(gc_complete_lock_);
342
Carl Shapiro58551df2011-07-24 03:09:51 -0700343 // True while the garbage collector is running.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700344 volatile bool is_gc_running_ GUARDED_BY(gc_complete_lock_);
345
346 // Guards access to heap statistics, some used to calculate when concurrent GC should occur.
347 // TODO: move bytes/objects allocated to thread-locals and remove need for lock?
348 Mutex* statistics_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700349
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700350 // Bytes until concurrent GC starts.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351 size_t concurrent_start_bytes_ GUARDED_BY(statistics_lock_);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700352 size_t concurrent_start_size_;
353 size_t concurrent_min_free_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700354 size_t sticky_gc_count_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700355
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700356 // Number of bytes allocated. Adjusted after each allocation and free.
357 size_t num_bytes_allocated_ GUARDED_BY(statistics_lock_);
358
359 // Number of objects allocated. Adjusted after each allocation and free.
360 size_t num_objects_allocated_ GUARDED_BY(statistics_lock_);
361
362 // Last trim time
363 uint64_t last_trim_time_;
364
365 UniquePtr<HeapBitmap> live_bitmap_ GUARDED_BY(GlobalSynchronization::heap_bitmap_lock_);
366 UniquePtr<HeapBitmap> mark_bitmap_ GUARDED_BY(GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700367
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700368 // True while the garbage collector is trying to signal the GC daemon thread.
369 // This flag is needed to prevent recursion from occurring when the JNI calls
370 // allocate memory and request another GC.
371 bool try_running_gc_;
372
373 // Used to ensure that we don't ever recursively request GC.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 volatile bool requesting_gc_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700375
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700376 // Mark stack that we reuse to avoid re-allocating the mark stack
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700377 UniquePtr<MarkStack> mark_stack_;
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700378
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700379 // Allocation stack, new allocations go here so that we can do sticky mark bits. This enables us
380 // to use the live bitmap as the old mark bitmap.
381 UniquePtr<MarkStack> allocation_stack_;
382
383 // Second allocation stack so that we can process allocation with the heap unlocked.
384 UniquePtr<MarkStack> live_stack_;
385
Brian Carlstrom1f870082011-08-23 16:02:11 -0700386 // offset of java.lang.ref.Reference.referent
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800387 MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700388
389 // offset of java.lang.ref.Reference.queue
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800390 MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700391
392 // offset of java.lang.ref.Reference.queueNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800393 MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700394
395 // offset of java.lang.ref.Reference.pendingNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800396 MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700397
398 // offset of java.lang.ref.FinalizerReference.zombie
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800399 MemberOffset finalizer_reference_zombie_offset_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700400
Brian Carlstrom395520e2011-09-25 19:35:00 -0700401 // Target ideal heap utilization ratio
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800402 float target_utilization_;
Brian Carlstrom395520e2011-09-25 19:35:00 -0700403
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800404 bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700405
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700406 friend class ScopedHeapLock;
Ian Rogers30fab402012-01-23 15:43:46 -0800407 FRIEND_TEST(SpaceTest, AllocAndFree);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800408 FRIEND_TEST(SpaceTest, AllocAndFreeList);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700409 FRIEND_TEST(SpaceTest, ZygoteSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800410 friend class SpaceTest;
Ian Rogers30fab402012-01-23 15:43:46 -0800411
Carl Shapiro69759ea2011-07-21 18:13:35 -0700412 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
413};
414
Carl Shapiro1fb86202011-06-27 17:43:13 -0700415} // namespace art
416
417#endif // ART_SRC_HEAP_H_