blob: 23f2ac3c37d2fa7a693ed673effad238319ebceb [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};
Mathieu Chartierfd678be2012-08-30 14:50:54 -070061std::ostream& operator<<(std::ostream& os, const GcType& policy);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070062
Elliott Hughesf8349362012-06-18 15:00:06 -070063class LOCKABLE Heap {
Carl Shapiro1fb86202011-06-27 17:43:13 -070064 public:
Ian Rogers30fab402012-01-23 15:43:46 -080065 static const size_t kInitialSize = 2 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070066
Ian Rogers30fab402012-01-23 15:43:46 -080067 static const size_t kMaximumSize = 32 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070068
Mathieu Chartier357e9be2012-08-01 11:00:14 -070069 // After how many GCs we force to do a partial GC instead of sticky mark bits GC.
70 static const size_t kPartialGCFrequency = 10;
71
72 // Sticky mark bits GC has some overhead, so if we have less a few megabytes of AllocSpace then
73 // it's probably better to just do a partial GC.
74 static const size_t kMinAllocSpaceSizeForStickyGC = 6 * MB;
75
76 // Minimum remaining size fo sticky GC. Since sticky GC doesn't free up as much memory as a
77 // normal GC, it is important to not use it when we are almost out of memory.
78 static const size_t kMinRemainingSpaceForStickyGC = 1 * MB;
79
Elliott Hughes410c0c82011-09-01 17:58:25 -070080 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070081 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070082
Brian Carlstrom58ae9412011-10-04 00:56:06 -070083 // Create a heap with the requested sizes. The possible empty
84 // image_file_names names specify Spaces to load based on
85 // ImageWriter output.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080086 explicit Heap(size_t starting_size, size_t growth_limit, size_t capacity,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 const std::string& image_file_name, bool concurrent_gc);
Carl Shapiro61e019d2011-07-14 16:53:09 -070088
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080089 ~Heap();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070090
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070091 // Allocates and initializes storage for an object instance.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 Object* AllocObject(Class* klass, size_t num_bytes)
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 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();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700104 static void RootMatchesObjectVisitor(const Object* root, void* arg);
105 void VerifyHeapReferences(const std::string& phase)
106 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_)
107 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700108
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700109 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -0700110 // and doesn't abort on error, allowing the caller to report more
111 // meaningful diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800112 bool IsHeapAddress(const Object* obj);
113
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700114 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
115 // Requires the heap lock to be held.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 bool IsLiveObjectLocked(const Object* obj)
117 SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700118
Carl Shapiro69759ea2011-07-21 18:13:35 -0700119 // Initiates an explicit garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 void CollectGarbage(bool clear_soft_references)
121 LOCKS_EXCLUDED(GlobalSynchronization::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700122
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700123 // Does a concurrent GC, should only be called by the GC daemon thread
124 // through runtime.
125 void ConcurrentGC();
126
Elliott Hughesbf86d042011-08-31 17:53:14 -0700127 // Implements java.lang.Runtime.maxMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800128 int64_t GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700129 // Implements java.lang.Runtime.totalMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800130 int64_t GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700131 // Implements java.lang.Runtime.freeMemory.
Mathieu Chartier037813d2012-08-23 16:44:59 -0700132 int64_t GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700133
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700134 // Implements VMDebug.countInstancesOfClass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 int64_t CountInstances(Class* c, bool count_assignable)
136 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_)
137 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700138
Ian Rogers3bb17a62012-01-27 23:56:44 -0800139 // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
140 // implement dalvik.system.VMRuntime.clearGrowthLimit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800141 void ClearGrowthLimit();
jeffhaoc1160702011-10-27 15:48:45 -0700142
Ian Rogers30fab402012-01-23 15:43:46 -0800143 // Target ideal heap utilization ratio, implements
144 // dalvik.system.VMRuntime.getTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800145 float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700146 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700147 }
Ian Rogers30fab402012-01-23 15:43:46 -0800148 // Set target ideal heap utilization ratio, implements
149 // dalvik.system.VMRuntime.setTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800150 void SetTargetHeapUtilization(float target) {
Ian Rogers30fab402012-01-23 15:43:46 -0800151 DCHECK_GT(target, 0.0f); // asserted in Java code
152 DCHECK_LT(target, 1.0f);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700153 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700154 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800155
156 // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
157 // from the system. Doesn't allow the space to exceed its growth limit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800158 void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700159
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700160 // Blocks the caller until the garbage collector becomes idle and returns
161 // true if we waited for the GC to complete.
162 bool WaitForConcurrentGcToComplete();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700163
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700164 const Spaces& GetSpaces() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700165 return spaces_;
166 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700167
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800168 void SetReferenceOffsets(MemberOffset reference_referent_offset,
169 MemberOffset reference_queue_offset,
170 MemberOffset reference_queueNext_offset,
171 MemberOffset reference_pendingNext_offset,
172 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700173
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800174 Object* GetReferenceReferent(Object* reference);
175 void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700176
Elliott Hughesadb460d2011-10-05 17:02:34 -0700177 // Returns true if the reference object has not yet been enqueued.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800178 bool IsEnqueuable(const Object* ref);
179 void EnqueueReference(Object* ref, Object** list);
180 void EnqueuePendingReference(Object* ref, Object** list);
181 Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700182
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800183 MemberOffset GetReferencePendingNextOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700184 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700185 return reference_pendingNext_offset_;
186 }
187
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800188 MemberOffset GetFinalizerReferenceZombieOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700189 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700190 return finalizer_reference_zombie_offset_;
191 }
192
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800193 void EnableObjectValidation() {
Ian Rogers30fab402012-01-23 15:43:46 -0800194#if VERIFY_OBJECT_ENABLED
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800195 VerifyHeap();
Ian Rogers30fab402012-01-23 15:43:46 -0800196#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700197 verify_objects_ = true;
198 }
199
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800200 void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700201 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700202 }
203
Mathieu Chartier037813d2012-08-23 16:44:59 -0700204 void RecordFree(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700205
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700206 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
207 // The call is not needed if NULL is stored in the field.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700208 void WriteBarrierField(const Object* dst, MemberOffset /*offset*/, const Object* /*new_value*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700209 if (!card_marking_disabled_) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700210 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700211 }
212 }
213
214 // Write barrier for array operations that update many field positions
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700215 void WriteBarrierArray(const Object* dst, int /*start_offset*/,
216 size_t /*length TODO: element_count or byte_count?*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700217 if (UNLIKELY(!card_marking_disabled_)) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700218 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700219 }
220 }
221
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800222 CardTable* GetCardTable() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700223 return card_table_.get();
Ian Rogers5d76c432011-10-31 21:42:49 -0700224 }
225
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800226 void DisableCardMarking() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700227 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
228 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700229 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700230
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800231 void AddFinalizerReference(Thread* self, Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700232
Mathieu Chartier037813d2012-08-23 16:44:59 -0700233 size_t GetBytesAllocated() const;
234 size_t GetObjectsAllocated() const;
235 size_t GetConcurrentStartSize() const;
236 size_t GetConcurrentMinFree() const;
237 size_t GetUsedMemorySize() const;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700238
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700239 // Functions for getting the bitmap which corresponds to an object's address.
240 // This is probably slow, TODO: use better data structure like binary tree .
241 Space* FindSpaceFromObject(const Object*) const;
242
Mathieu Chartier037813d2012-08-23 16:44:59 -0700243 void DumpForSigQuit(std::ostream& os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700244
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700245 void Trim();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700246
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700247 HeapBitmap* GetLiveBitmap() SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700248 return live_bitmap_.get();
249 }
250
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 HeapBitmap* GetMarkBitmap() SHARED_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700252 return mark_bitmap_.get();
253 }
254
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700255 void PreZygoteFork();
256
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700257 // Mark and empty stack.
258 void FlushAllocStack()
259 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
260
261 // Mark all the objects in the allocation stack as live.
262 void MarkStackAsLive(MarkStack* alloc_stack);
263
264 // Un-mark all the objects in the allocation stack.
265 void UnMarkStack(MarkStack* alloc_stack);
266
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700267 // Un-mark all live objects in the allocation stack.
268 void UnMarkStackAsLive(MarkStack* alloc_stack);
269
270 // Update and mark mod union table based on gc type.
271 void UpdateAndMarkModUnion(TimingLogger& timings, GcType gc_type)
272 EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
273
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700274 // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700275 // Assumes there is only one image space.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700276 ImageSpace* GetImageSpace();
277 AllocSpace* GetAllocSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700278 void DumpSpaces();
Elliott Hughesf8349362012-06-18 15:00:06 -0700279
Carl Shapiro58551df2011-07-24 03:09:51 -0700280 private:
281 // Allocates uninitialized storage.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700282 Object* Allocate(AllocSpace* space, size_t num_bytes)
283 LOCKS_EXCLUDED(GlobalSynchronization::thread_suspend_count_lock_)
284 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700285
Elliott Hughesadb460d2011-10-05 17:02:34 -0700286 // Pushes a list of cleared references out to the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800287 void EnqueueClearedReferences(Object** cleared_references);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700288
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800289 void RequestHeapTrim();
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700290 void RequestConcurrentGC();
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800291
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700292 void RecordAllocation(AllocSpace* space, const Object* object)
Mathieu Chartier037813d2012-08-23 16:44:59 -0700293 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700294
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700295 void CollectGarbageInternal(GcType gc_plan, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700296 LOCKS_EXCLUDED(gc_complete_lock_,
297 GlobalSynchronization::heap_bitmap_lock_,
298 GlobalSynchronization::mutator_lock_,
299 GlobalSynchronization::thread_suspend_count_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700300 void CollectGarbageMarkSweepPlan(GcType gc_plan, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700301 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_,
302 GlobalSynchronization::mutator_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700303 void CollectGarbageConcurrentMarkSweepPlan(GcType gc_plan, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700304 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_,
305 GlobalSynchronization::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700306
Ian Rogers3bb17a62012-01-27 23:56:44 -0800307 // Given the current contents of the alloc space, increase the allowed heap footprint to match
308 // the target utilization ratio. This should only be called immediately after a full garbage
309 // collection.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800310 void GrowForUtilization();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700311
Mathieu Chartier637e3482012-08-17 10:41:32 -0700312 size_t GetPercentFree();
Elliott Hughesc967f782012-04-16 10:23:15 -0700313
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700314 void AddSpace(Space* space) LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800315
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700316 // No thread saftey analysis since we call this everywhere and it is impossible to find a proper
317 // lock ordering for it.
318 void VerifyObjectBody(const Object *obj)
319 NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700320
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700321 static void VerificationCallback(Object* obj, void* arg)
322 SHARED_LOCKS_REQUIRED(GlobalSychronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700323
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700324 // Swpa bitmaps (if we are a full Gc then we swap the zygote bitmap too).
325 void SwapBitmaps();
326
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700327 Spaces spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700328
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700329 // The alloc space which we are currently allocating into.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800330 AllocSpace* alloc_space_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700331
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700332 // The mod-union table remembers all of the references from the image space to the alloc /
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700333 // zygote spaces.
334 UniquePtr<ModUnionTable> mod_union_table_;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700335
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700336 // This table holds all of the references from the zygote space to the alloc space.
337 UniquePtr<ModUnionTable> zygote_mod_union_table_;
338
339 UniquePtr<CardTable> card_table_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700340
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700341 // True for concurrent mark sweep GC, false for mark sweep.
342 const bool concurrent_gc_;
343
344 // If we have a zygote space.
345 bool have_zygote_space_;
346
Ian Rogers5d76c432011-10-31 21:42:49 -0700347 // Used by the image writer to disable card marking on copied objects
348 // TODO: remove
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800349 bool card_marking_disabled_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700350
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700351 // Guards access to the state of GC, associated conditional variable is used to signal when a GC
352 // completes.
353 Mutex* gc_complete_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
354 UniquePtr<ConditionVariable> gc_complete_cond_ GUARDED_BY(gc_complete_lock_);
355
Carl Shapiro58551df2011-07-24 03:09:51 -0700356 // True while the garbage collector is running.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700357 volatile bool is_gc_running_ GUARDED_BY(gc_complete_lock_);;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700358
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700359 // Bytes until concurrent GC starts.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700360 volatile size_t concurrent_start_bytes_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700361 size_t concurrent_start_size_;
362 size_t concurrent_min_free_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700363 size_t sticky_gc_count_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700364
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365 // Number of bytes allocated. Adjusted after each allocation and free.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700366 volatile size_t num_bytes_allocated_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700367
368 // Number of objects allocated. Adjusted after each allocation and free.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700369 volatile size_t num_objects_allocated_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700370
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700371 // Heap verification flags.
372 const bool pre_gc_verify_heap_;
373 const bool post_gc_verify_heap_;
374 const bool verify_mod_union_table_;
375
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700376 // Last trim time
377 uint64_t last_trim_time_;
378
379 UniquePtr<HeapBitmap> live_bitmap_ GUARDED_BY(GlobalSynchronization::heap_bitmap_lock_);
380 UniquePtr<HeapBitmap> mark_bitmap_ GUARDED_BY(GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700381
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700382 // True while the garbage collector is trying to signal the GC daemon thread.
383 // This flag is needed to prevent recursion from occurring when the JNI calls
384 // allocate memory and request another GC.
385 bool try_running_gc_;
386
387 // Used to ensure that we don't ever recursively request GC.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700388 volatile bool requesting_gc_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700389
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700390 // Mark stack that we reuse to avoid re-allocating the mark stack
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700391 UniquePtr<MarkStack> mark_stack_;
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700392
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700393 // Allocation stack, new allocations go here so that we can do sticky mark bits. This enables us
394 // to use the live bitmap as the old mark bitmap.
395 UniquePtr<MarkStack> allocation_stack_;
396
397 // Second allocation stack so that we can process allocation with the heap unlocked.
398 UniquePtr<MarkStack> live_stack_;
399
Brian Carlstrom1f870082011-08-23 16:02:11 -0700400 // offset of java.lang.ref.Reference.referent
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800401 MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700402
403 // offset of java.lang.ref.Reference.queue
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800404 MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700405
406 // offset of java.lang.ref.Reference.queueNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800407 MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700408
409 // offset of java.lang.ref.Reference.pendingNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800410 MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700411
412 // offset of java.lang.ref.FinalizerReference.zombie
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800413 MemberOffset finalizer_reference_zombie_offset_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700414
Brian Carlstrom395520e2011-09-25 19:35:00 -0700415 // Target ideal heap utilization ratio
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800416 float target_utilization_;
Brian Carlstrom395520e2011-09-25 19:35:00 -0700417
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800418 bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700419
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700420 friend class VerifyReferenceVisitor;
421 friend class VerifyObjectVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700422 friend class ScopedHeapLock;
Ian Rogers30fab402012-01-23 15:43:46 -0800423 FRIEND_TEST(SpaceTest, AllocAndFree);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800424 FRIEND_TEST(SpaceTest, AllocAndFreeList);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700425 FRIEND_TEST(SpaceTest, ZygoteSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800426 friend class SpaceTest;
Ian Rogers30fab402012-01-23 15:43:46 -0800427
Carl Shapiro69759ea2011-07-21 18:13:35 -0700428 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
429};
430
Carl Shapiro1fb86202011-06-27 17:43:13 -0700431} // namespace art
432
433#endif // ART_SRC_HEAP_H_