blob: 1f9ac868493b6262ccae41ff15a598b742200ca1 [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 Chartier866fb2a2012-09-10 10:47:49 -070054// The ordering of the enum matters, it is used to determine which GCs are run first.
Mathieu Chartier357e9be2012-08-01 11:00:14 -070055enum GcType {
Mathieu Chartier866fb2a2012-09-10 10:47:49 -070056 // No Gc
57 kGcTypeNone,
Mathieu Chartier357e9be2012-08-01 11:00:14 -070058 // Sticky mark bits "generational" GC.
Mathieu Chartier0325e622012-09-05 14:22:51 -070059 kGcTypeSticky,
60 // Partial GC, over only the alloc space.
61 kGcTypePartial,
Mathieu Chartier866fb2a2012-09-10 10:47:49 -070062 // Full GC
63 kGcTypeFull,
Mathieu Chartier0325e622012-09-05 14:22:51 -070064 // Number of different Gc types.
65 kGcTypeMax,
Mathieu Chartier357e9be2012-08-01 11:00:14 -070066};
Mathieu Chartierfd678be2012-08-30 14:50:54 -070067std::ostream& operator<<(std::ostream& os, const GcType& policy);
Mathieu Chartier357e9be2012-08-01 11:00:14 -070068
Elliott Hughesf8349362012-06-18 15:00:06 -070069class LOCKABLE Heap {
Carl Shapiro1fb86202011-06-27 17:43:13 -070070 public:
Ian Rogers30fab402012-01-23 15:43:46 -080071 static const size_t kInitialSize = 2 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070072
Ian Rogers30fab402012-01-23 15:43:46 -080073 static const size_t kMaximumSize = 32 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070074
Elliott Hughes410c0c82011-09-01 17:58:25 -070075 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070076 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070077
Brian Carlstrom58ae9412011-10-04 00:56:06 -070078 // Create a heap with the requested sizes. The possible empty
79 // image_file_names names specify Spaces to load based on
80 // ImageWriter output.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080081 explicit Heap(size_t starting_size, size_t growth_limit, size_t capacity,
Ian Rogers00f7d0e2012-07-19 15:28:27 -070082 const std::string& image_file_name, bool concurrent_gc);
Carl Shapiro61e019d2011-07-14 16:53:09 -070083
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080084 ~Heap();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070085
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070086 // Allocates and initializes storage for an object instance.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 Object* AllocObject(Class* klass, size_t num_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -070088 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070089
Elliott Hughesa2501992011-08-26 19:39:54 -070090 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070091#if VERIFY_OBJECT_ENABLED
Elliott Hughes1bac54f2012-03-16 12:48:31 -070092 void VerifyObject(const Object* o);
Elliott Hughes3e465b12011-09-02 18:26:12 -070093#else
Elliott Hughes1bac54f2012-03-16 12:48:31 -070094 void VerifyObject(const Object*) {}
Elliott Hughes3e465b12011-09-02 18:26:12 -070095#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070096
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070097 // Check sanity of all live references. Requires the heap lock.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080098 void VerifyHeap();
Mathieu Chartierfd678be2012-08-30 14:50:54 -070099 static void RootMatchesObjectVisitor(const Object* root, void* arg);
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700100 bool VerifyHeapReferences()
101 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103 bool VerifyMissingCardMarks()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700104 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_)
105 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700106
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700107 // A weaker test than IsLiveObject or VerifyObject that doesn't require the heap lock,
Elliott Hughesa2501992011-08-26 19:39:54 -0700108 // and doesn't abort on error, allowing the caller to report more
109 // meaningful diagnostics.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800110 bool IsHeapAddress(const Object* obj);
111
Elliott Hughes6a5bd492011-10-28 14:33:57 -0700112 // Returns true if 'obj' is a live heap object, false otherwise (including for invalid addresses).
113 // Requires the heap lock to be held.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 bool IsLiveObjectLocked(const Object* obj)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700115 SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Elliott Hughesa2501992011-08-26 19:39:54 -0700116
Carl Shapiro69759ea2011-07-21 18:13:35 -0700117 // Initiates an explicit garbage collection.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 void CollectGarbage(bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700119 LOCKS_EXCLUDED(Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700120
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700121 // Does a concurrent GC, should only be called by the GC daemon thread
122 // through runtime.
123 void ConcurrentGC();
124
Elliott Hughesbf86d042011-08-31 17:53:14 -0700125 // Implements java.lang.Runtime.maxMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800126 int64_t GetMaxMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700127 // Implements java.lang.Runtime.totalMemory.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800128 int64_t GetTotalMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700129 // Implements java.lang.Runtime.freeMemory.
Mathieu Chartier037813d2012-08-23 16:44:59 -0700130 int64_t GetFreeMemory();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700131
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700132 // Implements VMDebug.countInstancesOfClass.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700133 int64_t CountInstances(Class* c, bool count_assignable)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700134 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_)
135 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Elliott Hughes9d5ccec2011-09-19 13:19:50 -0700136
Ian Rogers3bb17a62012-01-27 23:56:44 -0800137 // Removes the growth limit on the alloc space so it may grow to its maximum capacity. Used to
138 // implement dalvik.system.VMRuntime.clearGrowthLimit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800139 void ClearGrowthLimit();
jeffhaoc1160702011-10-27 15:48:45 -0700140
Ian Rogers30fab402012-01-23 15:43:46 -0800141 // Target ideal heap utilization ratio, implements
142 // dalvik.system.VMRuntime.getTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800143 float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700144 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700145 }
Ian Rogers30fab402012-01-23 15:43:46 -0800146 // Set target ideal heap utilization ratio, implements
147 // dalvik.system.VMRuntime.setTargetHeapUtilization.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800148 void SetTargetHeapUtilization(float target) {
Ian Rogers30fab402012-01-23 15:43:46 -0800149 DCHECK_GT(target, 0.0f); // asserted in Java code
150 DCHECK_LT(target, 1.0f);
Brian Carlstrom395520e2011-09-25 19:35:00 -0700151 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700152 }
Ian Rogers3bb17a62012-01-27 23:56:44 -0800153
154 // For the alloc space, sets the maximum number of bytes that the heap is allowed to allocate
155 // from the system. Doesn't allow the space to exceed its growth limit.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800156 void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700157
Mathieu Chartierfc8cfac2012-06-19 11:56:36 -0700158 // Blocks the caller until the garbage collector becomes idle and returns
159 // true if we waited for the GC to complete.
Mathieu Chartier866fb2a2012-09-10 10:47:49 -0700160 GcType WaitForConcurrentGcToComplete();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700161
Mathieu Chartier654d3a22012-07-11 17:54:18 -0700162 const Spaces& GetSpaces() {
Carl Shapiro58551df2011-07-24 03:09:51 -0700163 return spaces_;
164 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700165
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800166 void SetReferenceOffsets(MemberOffset reference_referent_offset,
167 MemberOffset reference_queue_offset,
168 MemberOffset reference_queueNext_offset,
169 MemberOffset reference_pendingNext_offset,
170 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700171
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800172 Object* GetReferenceReferent(Object* reference);
173 void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700174
Elliott Hughesadb460d2011-10-05 17:02:34 -0700175 // Returns true if the reference object has not yet been enqueued.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800176 bool IsEnqueuable(const Object* ref);
177 void EnqueueReference(Object* ref, Object** list);
178 void EnqueuePendingReference(Object* ref, Object** list);
179 Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700180
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800181 MemberOffset GetReferencePendingNextOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700182 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700183 return reference_pendingNext_offset_;
184 }
185
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800186 MemberOffset GetFinalizerReferenceZombieOffset() {
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700187 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700188 return finalizer_reference_zombie_offset_;
189 }
190
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800191 void EnableObjectValidation() {
Ian Rogers30fab402012-01-23 15:43:46 -0800192#if VERIFY_OBJECT_ENABLED
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800193 VerifyHeap();
Ian Rogers30fab402012-01-23 15:43:46 -0800194#endif
Elliott Hughes85d15452011-09-16 17:33:01 -0700195 verify_objects_ = true;
196 }
197
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800198 void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700199 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700200 }
201
Mathieu Chartier037813d2012-08-23 16:44:59 -0700202 void RecordFree(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700203
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700204 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
205 // The call is not needed if NULL is stored in the field.
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700206 void WriteBarrierField(const Object* dst, MemberOffset /*offset*/, const Object* /*new_value*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700207 if (!card_marking_disabled_) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700208 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700209 }
210 }
211
212 // Write barrier for array operations that update many field positions
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700213 void WriteBarrierArray(const Object* dst, int /*start_offset*/,
214 size_t /*length TODO: element_count or byte_count?*/) {
Ian Rogers5d76c432011-10-31 21:42:49 -0700215 if (UNLIKELY(!card_marking_disabled_)) {
Elliott Hughes1bac54f2012-03-16 12:48:31 -0700216 card_table_->MarkCard(dst);
Ian Rogers5d76c432011-10-31 21:42:49 -0700217 }
218 }
219
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800220 CardTable* GetCardTable() {
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700221 return card_table_.get();
Ian Rogers5d76c432011-10-31 21:42:49 -0700222 }
223
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800224 void DisableCardMarking() {
Ian Rogers5d76c432011-10-31 21:42:49 -0700225 // TODO: we shouldn't need to disable card marking, this is here to help the image_writer
226 card_marking_disabled_ = true;
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700227 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700228
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800229 void AddFinalizerReference(Thread* self, Object* object);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700230
Mathieu Chartier037813d2012-08-23 16:44:59 -0700231 size_t GetBytesAllocated() const;
232 size_t GetObjectsAllocated() const;
233 size_t GetConcurrentStartSize() const;
234 size_t GetConcurrentMinFree() const;
235 size_t GetUsedMemorySize() const;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700236
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700237 // Functions for getting the bitmap which corresponds to an object's address.
238 // This is probably slow, TODO: use better data structure like binary tree .
239 Space* FindSpaceFromObject(const Object*) const;
240
Mathieu Chartier037813d2012-08-23 16:44:59 -0700241 void DumpForSigQuit(std::ostream& os);
Elliott Hughesc967f782012-04-16 10:23:15 -0700242
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700243 void Trim();
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700244
Ian Rogersb726dcb2012-09-05 08:57:23 -0700245 HeapBitmap* GetLiveBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700246 return live_bitmap_.get();
247 }
248
Ian Rogersb726dcb2012-09-05 08:57:23 -0700249 HeapBitmap* GetMarkBitmap() SHARED_LOCKS_REQUIRED(Locks::heap_bitmap_lock_) {
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700250 return mark_bitmap_.get();
251 }
252
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700253 void PreZygoteFork();
254
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700255 // Mark and empty stack.
256 void FlushAllocStack()
Ian Rogersb726dcb2012-09-05 08:57:23 -0700257 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700258
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700259 // Mark all the objects in the allocation stack in the specified bitmap.
260 void MarkAllocStack(SpaceBitmap* bitmap, MarkStack* stack)
261 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700262
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700263 // Unmark all the objects in the allocation stack in the specified bitmap.
264 void UnMarkAllocStack(SpaceBitmap* bitmap, MarkStack* stack)
265 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700266
267 // Update and mark mod union table based on gc type.
268 void UpdateAndMarkModUnion(TimingLogger& timings, GcType gc_type)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700269 EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_);
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700270
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700271 // DEPRECATED: Should remove in "near" future when support for multiple image spaces is added.
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700272 // Assumes there is only one image space.
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700273 ImageSpace* GetImageSpace();
274 AllocSpace* GetAllocSpace();
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700275 void DumpSpaces();
Elliott Hughesf8349362012-06-18 15:00:06 -0700276
Carl Shapiro58551df2011-07-24 03:09:51 -0700277 private:
278 // Allocates uninitialized storage.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700279 Object* Allocate(AllocSpace* space, size_t num_bytes)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700280 LOCKS_EXCLUDED(Locks::thread_suspend_count_lock_)
281 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiera6399032012-06-11 18:49:50 -0700282
Elliott Hughesadb460d2011-10-05 17:02:34 -0700283 // Pushes a list of cleared references out to the managed heap.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800284 void EnqueueClearedReferences(Object** cleared_references);
Elliott Hughesadb460d2011-10-05 17:02:34 -0700285
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800286 void RequestHeapTrim();
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700287 void RequestConcurrentGC();
Elliott Hughes8cf5bc02012-02-02 16:32:16 -0800288
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700289 void RecordAllocation(AllocSpace* space, const Object* object)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700290 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700291
Mathieu Chartier866fb2a2012-09-10 10:47:49 -0700292 // Sometimes CollectGarbageInternal decides to run a different Gc than you requested. Returns
293 // which type of Gc was actually ran.
294 GcType CollectGarbageInternal(GcType gc_plan, bool clear_soft_references)
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700295 LOCKS_EXCLUDED(gc_complete_lock_,
Ian Rogersb726dcb2012-09-05 08:57:23 -0700296 Locks::heap_bitmap_lock_,
297 Locks::mutator_lock_,
298 Locks::thread_suspend_count_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700299 void CollectGarbageMarkSweepPlan(GcType gc_plan, bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700300 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_,
301 Locks::mutator_lock_);
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700302 void CollectGarbageConcurrentMarkSweepPlan(GcType gc_plan, bool clear_soft_references)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700303 LOCKS_EXCLUDED(Locks::heap_bitmap_lock_,
304 Locks::mutator_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700305
Ian Rogers3bb17a62012-01-27 23:56:44 -0800306 // Given the current contents of the alloc space, increase the allowed heap footprint to match
307 // the target utilization ratio. This should only be called immediately after a full garbage
308 // collection.
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800309 void GrowForUtilization();
Carl Shapiro69759ea2011-07-21 18:13:35 -0700310
Mathieu Chartier637e3482012-08-17 10:41:32 -0700311 size_t GetPercentFree();
Elliott Hughesc967f782012-04-16 10:23:15 -0700312
Ian Rogersb726dcb2012-09-05 08:57:23 -0700313 void AddSpace(Space* space) LOCKS_EXCLUDED(Locks::heap_bitmap_lock_);
Ian Rogers30fab402012-01-23 15:43:46 -0800314
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700315 // No thread saftey analysis since we call this everywhere and it is impossible to find a proper
316 // lock ordering for it.
317 void VerifyObjectBody(const Object *obj)
318 NO_THREAD_SAFETY_ANALYSIS;
Elliott Hughes92b3b562011-09-08 16:32:26 -0700319
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700320 static void VerificationCallback(Object* obj, void* arg)
321 SHARED_LOCKS_REQUIRED(GlobalSychronization::heap_bitmap_lock_);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700322
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700323 // Swpa bitmaps (if we are a full Gc then we swap the zygote bitmap too).
324 void SwapBitmaps();
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700325 void SwapStacks();
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700326
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
Mathieu Chartier0325e622012-09-05 14:22:51 -0700332 // One cumulative logger for each type of Gc.
333 typedef SafeMap<GcType, CumulativeLogger*> CumulativeTimings;
334 CumulativeTimings cumulative_timings_;
335
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700336 // The mod-union table remembers all of the references from the image space to the alloc /
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700337 // zygote spaces.
338 UniquePtr<ModUnionTable> mod_union_table_;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700339
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700340 // This table holds all of the references from the zygote space to the alloc space.
341 UniquePtr<ModUnionTable> zygote_mod_union_table_;
342
343 UniquePtr<CardTable> card_table_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700344
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700345 // True for concurrent mark sweep GC, false for mark sweep.
346 const bool concurrent_gc_;
347
348 // If we have a zygote space.
349 bool have_zygote_space_;
350
Ian Rogers5d76c432011-10-31 21:42:49 -0700351 // Used by the image writer to disable card marking on copied objects
352 // TODO: remove
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800353 bool card_marking_disabled_;
Ian Rogers5d76c432011-10-31 21:42:49 -0700354
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700355 // Guards access to the state of GC, associated conditional variable is used to signal when a GC
356 // completes.
357 Mutex* gc_complete_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
358 UniquePtr<ConditionVariable> gc_complete_cond_ GUARDED_BY(gc_complete_lock_);
359
Carl Shapiro58551df2011-07-24 03:09:51 -0700360 // True while the garbage collector is running.
Mathieu Chartier866fb2a2012-09-10 10:47:49 -0700361 volatile bool is_gc_running_ GUARDED_BY(gc_complete_lock_);
362
363 // Last Gc type we ran. Used by WaitForConcurrentGc to know which Gc was waited on.
364 volatile GcType last_gc_type_ 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.
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700379 const bool verify_missing_card_marks_;
380 const bool verify_system_weaks_;
381 const bool verify_pre_gc_heap_;
382 const bool verify_post_gc_heap_;
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700383 const bool verify_mod_union_table_;
384
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700385 // After how many GCs we force to do a partial GC instead of sticky mark bits GC.
386 const size_t partial_gc_frequency_;
387
388 // Sticky mark bits GC has some overhead, so if we have less a few megabytes of AllocSpace then
389 // it's probably better to just do a partial GC.
390 const size_t min_alloc_space_size_for_sticky_gc_;
391
392 // Minimum remaining size for sticky GC. Since sticky GC doesn't free up as much memory as a
393 // normal GC, it is important to not use it when we are almost out of memory.
394 const size_t min_remaining_space_for_sticky_gc_;
395
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700396 // Last trim time
397 uint64_t last_trim_time_;
398
Ian Rogersb726dcb2012-09-05 08:57:23 -0700399 UniquePtr<HeapBitmap> live_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
400 UniquePtr<HeapBitmap> mark_bitmap_ GUARDED_BY(Locks::heap_bitmap_lock_);
Mathieu Chartierb062fdd2012-07-03 09:51:48 -0700401
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700402 // True while the garbage collector is trying to signal the GC daemon thread.
403 // This flag is needed to prevent recursion from occurring when the JNI calls
404 // allocate memory and request another GC.
405 bool try_running_gc_;
406
407 // Used to ensure that we don't ever recursively request GC.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700408 volatile bool requesting_gc_;
Mathieu Chartier7664f5c2012-06-08 18:15:32 -0700409
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700410 // Mark stack that we reuse to avoid re-allocating the mark stack
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700411 UniquePtr<MarkStack> mark_stack_;
Mathieu Chartier5301cd22012-05-31 12:11:36 -0700412
Mathieu Chartier357e9be2012-08-01 11:00:14 -0700413 // Allocation stack, new allocations go here so that we can do sticky mark bits. This enables us
414 // to use the live bitmap as the old mark bitmap.
415 UniquePtr<MarkStack> allocation_stack_;
416
417 // Second allocation stack so that we can process allocation with the heap unlocked.
418 UniquePtr<MarkStack> live_stack_;
419
Brian Carlstrom1f870082011-08-23 16:02:11 -0700420 // offset of java.lang.ref.Reference.referent
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800421 MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700422
423 // offset of java.lang.ref.Reference.queue
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800424 MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700425
426 // offset of java.lang.ref.Reference.queueNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800427 MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700428
429 // offset of java.lang.ref.Reference.pendingNext
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800430 MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700431
432 // offset of java.lang.ref.FinalizerReference.zombie
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800433 MemberOffset finalizer_reference_zombie_offset_;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700434
Brian Carlstrom395520e2011-09-25 19:35:00 -0700435 // Target ideal heap utilization ratio
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800436 float target_utilization_;
Brian Carlstrom395520e2011-09-25 19:35:00 -0700437
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800438 bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700439
Mathieu Chartierc7b83a02012-09-11 18:07:39 -0700440 friend class MarkSweep;
441 friend class VerifyReferenceCardVisitor;
Mathieu Chartierfd678be2012-08-30 14:50:54 -0700442 friend class VerifyReferenceVisitor;
443 friend class VerifyObjectVisitor;
Mathieu Chartierb43b7d42012-06-19 13:15:09 -0700444 friend class ScopedHeapLock;
Ian Rogers30fab402012-01-23 15:43:46 -0800445 FRIEND_TEST(SpaceTest, AllocAndFree);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800446 FRIEND_TEST(SpaceTest, AllocAndFreeList);
Mathieu Chartiercc236d72012-07-20 10:29:05 -0700447 FRIEND_TEST(SpaceTest, ZygoteSpace);
Ian Rogers3bb17a62012-01-27 23:56:44 -0800448 friend class SpaceTest;
Ian Rogers30fab402012-01-23 15:43:46 -0800449
Carl Shapiro69759ea2011-07-21 18:13:35 -0700450 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
451};
452
Carl Shapiro1fb86202011-06-27 17:43:13 -0700453} // namespace art
454
455#endif // ART_SRC_HEAP_H_