blob: 5fe491f5f94ea78da41c057b9fc9338580eedfe2 [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"
Ian Rogers5d76c432011-10-31 21:42:49 -070025#include "card_table.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026#include "globals.h"
Ian Rogers30fab402012-01-23 15:43:46 -080027#include "gtest/gtest.h"
Elliott Hughes5e71b522011-10-20 13:12:32 -070028#include "heap_bitmap.h"
Ian Rogers81d425b2012-09-27 16:03:43 -070029#include "locks.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070030#include "offsets.h"
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070031#include "safe_map.h"
Mathieu Chartier0325e622012-09-05 14:22:51 -070032#include "timing_logger.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070033
Elliott Hughes3e465b12011-09-02 18:26:12 -070034#define VERIFY_OBJECT_ENABLED 0
35
Mathieu Chartierdcf8d722012-08-02 14:55:54 -070036// Fast verification means we do not verify the classes of objects.
37#define VERIFY_OBJECT_FAST 1
38
Carl Shapiro1fb86202011-06-27 17:43:13 -070039namespace art {
40
Ian Rogers30fab402012-01-23 15:43:46 -080041class AllocSpace;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070042class Class;
Ian Rogers81d425b2012-09-27 16:03:43 -070043class ConditionVariable;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070044class HeapBitmap;
Brian Carlstromfddf6f62012-03-15 16:56:45 -070045class ImageSpace;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -070046class LargeObjectSpace;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070047class MarkStack;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -070048class ModUnionTable;
Ian Rogers81d425b2012-09-27 16:03:43 -070049class Mutex;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070050class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070051class Space;
Ian Rogers30fab402012-01-23 15:43:46 -080052class SpaceTest;
Mathieu Chartier5301cd22012-05-31 12:11:36 -070053class Thread;
Mathieu Chartier357e9be2012-08-01 11:00:14 -070054class TimingLogger;
Carl Shapiro69759ea2011-07-21 18:13:35 -070055
Mathieu Chartier2fde5332012-09-14 14:51:54 -070056typedef std::vector<ContinuousSpace*> Spaces;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -070057
Mathieu Chartier866fb2a2012-09-10 10:47:49 -070058// The ordering of the enum matters, it is used to determine which GCs are run first.
Mathieu Chartier357e9be2012-08-01 11:00:14 -070059enum GcType {
Mathieu Chartier866fb2a2012-09-10 10:47:49 -070060 // No Gc
61 kGcTypeNone,
Mathieu Chartier357e9be2012-08-01 11:00:14 -070062 // Sticky mark bits "generational" GC.
Mathieu Chartier0325e622012-09-05 14:22:51 -070063 kGcTypeSticky,
64 // Partial GC, over only the alloc space.
65 kGcTypePartial,
Mathieu Chartier866fb2a2012-09-10 10:47:49 -070066 // Full GC
67 kGcTypeFull,
Mathieu Chartier0325e622012-09-05 14:22:51 -070068 // Number of different Gc types.
69 kGcTypeMax,
Mathieu Chartier357e9be2012-08-01 11:00:14 -070070};
Mathieu Chartierfd678be2012-08-30 14:50:54 -070071std::ostream& operator<<(std::ostream& os, const GcType& policy);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070072
Mathieu Chartier2fde5332012-09-14 14:51:54 -070073enum GcCause {
74 kGcCauseForAlloc,
75 kGcCauseBackground,
76 kGcCauseExplicit,
77};
78std::ostream& operator<<(std::ostream& os, const GcCause& policy);
79
Elliott Hughesf8349362012-06-18 15:00:06 -070080class LOCKABLE Heap {
Carl Shapiro1fb86202011-06-27 17:43:13 -070081 public:
Ian Rogers30fab402012-01-23 15:43:46 -080082 static const size_t kInitialSize = 2 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070083
Ian Rogers30fab402012-01-23 15:43:46 -080084 static const size_t kMaximumSize = 32 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070085
Elliott Hughes410c0c82011-09-01 17:58:25 -070086 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070087 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070088
Brian Carlstrom58ae9412011-10-04 00:56:06 -070089 // Create a heap with the requested sizes. The possible empty
90 // image_file_names names specify Spaces to load based on
91 // ImageWriter output.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080092 explicit Heap(size_t starting_size, size_t growth_limit, size_t capacity,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 const std::string& image_file_name, bool concurrent_gc);
Carl Shapiro61e019d2011-07-14 16:53:09 -070094
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080095 ~Heap();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070096
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070097 // Allocates and initializes storage for an object instance.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070098 Object* AllocObject(Class* klass, size_t num_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -070099 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -0700100
Elliott Hughesa2501992011-08-26 19:39:54 -0700101 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -0700102#if VERIFY_OBJECT_ENABLED
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700103 void VerifyObject(const Object* o);
Elliott Hughes3e465b12011-09-02 18:26:12 -0700104#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700105 void VerifyObject(const Object*) {}
Elliott Hughes3e465b12011-09-02 18:26:12 -0700106#endif
Ian Rogers408f79a2011-08-23 18:22:33 -0700107
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700108 // Check sanity of all live references. Requires the heap lock.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800109 void VerifyHeap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700110 static void RootMatchesObjectVisitor(const Object* root, void* arg);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700111 bool VerifyHeapReferences()
112 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
113 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
114 bool VerifyMissingCardMarks()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
116 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700117
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700118 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -0700119 // and doesn't abort on error, allowing the caller to report more
120 // meaningful diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800121 bool IsHeapAddress(const Object* obj);
122
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700123 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
124 // Requires the heap lock to be held.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 bool IsLiveObjectLocked(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700126 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700127
Carl Shapiro69759ea2011-07-21 18:13:35 -0700128 // Initiates an explicit garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700129 void CollectGarbage(bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700130 LOCKS_EXCLUDED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700131
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700132 // Does a concurrent GC, should only be called by the GC daemon thread
133 // through runtime.
Ian Rogers81d425b2012-09-27 16:03:43 -0700134 void ConcurrentGC(Thread* self);
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700135
Elliott Hughesbf86d042011-08-31 17:53:14 -0700136 // Implements java.lang.Runtime.maxMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800137 int64_t GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700138 // Implements java.lang.Runtime.totalMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800139 int64_t GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700140 // Implements java.lang.Runtime.freeMemory.
Mathieu Chartier037813d2012-08-23 16:44:59 -0700141 int64_t GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700142
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700143 // Implements VMDebug.countInstancesOfClass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700144 int64_t CountInstances(Class* c, bool count_assignable)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700145 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
146 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700147
Ian Rogers3bb17a62012-01-27 23:56:44 -0800148 // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
149 // implement dalvik.system.VMRuntime.clearGrowthLimit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800150 void ClearGrowthLimit();
jeffhaoc1160702011-10-27 15:48:45 -0700151
Ian Rogers30fab402012-01-23 15:43:46 -0800152 // Target ideal heap utilization ratio, implements
153 // dalvik.system.VMRuntime.getTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800154 float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700155 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700156 }
Ian Rogers30fab402012-01-23 15:43:46 -0800157 // Set target ideal heap utilization ratio, implements
158 // dalvik.system.VMRuntime.setTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800159 void SetTargetHeapUtilization(float target) {
Ian Rogers30fab402012-01-23 15:43:46 -0800160 DCHECK_GT(target, 0.0f); // asserted in Java code
161 DCHECK_LT(target, 1.0f);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700162 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700163 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800164
165 // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
166 // from the system. Doesn't allow the space to exceed its growth limit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800167 void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700168
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700169 // Blocks the caller until the garbage collector becomes idle and returns
170 // true if we waited for the GC to complete.
Ian Rogers81d425b2012-09-27 16:03:43 -0700171 GcType WaitForConcurrentGcToComplete(Thread* self);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700172
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700173 const Spaces& GetSpaces() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700174 return spaces_;
175 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700176
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800177 void SetReferenceOffsets(MemberOffset reference_referent_offset,
178 MemberOffset reference_queue_offset,
179 MemberOffset reference_queueNext_offset,
180 MemberOffset reference_pendingNext_offset,
181 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700182
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800183 Object* GetReferenceReferent(Object* reference);
Ian Rogers23435d02012-09-24 11:23:12 -0700184 void ClearReferenceReferent(Object* reference) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700185
Elliott Hughesadb460d2011-10-05 17:02:34 -0700186 // Returns true if the reference object has not yet been enqueued.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800187 bool IsEnqueuable(const Object* ref);
Ian Rogers23435d02012-09-24 11:23:12 -0700188 void EnqueueReference(Object* ref, Object** list) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
189 void EnqueuePendingReference(Object* ref, Object** list)
190 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
191 Object* DequeuePendingReference(Object** list) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700192
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800193 MemberOffset GetReferencePendingNextOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700194 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700195 return reference_pendingNext_offset_;
196 }
197
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800198 MemberOffset GetFinalizerReferenceZombieOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700199 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700200 return finalizer_reference_zombie_offset_;
201 }
202
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800203 void EnableObjectValidation() {
Ian Rogers30fab402012-01-23 15:43:46 -0800204#if VERIFY_OBJECT_ENABLED
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800205 VerifyHeap();
Ian Rogers30fab402012-01-23 15:43:46 -0800206#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700207 verify_objects_ = true;
208 }
209
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800210 void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700211 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700212 }
213
Ian Rogers23435d02012-09-24 11:23:12 -0700214 bool IsObjectValidationEnabled() const {
215 return verify_objects_;
216 }
217
Mathieu Chartier037813d2012-08-23 16:44:59 -0700218 void RecordFree(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700219
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700220 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
221 // The call is not needed if NULL is stored in the field.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700222 void WriteBarrierField(const Object* dst, MemberOffset /*offset*/, const Object* /*new_value*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700223 if (!card_marking_disabled_) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700224 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700225 }
226 }
227
228 // Write barrier for array operations that update many field positions
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700229 void WriteBarrierArray(const Object* dst, int /*start_offset*/,
230 size_t /*length TODO: element_count or byte_count?*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700231 if (UNLIKELY(!card_marking_disabled_)) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700232 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700233 }
234 }
235
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800236 CardTable* GetCardTable() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700237 return card_table_.get();
Ian Rogers5d76c432011-10-31 21:42:49 -0700238 }
239
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800240 void DisableCardMarking() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700241 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
242 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700243 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700244
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800245 void AddFinalizerReference(Thread* self, Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700246
Mathieu Chartier037813d2012-08-23 16:44:59 -0700247 size_t GetBytesAllocated() const;
248 size_t GetObjectsAllocated() const;
249 size_t GetConcurrentStartSize() const;
250 size_t GetConcurrentMinFree() const;
251 size_t GetUsedMemorySize() const;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700252
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700253 // Functions for getting the bitmap which corresponds to an object's address.
254 // This is probably slow, TODO: use better data structure like binary tree .
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700255 ContinuousSpace* FindSpaceFromObject(const Object*) const;
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700256
Mathieu Chartier037813d2012-08-23 16:44:59 -0700257 void DumpForSigQuit(std::ostream& os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700258
Ian Rogers81d425b2012-09-27 16:03:43 -0700259 void Trim(Thread* self);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700260
Ian Rogersb726dcb2012-09-05 08:57:23 -0700261 HeapBitmap* GetLiveBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700262 return live_bitmap_.get();
263 }
264
Ian Rogersb726dcb2012-09-05 08:57:23 -0700265 HeapBitmap* GetMarkBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700266 return mark_bitmap_.get();
267 }
268
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700269 void PreZygoteFork();
270
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700271 // Mark and empty stack.
272 void FlushAllocStack()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700273 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700274
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700275 // Mark all the objects in the allocation stack in the specified bitmap.
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700276 void MarkAllocStack(SpaceBitmap* bitmap, SpaceSetMap* large_objects, MarkStack* stack)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700277 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700278
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700279 // Unmark all the objects in the allocation stack in the specified bitmap.
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700280 void UnMarkAllocStack(SpaceBitmap* bitmap, SpaceSetMap* large_objects, MarkStack* stack)
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700281 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700282
283 // Update and mark mod union table based on gc type.
284 void UpdateAndMarkModUnion(TimingLogger& timings, GcType gc_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700285 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700286
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700287 // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700288 // Assumes there is only one image space.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700289 ImageSpace* GetImageSpace();
290 AllocSpace* GetAllocSpace();
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700291 LargeObjectSpace* GetLargeObjectsSpace() {
292 return large_object_space_.get();
293 }
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700294 void DumpSpaces();
Elliott Hughesf8349362012-06-18 15:00:06 -0700295
Carl Shapiro58551df2011-07-24 03:09:51 -0700296 private:
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700297 // Allocates uninitialized storage. Passing in a null space tries to place the object in the
298 // large object space.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700299 Object* Allocate(AllocSpace* space, size_t num_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
301 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700302
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700303 // Try to allocate a number of bytes, this function never does any GCs.
304 Object* TryToAllocate(AllocSpace* space, size_t alloc_size, bool grow)
305 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
306 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
307
Elliott Hughesadb460d2011-10-05 17:02:34 -0700308 // Pushes a list of cleared references out to the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800309 void EnqueueClearedReferences(Object** cleared_references);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700310
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800311 void RequestHeapTrim();
Ian Rogers1f539342012-10-03 21:09:42 -0700312 void RequestConcurrentGC(Thread* self);
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800313
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700314 // Swap bitmaps (if we are a full Gc then we swap the zygote bitmap too).
Ian Rogers81d425b2012-09-27 16:03:43 -0700315 void SwapBitmaps(Thread* self) EXCLUSIVE_LOCKS_REQUIRED(GlobalSynchronization::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700316
317 void RecordAllocation(size_t size, const Object* object)
318 LOCKS_EXCLUDED(GlobalSynchronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700319
Mathieu Chartier866fb2a2012-09-10 10:47:49 -0700320 // Sometimes CollectGarbageInternal decides to run a different Gc than you requested. Returns
321 // which type of Gc was actually ran.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700322 GcType CollectGarbageInternal(GcType gc_plan, GcCause gc_cause, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700323 LOCKS_EXCLUDED(gc_complete_lock_,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700324 Locks::heap_bitmap_lock_,
325 Locks::mutator_lock_,
326 Locks::thread_suspend_count_lock_);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700327 void CollectGarbageMarkSweepPlan(Thread* self, GcType gc_plan, GcCause gc_cause,
328 bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700329 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_,
330 Locks::mutator_lock_);
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700331 void CollectGarbageConcurrentMarkSweepPlan(Thread* self, GcType gc_plan, GcCause gc_cause,
Ian Rogers81d425b2012-09-27 16:03:43 -0700332 bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700333 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_,
334 Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700335
Ian Rogers3bb17a62012-01-27 23:56:44 -0800336 // Given the current contents of the alloc space, increase the allowed heap footprint to match
337 // the target utilization ratio. This should only be called immediately after a full garbage
338 // collection.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800339 void GrowForUtilization();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700340
Mathieu Chartier637e3482012-08-17 10:41:32 -0700341 size_t GetPercentFree();
Elliott Hughesc967f782012-04-16 10:23:15 -0700342
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700343 void AddSpace(ContinuousSpace* space) LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700344
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700345 // No thread saftey analysis since we call this everywhere and it is impossible to find a proper
346 // lock ordering for it.
347 void VerifyObjectBody(const Object *obj)
348 NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700349
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700350 static void VerificationCallback(Object* obj, void* arg)
351 SHARED_LOCKS_REQUIRED(GlobalSychronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700352
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700353 // Swap the allocation stack with the live stack.
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700354 void SwapStacks();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700355
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700356 Spaces spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700357
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700358 // The alloc space which we are currently allocating into.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800359 AllocSpace* alloc_space_;
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700360
Mathieu Chartier0325e622012-09-05 14:22:51 -0700361 // One cumulative logger for each type of Gc.
362 typedef SafeMap<GcType, CumulativeLogger*> CumulativeTimings;
363 CumulativeTimings cumulative_timings_;
364
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700365 // The mod-union table remembers all of the references from the image space to the alloc /
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700366 // zygote spaces.
367 UniquePtr<ModUnionTable> mod_union_table_;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700368
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700369 // This table holds all of the references from the zygote space to the alloc space.
370 UniquePtr<ModUnionTable> zygote_mod_union_table_;
371
372 UniquePtr<CardTable> card_table_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700373
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700374 // True for concurrent mark sweep GC, false for mark sweep.
375 const bool concurrent_gc_;
376
377 // If we have a zygote space.
378 bool have_zygote_space_;
379
Ian Rogers5d76c432011-10-31 21:42:49 -0700380 // Used by the image writer to disable card marking on copied objects
381 // TODO: remove
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800382 bool card_marking_disabled_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700383
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700384 // Guards access to the state of GC, associated conditional variable is used to signal when a GC
385 // completes.
386 Mutex* gc_complete_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
387 UniquePtr<ConditionVariable> gc_complete_cond_ GUARDED_BY(gc_complete_lock_);
388
Carl Shapiro58551df2011-07-24 03:09:51 -0700389 // True while the garbage collector is running.
Mathieu Chartier866fb2a2012-09-10 10:47:49 -0700390 volatile bool is_gc_running_ GUARDED_BY(gc_complete_lock_);
391
392 // Last Gc type we ran. Used by WaitForConcurrentGc to know which Gc was waited on.
393 volatile GcType last_gc_type_ GUARDED_BY(gc_complete_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700394
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700395 // Maximum size that the heap can reach.
396 size_t growth_limit_;
397
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700398 // Bytes until concurrent GC starts.
Mathieu Chartier637e3482012-08-17 10:41:32 -0700399 volatile size_t concurrent_start_bytes_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700400 size_t concurrent_start_size_;
401 size_t concurrent_min_free_;
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700402 // Number of bytes allocated since the last Gc, we use this to help determine when to schedule concurrent GCs.
403 size_t bytes_since_last_gc_;
404 // Start a concurrent GC if we have allocated concurrent_gc_start_rate_ bytes and not done a GCs.
405 size_t concurrent_gc_start_rate_;
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700406 size_t sticky_gc_count_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700407
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700408 // Primitive objects larger than this size are put in the large object space.
409 size_t large_object_threshold_;
410
411 // Large object space.
412 UniquePtr<LargeObjectSpace> large_object_space_;
413
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700414 // Number of bytes allocated. Adjusted after each allocation and free.
Mathieu Chartier2fde5332012-09-14 14:51:54 -0700415 AtomicInteger num_bytes_allocated_;
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700416
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700417 // Heap verification flags.
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700418 const bool verify_missing_card_marks_;
419 const bool verify_system_weaks_;
420 const bool verify_pre_gc_heap_;
421 const bool verify_post_gc_heap_;
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700422 const bool verify_mod_union_table_;
423
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700424 // After how many GCs we force to do a partial GC instead of sticky mark bits GC.
425 const size_t partial_gc_frequency_;
426
427 // Sticky mark bits GC has some overhead, so if we have less a few megabytes of AllocSpace then
428 // it's probably better to just do a partial GC.
429 const size_t min_alloc_space_size_for_sticky_gc_;
430
431 // Minimum remaining size for sticky GC. Since sticky GC doesn't free up as much memory as a
432 // normal GC, it is important to not use it when we are almost out of memory.
433 const size_t min_remaining_space_for_sticky_gc_;
434
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700435 // Last trim time
436 uint64_t last_trim_time_;
437
Ian Rogersb726dcb2012-09-05 08:57:23 -0700438 UniquePtr<HeapBitmap> live_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
439 UniquePtr<HeapBitmap> mark_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700440
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700441 // True while the garbage collector is trying to signal the GC daemon thread.
442 // This flag is needed to prevent recursion from occurring when the JNI calls
443 // allocate memory and request another GC.
444 bool try_running_gc_;
445
446 // Used to ensure that we don't ever recursively request GC.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700447 volatile bool requesting_gc_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700448
Mathieu Chartiere0f0cb32012-08-28 11:26:00 -0700449 // Mark stack that we reuse to avoid re-allocating the mark stack.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700450 UniquePtr<MarkStack> mark_stack_;
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700451
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700452 // Allocation stack, new allocations go here so that we can do sticky mark bits. This enables us
453 // to use the live bitmap as the old mark bitmap.
454 UniquePtr<MarkStack> allocation_stack_;
455
456 // Second allocation stack so that we can process allocation with the heap unlocked.
457 UniquePtr<MarkStack> live_stack_;
458
Brian Carlstrom1f870082011-08-23 16:02:11 -0700459 // offset of java.lang.ref.Reference.referent
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800460 MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700461
462 // offset of java.lang.ref.Reference.queue
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800463 MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700464
465 // offset of java.lang.ref.Reference.queueNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800466 MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700467
468 // offset of java.lang.ref.Reference.pendingNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800469 MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700470
471 // offset of java.lang.ref.FinalizerReference.zombie
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800472 MemberOffset finalizer_reference_zombie_offset_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700473
Brian Carlstrom395520e2011-09-25 19:35:00 -0700474 // Target ideal heap utilization ratio
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800475 float target_utilization_;
Brian Carlstrom395520e2011-09-25 19:35:00 -0700476
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800477 bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700478
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700479 friend class MarkSweep;
480 friend class VerifyReferenceCardVisitor;
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700481 friend class VerifyReferenceVisitor;
482 friend class VerifyObjectVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700483 friend class ScopedHeapLock;
Ian Rogers30fab402012-01-23 15:43:46 -0800484 FRIEND_TEST(SpaceTest, AllocAndFree);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800485 FRIEND_TEST(SpaceTest, AllocAndFreeList);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700486 FRIEND_TEST(SpaceTest, ZygoteSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800487 friend class SpaceTest;
Ian Rogers30fab402012-01-23 15:43:46 -0800488
Carl Shapiro69759ea2011-07-21 18:13:35 -0700489 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
490};
491
Carl Shapiro1fb86202011-06-27 17:43:13 -0700492} // namespace art
493
494#endif // ART_SRC_HEAP_H_