blob: 912bfb6b8d6a0b9b9a93df90694a4e497747cf77 [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"
Mathieu Chartier0325e622012-09-05 14:22:51 -070031#include "timing_logger.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070032
Elliott Hughes3e465b12011-09-02 18:26:12 -070033#define VERIFY_OBJECT_ENABLED 0
34
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070035// Fast verification means we do not verify the classes of objects.
36#define VERIFY_OBJECT_FAST 1
37
Carl Shapiro1fb86202011-06-27 17:43:13 -070038namespace art {
39
Ian Rogers30fab402012-01-23 15:43:46 -080040class AllocSpace;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070041class Class;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070042class HeapBitmap;
Brian Carlstromfddf6f62012-03-15 16:56:45 -070043class ImageSpace;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070044class MarkStack;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070045class ModUnionTable;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070046class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070047class Space;
Ian Rogers30fab402012-01-23 15:43:46 -080048class SpaceTest;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070049class Thread;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070050class TimingLogger;
Carl Shapiro69759ea2011-07-21 18:13:35 -070051
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070052typedef std::vector<Space*> Spaces;
53
Mathieu Chartier357e9be2012-08-01 11:00:14 -070054enum GcType {
55 // Full GC
Mathieu Chartier0325e622012-09-05 14:22:51 -070056 kGcTypeFull,
Mathieu Chartier357e9be2012-08-01 11:00:14 -070057 // Sticky mark bits "generational" GC.
Mathieu Chartier0325e622012-09-05 14:22:51 -070058 kGcTypeSticky,
59 // Partial GC, over only the alloc space.
60 kGcTypePartial,
61 // Number of different Gc types.
62 kGcTypeMax,
Mathieu Chartier357e9be2012-08-01 11:00:14 -070063};
Mathieu Chartierfd678be2012-08-30 14:50:54 -070064std::ostream& operator<<(std::ostream& os, const GcType& policy);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070065
Elliott Hughesf8349362012-06-18 15:00:06 -070066class LOCKABLE Heap {
Carl Shapiro1fb86202011-06-27 17:43:13 -070067 public:
Ian Rogers30fab402012-01-23 15:43:46 -080068 static const size_t kInitialSize = 2 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070069
Ian Rogers30fab402012-01-23 15:43:46 -080070 static const size_t kMaximumSize = 32 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070071
Mathieu Chartier357e9be2012-08-01 11:00:14 -070072 // After how many GCs we force to do a partial GC instead of sticky mark bits GC.
73 static const size_t kPartialGCFrequency = 10;
74
75 // Sticky mark bits GC has some overhead, so if we have less a few megabytes of AllocSpace then
76 // it's probably better to just do a partial GC.
77 static const size_t kMinAllocSpaceSizeForStickyGC = 6 * MB;
78
79 // Minimum remaining size fo sticky GC. Since sticky GC doesn't free up as much memory as a
80 // normal GC, it is important to not use it when we are almost out of memory.
81 static const size_t kMinRemainingSpaceForStickyGC = 1 * MB;
82
Elliott Hughes410c0c82011-09-01 17:58:25 -070083 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070084 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070085
Brian Carlstrom58ae9412011-10-04 00:56:06 -070086 // Create a heap with the requested sizes. The possible empty
87 // image_file_names names specify Spaces to load based on
88 // ImageWriter output.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080089 explicit Heap(size_t starting_size, size_t growth_limit, size_t capacity,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090 const std::string& image_file_name, bool concurrent_gc);
Carl Shapiro61e019d2011-07-14 16:53:09 -070091
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080092 ~Heap();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070093
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070094 // Allocates and initializes storage for an object instance.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 Object* AllocObject(Class* klass, size_t num_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -070096 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070097
Elliott Hughesa2501992011-08-26 19:39:54 -070098 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070099#if VERIFY_OBJECT_ENABLED
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700100 void VerifyObject(const Object* o);
Elliott Hughes3e465b12011-09-02 18:26:12 -0700101#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700102 void VerifyObject(const Object*) {}
Elliott Hughes3e465b12011-09-02 18:26:12 -0700103#endif
Ian Rogers408f79a2011-08-23 18:22:33 -0700104
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700105 // Check sanity of all live references. Requires the heap lock.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800106 void VerifyHeap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700107 static void RootMatchesObjectVisitor(const Object* root, void* arg);
108 void VerifyHeapReferences(const std::string& phase)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700109 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700111
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700112 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -0700113 // and doesn't abort on error, allowing the caller to report more
114 // meaningful diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800115 bool IsHeapAddress(const Object* obj);
116
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700117 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
118 // Requires the heap lock to be held.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119 bool IsLiveObjectLocked(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700120 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700121
Carl Shapiro69759ea2011-07-21 18:13:35 -0700122 // Initiates an explicit garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700123 void CollectGarbage(bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700124 LOCKS_EXCLUDED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700125
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700126 // Does a concurrent GC, should only be called by the GC daemon thread
127 // through runtime.
128 void ConcurrentGC();
129
Elliott Hughesbf86d042011-08-31 17:53:14 -0700130 // Implements java.lang.Runtime.maxMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800131 int64_t GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700132 // Implements java.lang.Runtime.totalMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800133 int64_t GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700134 // Implements java.lang.Runtime.freeMemory.
Mathieu Chartier037813d2012-08-23 16:44:59 -0700135 int64_t GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700136
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700137 // Implements VMDebug.countInstancesOfClass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700138 int64_t CountInstances(Class* c, bool count_assignable)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700139 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700141
Ian Rogers3bb17a62012-01-27 23:56:44 -0800142 // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
143 // implement dalvik.system.VMRuntime.clearGrowthLimit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800144 void ClearGrowthLimit();
jeffhaoc1160702011-10-27 15:48:45 -0700145
Ian Rogers30fab402012-01-23 15:43:46 -0800146 // Target ideal heap utilization ratio, implements
147 // dalvik.system.VMRuntime.getTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800148 float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700149 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700150 }
Ian Rogers30fab402012-01-23 15:43:46 -0800151 // Set target ideal heap utilization ratio, implements
152 // dalvik.system.VMRuntime.setTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800153 void SetTargetHeapUtilization(float target) {
Ian Rogers30fab402012-01-23 15:43:46 -0800154 DCHECK_GT(target, 0.0f); // asserted in Java code
155 DCHECK_LT(target, 1.0f);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700156 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700157 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800158
159 // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
160 // from the system. Doesn't allow the space to exceed its growth limit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800161 void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700162
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700163 // Blocks the caller until the garbage collector becomes idle and returns
164 // true if we waited for the GC to complete.
165 bool WaitForConcurrentGcToComplete();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700166
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700167 const Spaces& GetSpaces() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700168 return spaces_;
169 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700170
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800171 void SetReferenceOffsets(MemberOffset reference_referent_offset,
172 MemberOffset reference_queue_offset,
173 MemberOffset reference_queueNext_offset,
174 MemberOffset reference_pendingNext_offset,
175 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700176
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800177 Object* GetReferenceReferent(Object* reference);
178 void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700179
Elliott Hughesadb460d2011-10-05 17:02:34 -0700180 // Returns true if the reference object has not yet been enqueued.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800181 bool IsEnqueuable(const Object* ref);
182 void EnqueueReference(Object* ref, Object** list);
183 void EnqueuePendingReference(Object* ref, Object** list);
184 Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700185
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800186 MemberOffset GetReferencePendingNextOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700188 return reference_pendingNext_offset_;
189 }
190
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800191 MemberOffset GetFinalizerReferenceZombieOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700192 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700193 return finalizer_reference_zombie_offset_;
194 }
195
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800196 void EnableObjectValidation() {
Ian Rogers30fab402012-01-23 15:43:46 -0800197#if VERIFY_OBJECT_ENABLED
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800198 VerifyHeap();
Ian Rogers30fab402012-01-23 15:43:46 -0800199#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700200 verify_objects_ = true;
201 }
202
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800203 void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700204 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700205 }
206
Mathieu Chartier037813d2012-08-23 16:44:59 -0700207 void RecordFree(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700208
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700209 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
210 // The call is not needed if NULL is stored in the field.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700211 void WriteBarrierField(const Object* dst, MemberOffset /*offset*/, const Object* /*new_value*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700212 if (!card_marking_disabled_) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700213 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700214 }
215 }
216
217 // Write barrier for array operations that update many field positions
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700218 void WriteBarrierArray(const Object* dst, int /*start_offset*/,
219 size_t /*length TODO: element_count or byte_count?*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700220 if (UNLIKELY(!card_marking_disabled_)) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700221 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700222 }
223 }
224
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800225 CardTable* GetCardTable() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700226 return card_table_.get();
Ian Rogers5d76c432011-10-31 21:42:49 -0700227 }
228
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800229 void DisableCardMarking() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700230 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
231 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700232 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700233
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800234 void AddFinalizerReference(Thread* self, Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700235
Mathieu Chartier037813d2012-08-23 16:44:59 -0700236 size_t GetBytesAllocated() const;
237 size_t GetObjectsAllocated() const;
238 size_t GetConcurrentStartSize() const;
239 size_t GetConcurrentMinFree() const;
240 size_t GetUsedMemorySize() const;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700241
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700242 // Functions for getting the bitmap which corresponds to an object's address.
243 // This is probably slow, TODO: use better data structure like binary tree .
244 Space* FindSpaceFromObject(const Object*) const;
245
Mathieu Chartier037813d2012-08-23 16:44:59 -0700246 void DumpForSigQuit(std::ostream& os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700247
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700248 void Trim();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700249
Ian Rogersb726dcb2012-09-05 08:57:23 -0700250 HeapBitmap* GetLiveBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700251 return live_bitmap_.get();
252 }
253
Ian Rogersb726dcb2012-09-05 08:57:23 -0700254 HeapBitmap* GetMarkBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700255 return mark_bitmap_.get();
256 }
257
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700258 void PreZygoteFork();
259
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700260 // Mark and empty stack.
261 void FlushAllocStack()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700262 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700263
264 // Mark all the objects in the allocation stack as live.
265 void MarkStackAsLive(MarkStack* alloc_stack);
266
267 // Un-mark all the objects in the allocation stack.
268 void UnMarkStack(MarkStack* alloc_stack);
269
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700270 // Un-mark all live objects in the allocation stack.
271 void UnMarkStackAsLive(MarkStack* alloc_stack);
272
273 // Update and mark mod union table based on gc type.
274 void UpdateAndMarkModUnion(TimingLogger& timings, GcType gc_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700275 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700276
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700277 // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700278 // Assumes there is only one image space.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700279 ImageSpace* GetImageSpace();
280 AllocSpace* GetAllocSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700281 void DumpSpaces();
Elliott Hughesf8349362012-06-18 15:00:06 -0700282
Carl Shapiro58551df2011-07-24 03:09:51 -0700283 private:
284 // Allocates uninitialized storage.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700285 Object* Allocate(AllocSpace* space, size_t num_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700286 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
287 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700288
Elliott Hughesadb460d2011-10-05 17:02:34 -0700289 // Pushes a list of cleared references out to the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800290 void EnqueueClearedReferences(Object** cleared_references);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700291
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800292 void RequestHeapTrim();
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700293 void RequestConcurrentGC();
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800294
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 void RecordAllocation(AllocSpace* space, const Object* object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700297
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700298 void CollectGarbageInternal(GcType gc_plan, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299 LOCKS_EXCLUDED(gc_complete_lock_,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 Locks::heap_bitmap_lock_,
301 Locks::mutator_lock_,
302 Locks::thread_suspend_count_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700303 void CollectGarbageMarkSweepPlan(GcType gc_plan, bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700304 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_,
305 Locks::mutator_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700306 void CollectGarbageConcurrentMarkSweepPlan(GcType gc_plan, bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700307 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_,
308 Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700309
Ian Rogers3bb17a62012-01-27 23:56:44 -0800310 // Given the current contents of the alloc space, increase the allowed heap footprint to match
311 // the target utilization ratio. This should only be called immediately after a full garbage
312 // collection.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800313 void GrowForUtilization();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700314
Mathieu Chartier637e3482012-08-17 10:41:32 -0700315 size_t GetPercentFree();
Elliott Hughesc967f782012-04-16 10:23:15 -0700316
Ian Rogersb726dcb2012-09-05 08:57:23 -0700317 void AddSpace(Space* space) LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800318
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700319 // No thread saftey analysis since we call this everywhere and it is impossible to find a proper
320 // lock ordering for it.
321 void VerifyObjectBody(const Object *obj)
322 NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700323
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700324 static void VerificationCallback(Object* obj, void* arg)
325 SHARED_LOCKS_REQUIRED(GlobalSychronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700326
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700327 // Swpa bitmaps (if we are a full Gc then we swap the zygote bitmap too).
328 void SwapBitmaps();
329
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700330 Spaces spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700331
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700332 // The alloc space which we are currently allocating into.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800333 AllocSpace* alloc_space_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700334
Mathieu Chartier0325e622012-09-05 14:22:51 -0700335 // One cumulative logger for each type of Gc.
336 typedef SafeMap<GcType, CumulativeLogger*> CumulativeTimings;
337 CumulativeTimings cumulative_timings_;
338
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700339 // The mod-union table remembers all of the references from the image space to the alloc /
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700340 // zygote spaces.
341 UniquePtr<ModUnionTable> mod_union_table_;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700342
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700343 // This table holds all of the references from the zygote space to the alloc space.
344 UniquePtr<ModUnionTable> zygote_mod_union_table_;
345
346 UniquePtr<CardTable> card_table_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700347
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700348 // True for concurrent mark sweep GC, false for mark sweep.
349 const bool concurrent_gc_;
350
351 // If we have a zygote space.
352 bool have_zygote_space_;
353
Ian Rogers5d76c432011-10-31 21:42:49 -0700354 // Used by the image writer to disable card marking on copied objects
355 // TODO: remove
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800356 bool card_marking_disabled_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700357
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700358 // Guards access to the state of GC, associated conditional variable is used to signal when a GC
359 // completes.
360 Mutex* gc_complete_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
361 UniquePtr<ConditionVariable> gc_complete_cond_ GUARDED_BY(gc_complete_lock_);
362
Carl Shapiro58551df2011-07-24 03:09:51 -0700363 // True while the garbage collector is running.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700364 volatile bool is_gc_running_ GUARDED_BY(gc_complete_lock_);;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700365
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700366 // Bytes until concurrent GC starts.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700367 volatile size_t concurrent_start_bytes_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700368 size_t concurrent_start_size_;
369 size_t concurrent_min_free_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700370 size_t sticky_gc_count_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700371
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700372 // Number of bytes allocated. Adjusted after each allocation and free.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700373 volatile size_t num_bytes_allocated_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374
375 // Number of objects allocated. Adjusted after each allocation and free.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700376 volatile size_t num_objects_allocated_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700377
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700378 // Heap verification flags.
379 const bool pre_gc_verify_heap_;
380 const bool post_gc_verify_heap_;
381 const bool verify_mod_union_table_;
382
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700383 // Last trim time
384 uint64_t last_trim_time_;
385
Ian Rogersb726dcb2012-09-05 08:57:23 -0700386 UniquePtr<HeapBitmap> live_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
387 UniquePtr<HeapBitmap> mark_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700388
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700389 // True while the garbage collector is trying to signal the GC daemon thread.
390 // This flag is needed to prevent recursion from occurring when the JNI calls
391 // allocate memory and request another GC.
392 bool try_running_gc_;
393
394 // Used to ensure that we don't ever recursively request GC.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700395 volatile bool requesting_gc_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700396
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700397 // Mark stack that we reuse to avoid re-allocating the mark stack
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700398 UniquePtr<MarkStack> mark_stack_;
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700399
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700400 // Allocation stack, new allocations go here so that we can do sticky mark bits. This enables us
401 // to use the live bitmap as the old mark bitmap.
402 UniquePtr<MarkStack> allocation_stack_;
403
404 // Second allocation stack so that we can process allocation with the heap unlocked.
405 UniquePtr<MarkStack> live_stack_;
406
Brian Carlstrom1f870082011-08-23 16:02:11 -0700407 // offset of java.lang.ref.Reference.referent
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800408 MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700409
410 // offset of java.lang.ref.Reference.queue
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800411 MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700412
413 // offset of java.lang.ref.Reference.queueNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800414 MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700415
416 // offset of java.lang.ref.Reference.pendingNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800417 MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700418
419 // offset of java.lang.ref.FinalizerReference.zombie
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800420 MemberOffset finalizer_reference_zombie_offset_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700421
Brian Carlstrom395520e2011-09-25 19:35:00 -0700422 // Target ideal heap utilization ratio
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800423 float target_utilization_;
Brian Carlstrom395520e2011-09-25 19:35:00 -0700424
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800425 bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700426
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700427 friend class VerifyReferenceVisitor;
428 friend class VerifyObjectVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700429 friend class ScopedHeapLock;
Ian Rogers30fab402012-01-23 15:43:46 -0800430 FRIEND_TEST(SpaceTest, AllocAndFree);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800431 FRIEND_TEST(SpaceTest, AllocAndFreeList);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700432 FRIEND_TEST(SpaceTest, ZygoteSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800433 friend class SpaceTest;
Ian Rogers30fab402012-01-23 15:43:46 -0800434
Carl Shapiro69759ea2011-07-21 18:13:35 -0700435 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
436};
437
Carl Shapiro1fb86202011-06-27 17:43:13 -0700438} // namespace art
439
440#endif // ART_SRC_HEAP_H_