45 #include "runtime/thread.hpp"
46 #include "runtime/vmThread.hpp"
47 #include "utilities/copy.hpp"
48 #include "utilities/events.hpp"
49
50 class HeapRegion;
51
52 void G1MarkSweep::invoke_at_safepoint(ReferenceProcessor* rp,
53 bool clear_all_softrefs) {
54 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
55
56 SharedHeap* sh = SharedHeap::heap();
57 #ifdef ASSERT
58 if (sh->collector_policy()->should_clear_all_soft_refs()) {
59 assert(clear_all_softrefs, "Policy should have been checked earler");
60 }
61 #endif
62 // hook up weak ref data so it can be used during Mark-Sweep
63 assert(GenMarkSweep::ref_processor() == NULL, "no stomping");
64 assert(rp != NULL, "should be non-NULL");
65 GenMarkSweep::_ref_processor = rp;
66 rp->setup_policy(clear_all_softrefs);
67
68 // When collecting the permanent generation methodOops may be moving,
69 // so we either have to flush all bcp data or convert it into bci.
70 CodeCache::gc_prologue();
71 Threads::gc_prologue();
72
73 // Increment the invocation count for the permanent generation, since it is
74 // implicitly collected whenever we do a full mark sweep collection.
75 sh->perm_gen()->stat_record()->invocations++;
76
77 bool marked_for_unloading = false;
78
79 allocate_stacks();
80
81 // We should save the marks of the currently locked biased monitors.
82 // The marking doesn't preserve the marks of biased objects.
83 BiasedLocking::preserve_marks();
84
122 }
123
124 void G1MarkSweep::mark_sweep_phase1(bool& marked_for_unloading,
125 bool clear_all_softrefs) {
126 // Recursively traverse all live objects and mark them
127 EventMark m("1 mark object");
128 TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty);
129 GenMarkSweep::trace(" 1");
130
131 SharedHeap* sh = SharedHeap::heap();
132
133 sh->process_strong_roots(true, // activeate StrongRootsScope
134 true, // Collecting permanent generation.
135 SharedHeap::SO_SystemClasses,
136 &GenMarkSweep::follow_root_closure,
137 &GenMarkSweep::follow_code_root_closure,
138 &GenMarkSweep::follow_root_closure);
139
140 // Process reference objects found during marking
141 ReferenceProcessor* rp = GenMarkSweep::ref_processor();
142 rp->setup_policy(clear_all_softrefs);
143 rp->process_discovered_references(&GenMarkSweep::is_alive,
144 &GenMarkSweep::keep_alive,
145 &GenMarkSweep::follow_stack_closure,
146 NULL);
147
148 // Follow system dictionary roots and unload classes
149 bool purged_class = SystemDictionary::do_unloading(&GenMarkSweep::is_alive);
150 assert(GenMarkSweep::_marking_stack.is_empty(),
151 "stack should be empty by now");
152
153 // Follow code cache roots (has to be done after system dictionary,
154 // assumes all live klasses are marked)
155 CodeCache::do_unloading(&GenMarkSweep::is_alive,
156 &GenMarkSweep::keep_alive,
157 purged_class);
158 GenMarkSweep::follow_stack();
159
160 // Update subklass/sibling/implementor links of live klasses
161 GenMarkSweep::follow_weak_klass_links();
162 assert(GenMarkSweep::_marking_stack.is_empty(),
163 "stack should be empty by now");
164
165 // Visit memoized MDO's and clear any unmarked weak refs
166 GenMarkSweep::follow_mdo_weak_refs();
167 assert(GenMarkSweep::_marking_stack.is_empty(), "just drained");
168
169
170 // Visit interned string tables and delete unmarked oops
171 StringTable::unlink(&GenMarkSweep::is_alive);
172 // Clean up unreferenced symbols in symbol table.
173 SymbolTable::unlink();
174
175 assert(GenMarkSweep::_marking_stack.is_empty(),
176 "stack should be empty by now");
177
178 if (VerifyDuringGC) {
179 HandleMark hm; // handle scope
180 COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact);
181 gclog_or_tty->print(" VerifyDuringGC:(full)[Verifying ");
182 Universe::heap()->prepare_for_verify();
183 // Note: we can verify only the heap here. When an object is
184 // marked, the previous value of the mark word (including
185 // identity hash values, ages, etc) is preserved, and the mark
186 // word is set to markOop::marked_value - effectively removing
187 // any hash values from the mark word. These hash values are
188 // used when verifying the dictionaries and so removing them
189 // from the mark word can make verification of the dictionaries
329 };
330
331 void G1MarkSweep::mark_sweep_phase3() {
332 G1CollectedHeap* g1h = G1CollectedHeap::heap();
333 Generation* pg = g1h->perm_gen();
334
335 // Adjust the pointers to reflect the new locations
336 EventMark m("3 adjust pointers");
337 TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty);
338 GenMarkSweep::trace("3");
339
340 SharedHeap* sh = SharedHeap::heap();
341
342 sh->process_strong_roots(true, // activate StrongRootsScope
343 true, // Collecting permanent generation.
344 SharedHeap::SO_AllClasses,
345 &GenMarkSweep::adjust_root_pointer_closure,
346 NULL, // do not touch code cache here
347 &GenMarkSweep::adjust_pointer_closure);
348
349 g1h->ref_processor()->weak_oops_do(&GenMarkSweep::adjust_root_pointer_closure);
350
351 // Now adjust pointers in remaining weak roots. (All of which should
352 // have been cleared if they pointed to non-surviving objects.)
353 g1h->g1_process_weak_roots(&GenMarkSweep::adjust_root_pointer_closure,
354 &GenMarkSweep::adjust_pointer_closure);
355
356 GenMarkSweep::adjust_marks();
357
358 G1AdjustPointersClosure blk;
359 g1h->heap_region_iterate(&blk);
360 pg->adjust_pointers();
361 }
362
363 class G1SpaceCompactClosure: public HeapRegionClosure {
364 public:
365 G1SpaceCompactClosure() {}
366
367 bool doHeapRegion(HeapRegion* hr) {
368 if (hr->isHumongous()) {
369 if (hr->startsHumongous()) {
|
45 #include "runtime/thread.hpp"
46 #include "runtime/vmThread.hpp"
47 #include "utilities/copy.hpp"
48 #include "utilities/events.hpp"
49
50 class HeapRegion;
51
52 void G1MarkSweep::invoke_at_safepoint(ReferenceProcessor* rp,
53 bool clear_all_softrefs) {
54 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
55
56 SharedHeap* sh = SharedHeap::heap();
57 #ifdef ASSERT
58 if (sh->collector_policy()->should_clear_all_soft_refs()) {
59 assert(clear_all_softrefs, "Policy should have been checked earler");
60 }
61 #endif
62 // hook up weak ref data so it can be used during Mark-Sweep
63 assert(GenMarkSweep::ref_processor() == NULL, "no stomping");
64 assert(rp != NULL, "should be non-NULL");
65 assert(rp == G1CollectedHeap::heap()->ref_processor_stw(), "Precondition");
66
67 GenMarkSweep::_ref_processor = rp;
68 rp->setup_policy(clear_all_softrefs);
69
70 // When collecting the permanent generation methodOops may be moving,
71 // so we either have to flush all bcp data or convert it into bci.
72 CodeCache::gc_prologue();
73 Threads::gc_prologue();
74
75 // Increment the invocation count for the permanent generation, since it is
76 // implicitly collected whenever we do a full mark sweep collection.
77 sh->perm_gen()->stat_record()->invocations++;
78
79 bool marked_for_unloading = false;
80
81 allocate_stacks();
82
83 // We should save the marks of the currently locked biased monitors.
84 // The marking doesn't preserve the marks of biased objects.
85 BiasedLocking::preserve_marks();
86
124 }
125
126 void G1MarkSweep::mark_sweep_phase1(bool& marked_for_unloading,
127 bool clear_all_softrefs) {
128 // Recursively traverse all live objects and mark them
129 EventMark m("1 mark object");
130 TraceTime tm("phase 1", PrintGC && Verbose, true, gclog_or_tty);
131 GenMarkSweep::trace(" 1");
132
133 SharedHeap* sh = SharedHeap::heap();
134
135 sh->process_strong_roots(true, // activeate StrongRootsScope
136 true, // Collecting permanent generation.
137 SharedHeap::SO_SystemClasses,
138 &GenMarkSweep::follow_root_closure,
139 &GenMarkSweep::follow_code_root_closure,
140 &GenMarkSweep::follow_root_closure);
141
142 // Process reference objects found during marking
143 ReferenceProcessor* rp = GenMarkSweep::ref_processor();
144 assert(rp == G1CollectedHeap::heap()->ref_processor_stw(), "Sanity");
145
146 rp->setup_policy(clear_all_softrefs);
147 rp->process_discovered_references(&GenMarkSweep::is_alive,
148 &GenMarkSweep::keep_alive,
149 &GenMarkSweep::follow_stack_closure,
150 NULL);
151
152 // Follow system dictionary roots and unload classes
153 bool purged_class = SystemDictionary::do_unloading(&GenMarkSweep::is_alive);
154 assert(GenMarkSweep::_marking_stack.is_empty(),
155 "stack should be empty by now");
156
157 // Follow code cache roots (has to be done after system dictionary,
158 // assumes all live klasses are marked)
159 CodeCache::do_unloading(&GenMarkSweep::is_alive,
160 &GenMarkSweep::keep_alive,
161 purged_class);
162 GenMarkSweep::follow_stack();
163
164 // Update subklass/sibling/implementor links of live klasses
165 GenMarkSweep::follow_weak_klass_links();
166 assert(GenMarkSweep::_marking_stack.is_empty(),
167 "stack should be empty by now");
168
169 // Visit memoized MDO's and clear any unmarked weak refs
170 GenMarkSweep::follow_mdo_weak_refs();
171 assert(GenMarkSweep::_marking_stack.is_empty(), "just drained");
172
173 // Visit interned string tables and delete unmarked oops
174 StringTable::unlink(&GenMarkSweep::is_alive);
175 // Clean up unreferenced symbols in symbol table.
176 SymbolTable::unlink();
177
178 assert(GenMarkSweep::_marking_stack.is_empty(),
179 "stack should be empty by now");
180
181 if (VerifyDuringGC) {
182 HandleMark hm; // handle scope
183 COMPILER2_PRESENT(DerivedPointerTableDeactivate dpt_deact);
184 gclog_or_tty->print(" VerifyDuringGC:(full)[Verifying ");
185 Universe::heap()->prepare_for_verify();
186 // Note: we can verify only the heap here. When an object is
187 // marked, the previous value of the mark word (including
188 // identity hash values, ages, etc) is preserved, and the mark
189 // word is set to markOop::marked_value - effectively removing
190 // any hash values from the mark word. These hash values are
191 // used when verifying the dictionaries and so removing them
192 // from the mark word can make verification of the dictionaries
332 };
333
334 void G1MarkSweep::mark_sweep_phase3() {
335 G1CollectedHeap* g1h = G1CollectedHeap::heap();
336 Generation* pg = g1h->perm_gen();
337
338 // Adjust the pointers to reflect the new locations
339 EventMark m("3 adjust pointers");
340 TraceTime tm("phase 3", PrintGC && Verbose, true, gclog_or_tty);
341 GenMarkSweep::trace("3");
342
343 SharedHeap* sh = SharedHeap::heap();
344
345 sh->process_strong_roots(true, // activate StrongRootsScope
346 true, // Collecting permanent generation.
347 SharedHeap::SO_AllClasses,
348 &GenMarkSweep::adjust_root_pointer_closure,
349 NULL, // do not touch code cache here
350 &GenMarkSweep::adjust_pointer_closure);
351
352 assert(GenMarkSweep::ref_processor() == g1h->ref_processor_stw(), "Sanity");
353 g1h->ref_processor_stw()->weak_oops_do(&GenMarkSweep::adjust_root_pointer_closure);
354
355 // Now adjust pointers in remaining weak roots. (All of which should
356 // have been cleared if they pointed to non-surviving objects.)
357 g1h->g1_process_weak_roots(&GenMarkSweep::adjust_root_pointer_closure,
358 &GenMarkSweep::adjust_pointer_closure);
359
360 GenMarkSweep::adjust_marks();
361
362 G1AdjustPointersClosure blk;
363 g1h->heap_region_iterate(&blk);
364 pg->adjust_pointers();
365 }
366
367 class G1SpaceCompactClosure: public HeapRegionClosure {
368 public:
369 G1SpaceCompactClosure() {}
370
371 bool doHeapRegion(HeapRegion* hr) {
372 if (hr->isHumongous()) {
373 if (hr->startsHumongous()) {
|