blob: 5b71d181803bde1027a9d1a7cf6e0174c5d30045 [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
Carl Shapiro58551df2011-07-24 03:09:51 -070020#include <vector>
21
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070022#include "globals.h"
Elliott Hughes5e71b522011-10-20 13:12:32 -070023#include "heap_bitmap.h"
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070024#include "offsets.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -070025
Elliott Hughes3e465b12011-09-02 18:26:12 -070026#define VERIFY_OBJECT_ENABLED 0
27
Carl Shapiro1fb86202011-06-27 17:43:13 -070028namespace art {
29
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070030class Class;
Elliott Hughes410c0c82011-09-01 17:58:25 -070031class Mutex;
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070032class Object;
Carl Shapiro69759ea2011-07-21 18:13:35 -070033class Space;
34class HeapBitmap;
35
Carl Shapiro1fb86202011-06-27 17:43:13 -070036class Heap {
37 public:
Brian Carlstrom27ec9612011-09-19 20:20:38 -070038 static const size_t kInitialSize = 4 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070039
Brian Carlstrom27ec9612011-09-19 20:20:38 -070040 static const size_t kMaximumSize = 16 * MB;
Carl Shapiro69759ea2011-07-21 18:13:35 -070041
Elliott Hughes410c0c82011-09-01 17:58:25 -070042 typedef void (RootVisitor)(const Object* root, void* arg);
Elliott Hughesc33a32b2011-10-11 18:18:07 -070043 typedef bool (IsMarkedTester)(const Object* object, void* arg);
Brian Carlstrom7e93b502011-08-04 14:16:22 -070044
Brian Carlstrom58ae9412011-10-04 00:56:06 -070045 // Create a heap with the requested sizes. The possible empty
46 // image_file_names names specify Spaces to load based on
47 // ImageWriter output.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070048 static void Init(bool is_verbose_heap, bool is_verbose_gc,
jeffhaoc1160702011-10-27 15:48:45 -070049 size_t starting_size, size_t maximum_size, size_t growth_size,
Brian Carlstrom58ae9412011-10-04 00:56:06 -070050 const std::vector<std::string>& image_file_names);
Carl Shapiro61e019d2011-07-14 16:53:09 -070051
Carl Shapiro69759ea2011-07-21 18:13:35 -070052 static void Destroy();
Brian Carlstroma7f4f482011-07-17 17:01:34 -070053
Brian Carlstrom6b4ef022011-10-23 14:59:04 -070054 static bool IsVerboseHeap() {
55 return is_verbose_heap_;
56 }
57
58 static bool IsVerboseGc() {
59 return is_verbose_gc_;
60 }
61
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070062 // Allocates and initializes storage for an object instance.
Carl Shapiro58551df2011-07-24 03:09:51 -070063 static Object* AllocObject(Class* klass, size_t num_bytes);
Brian Carlstroma7f4f482011-07-17 17:01:34 -070064
Elliott Hughesa2501992011-08-26 19:39:54 -070065 // Check sanity of given reference. Requires the heap lock.
Elliott Hughes3e465b12011-09-02 18:26:12 -070066#if VERIFY_OBJECT_ENABLED
Elliott Hughescf4c6c42011-09-01 15:16:42 -070067 static void VerifyObject(const Object *obj);
Elliott Hughes3e465b12011-09-02 18:26:12 -070068#else
69 static void VerifyObject(const Object *obj) {}
70#endif
Ian Rogers408f79a2011-08-23 18:22:33 -070071
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070072 // Check sanity of all live references. Requires the heap lock.
73 static void VerifyHeap();
74
Elliott Hughesa2501992011-08-26 19:39:54 -070075 // A weaker test than VerifyObject that doesn't require the heap lock,
76 // and doesn't abort on error, allowing the caller to report more
77 // meaningful diagnostics.
Elliott Hughescf4c6c42011-09-01 15:16:42 -070078 static bool IsHeapAddress(const Object* obj);
Elliott Hughesa2501992011-08-26 19:39:54 -070079
Carl Shapiro69759ea2011-07-21 18:13:35 -070080 // Initiates an explicit garbage collection.
81 static void CollectGarbage();
82
Elliott Hughesbf86d042011-08-31 17:53:14 -070083 // Implements java.lang.Runtime.maxMemory.
84 static int64_t GetMaxMemory();
85 // Implements java.lang.Runtime.totalMemory.
86 static int64_t GetTotalMemory();
87 // Implements java.lang.Runtime.freeMemory.
88 static int64_t GetFreeMemory();
89
Elliott Hughes9d5ccec2011-09-19 13:19:50 -070090 // Implements VMDebug.countInstancesOfClass.
91 static int64_t CountInstances(Class* c, bool count_assignable);
92
Elliott Hughes7ede61e2011-09-14 18:18:06 -070093 // Implements dalvik.system.VMRuntime.clearGrowthLimit.
jeffhaoc1160702011-10-27 15:48:45 -070094 static void ClearGrowthLimit();
95
Elliott Hughes7ede61e2011-09-14 18:18:06 -070096 // Implements dalvik.system.VMRuntime.getTargetHeapUtilization.
97 static float GetTargetHeapUtilization() {
Brian Carlstrom395520e2011-09-25 19:35:00 -070098 return target_utilization_;
Elliott Hughes7ede61e2011-09-14 18:18:06 -070099 }
100 // Implements dalvik.system.VMRuntime.setTargetHeapUtilization.
101 static void SetTargetHeapUtilization(float target) {
Brian Carlstrom395520e2011-09-25 19:35:00 -0700102 target_utilization_ = target;
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700103 }
Shih-wei Liao7f1caab2011-10-06 12:11:04 -0700104 // Sets the maximum number of bytes that the heap is allowed to allocate
105 // from the system. Clamps to the appropriate maximum value.
Shih-wei Liao8c2f6412011-10-03 22:58:14 -0700106 static void SetIdealFootprint(size_t max_allowed_footprint);
Elliott Hughes7ede61e2011-09-14 18:18:06 -0700107
Carl Shapiro69759ea2011-07-21 18:13:35 -0700108 // Blocks the caller until the garbage collector becomes idle.
109 static void WaitForConcurrentGcToComplete();
110
Brian Carlstrom24a3c2e2011-10-17 18:07:52 -0700111 static pid_t GetLockOwner(); // For SignalCatcher.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700112 static void Lock();
Elliott Hughes92b3b562011-09-08 16:32:26 -0700113 static void Unlock();
Carl Shapiro61e019d2011-07-14 16:53:09 -0700114
Carl Shapiro58551df2011-07-24 03:09:51 -0700115 static const std::vector<Space*>& GetSpaces() {
116 return spaces_;
117 }
Carl Shapiro61e019d2011-07-14 16:53:09 -0700118
Carl Shapiro58551df2011-07-24 03:09:51 -0700119 static HeapBitmap* GetLiveBits() {
120 return live_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700121 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700122
123 static HeapBitmap* GetMarkBits() {
124 return mark_bitmap_;
Carl Shapiro744ad052011-08-06 15:53:36 -0700125 }
Carl Shapiro58551df2011-07-24 03:09:51 -0700126
Elliott Hughesadb460d2011-10-05 17:02:34 -0700127 static void SetWellKnownClasses(Class* java_lang_ref_FinalizerReference,
128 Class* java_lang_ref_ReferenceQueue);
129
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700130 static void SetReferenceOffsets(MemberOffset reference_referent_offset,
131 MemberOffset reference_queue_offset,
132 MemberOffset reference_queueNext_offset,
133 MemberOffset reference_pendingNext_offset,
Elliott Hughesadb460d2011-10-05 17:02:34 -0700134 MemberOffset finalizer_reference_zombie_offset);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700135
Elliott Hughesadb460d2011-10-05 17:02:34 -0700136 static Object* GetReferenceReferent(Object* reference);
137 static void ClearReferenceReferent(Object* reference);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700138
Elliott Hughesadb460d2011-10-05 17:02:34 -0700139 // Returns true if the reference object has not yet been enqueued.
140 static bool IsEnqueuable(const Object* ref);
141 static void EnqueueReference(Object* ref, Object** list);
142 static void EnqueuePendingReference(Object* ref, Object** list);
143 static Object* DequeuePendingReference(Object** list);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700144
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700145 static MemberOffset GetReferencePendingNextOffset() {
146 DCHECK_NE(reference_pendingNext_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700147 return reference_pendingNext_offset_;
148 }
149
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150 static MemberOffset GetFinalizerReferenceZombieOffset() {
151 DCHECK_NE(finalizer_reference_zombie_offset_.Uint32Value(), 0U);
Brian Carlstrom1f870082011-08-23 16:02:11 -0700152 return finalizer_reference_zombie_offset_;
153 }
154
Elliott Hughes85d15452011-09-16 17:33:01 -0700155 static void EnableObjectValidation() {
156 verify_objects_ = true;
157 }
158
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700159 static void DisableObjectValidation() {
Elliott Hughes85d15452011-09-16 17:33:01 -0700160 verify_objects_ = false;
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700161 }
162
Elliott Hughes92b3b562011-09-08 16:32:26 -0700163 // Callers must hold the heap lock.
Elliott Hughes307f75d2011-10-12 18:04:40 -0700164 static void RecordFreeLocked(size_t freed_objects, size_t freed_bytes);
Brian Carlstrom693267a2011-09-06 09:25:34 -0700165
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700166 // Must be called if a field of an Object in the heap changes, and before any GC safe-point.
167 // The call is not needed if NULL is stored in the field.
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700168 static void WriteBarrier(const Object* object) {
Elliott Hughes5ee7a8b2011-09-13 16:40:07 -0700169#ifdef CONCURRENT_GARBAGE_COLLECTOR
170 // TODO: we need card marking for a concurrent collector.
171 UNIMPLEMENTED(FATAL);
172#endif
Elliott Hughes3a4f8df2011-09-13 15:22:36 -0700173 }
Elliott Hughes5ea047b2011-09-13 14:38:18 -0700174
Elliott Hughesadb460d2011-10-05 17:02:34 -0700175 static void AddFinalizerReference(Object* object);
176
Elliott Hughes7162ad92011-10-27 14:08:42 -0700177 static size_t GetBytesAllocated() { return num_bytes_allocated_; }
178 static size_t GetObjectsAllocated() { return num_objects_allocated_; }
179
Carl Shapiro58551df2011-07-24 03:09:51 -0700180 private:
181 // Allocates uninitialized storage.
Elliott Hughes92b3b562011-09-08 16:32:26 -0700182 static Object* AllocateLocked(size_t num_bytes);
183 static Object* AllocateLocked(Space* space, size_t num_bytes);
Carl Shapiro58551df2011-07-24 03:09:51 -0700184
Elliott Hughesadb460d2011-10-05 17:02:34 -0700185 // Pushes a list of cleared references out to the managed heap.
186 static void EnqueueClearedReferences(Object** cleared_references);
187
Elliott Hughes92b3b562011-09-08 16:32:26 -0700188 static void RecordAllocationLocked(Space* space, const Object* object);
Brian Carlstrom9cff8e12011-08-18 16:47:29 -0700189 static void RecordImageAllocations(Space* space);
Carl Shapiro69759ea2011-07-21 18:13:35 -0700190
191 static void CollectGarbageInternal();
192
193 static void GrowForUtilization();
194
Elliott Hughes92b3b562011-09-08 16:32:26 -0700195 static void VerifyObjectLocked(const Object *obj);
196
Brian Carlstrom78128a62011-09-15 17:21:19 -0700197 static void VerificationCallback(Object* obj, void* arg);
Elliott Hughes92b3b562011-09-08 16:32:26 -0700198
Carl Shapiro69759ea2011-07-21 18:13:35 -0700199 static Mutex* lock_;
200
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700201 static bool is_verbose_heap_;
202
203 static bool is_verbose_gc_;
204
Carl Shapiro58551df2011-07-24 03:09:51 -0700205 static std::vector<Space*> spaces_;
Carl Shapiro69759ea2011-07-21 18:13:35 -0700206
Brian Carlstrom4a289ed2011-08-16 17:17:49 -0700207 // default Space for allocations
208 static Space* alloc_space_;
209
Carl Shapiro69759ea2011-07-21 18:13:35 -0700210 static HeapBitmap* mark_bitmap_;
211
212 static HeapBitmap* live_bitmap_;
213
Carl Shapiro58551df2011-07-24 03:09:51 -0700214 // The maximum size of the heap in bytes.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700215 static size_t maximum_size_;
216
jeffhaoc1160702011-10-27 15:48:45 -0700217 // The largest size the heap may grow. This value allows the app to limit the
218 // growth below the maximum size. This is a work around until we can
219 // dynamically set the maximum size. This value can range between the starting
220 // size and the maximum size but should never be set below the current
221 // footprint of the heap.
222 static size_t growth_size_;
223
Carl Shapiro58551df2011-07-24 03:09:51 -0700224 // True while the garbage collector is running.
Carl Shapiro69759ea2011-07-21 18:13:35 -0700225 static bool is_gc_running_;
226
Carl Shapiro58551df2011-07-24 03:09:51 -0700227 // Number of bytes allocated. Adjusted after each allocation and
228 // free.
229 static size_t num_bytes_allocated_;
230
231 // Number of objects allocated. Adjusted after each allocation and
232 // free.
233 static size_t num_objects_allocated_;
234
Elliott Hughesadb460d2011-10-05 17:02:34 -0700235 static Class* java_lang_ref_FinalizerReference_;
236 static Class* java_lang_ref_ReferenceQueue_;
237
Brian Carlstrom1f870082011-08-23 16:02:11 -0700238 // offset of java.lang.ref.Reference.referent
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700239 static MemberOffset reference_referent_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700240
241 // offset of java.lang.ref.Reference.queue
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700242 static MemberOffset reference_queue_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700243
244 // offset of java.lang.ref.Reference.queueNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700245 static MemberOffset reference_queueNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700246
247 // offset of java.lang.ref.Reference.pendingNext
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700248 static MemberOffset reference_pendingNext_offset_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700249
250 // offset of java.lang.ref.FinalizerReference.zombie
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700251 static MemberOffset finalizer_reference_zombie_offset_;
252
Brian Carlstrom395520e2011-09-25 19:35:00 -0700253 // Target ideal heap utilization ratio
254 static float target_utilization_;
255
Elliott Hughes85d15452011-09-16 17:33:01 -0700256 static bool verify_objects_;
Brian Carlstrom1f870082011-08-23 16:02:11 -0700257
Carl Shapiro69759ea2011-07-21 18:13:35 -0700258 DISALLOW_IMPLICIT_CONSTRUCTORS(Heap);
259};
260
Carl Shapiro1fb86202011-06-27 17:43:13 -0700261} // namespace art
262
263#endif // ART_SRC_HEAP_H_