12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoader.hpp"
27 #include "classfile/javaClasses.hpp"
28 #include "gc/shared/gcLocker.inline.hpp"
29 #include "gc/shared/genCollectedHeap.hpp"
30 #include "gc/shared/vmGCOperations.hpp"
31 #include "memory/oopFactory.hpp"
32 #include "oops/instanceKlass.hpp"
33 #include "oops/instanceRefKlass.hpp"
34 #include "runtime/handles.inline.hpp"
35 #include "runtime/init.hpp"
36 #include "runtime/interfaceSupport.hpp"
37 #include "utilities/dtrace.hpp"
38 #include "utilities/macros.hpp"
39 #include "utilities/preserveException.hpp"
40 #if INCLUDE_ALL_GCS
41 #include "gc/g1/g1CollectedHeap.inline.hpp"
42 #endif // INCLUDE_ALL_GCS
43
44 VM_GC_Operation::~VM_GC_Operation() {
45 CollectedHeap* ch = Universe::heap();
46 ch->collector_policy()->set_all_soft_refs_clear(false);
47 }
48
49 // The same dtrace probe can't be inserted in two different files, so we
50 // have to call it here, so it's only in one file. Can't create new probes
51 // for the other file anymore. The dtrace probes have to remain stable.
199 G1CollectedHeap* g1h = G1CollectedHeap::heap();
200 g1h->g1_policy()->collector_state()->set_initiate_conc_mark_if_possible(true);
201
202 GCCauseSetter x(g1h, _gc_cause);
203
204 // At this point we are supposed to start a concurrent cycle. We
205 // will do so if one is not already in progress.
206 bool should_start = g1h->g1_policy()->force_initial_mark_if_outside_cycle(_gc_cause);
207
208 if (should_start) {
209 double pause_target = g1h->g1_policy()->max_pause_time_ms();
210 g1h->do_collection_pause_at_safepoint(pause_target);
211 }
212 return true;
213 }
214 #endif
215
216 return false;
217 }
218
219 static void log_metaspace_alloc_failure_for_concurrent_GC() {
220 if (Verbose && PrintGCDetails) {
221 if (UseConcMarkSweepGC) {
222 gclog_or_tty->print_cr("\nCMS full GC for Metaspace");
223 } else if (UseG1GC) {
224 gclog_or_tty->print_cr("\nG1 full GC for Metaspace");
225 }
226 }
227 }
228
229 void VM_CollectForMetadataAllocation::doit() {
230 SvcGCMarker sgcm(SvcGCMarker::FULL);
231
232 CollectedHeap* heap = Universe::heap();
233 GCCauseSetter gccs(heap, _gc_cause);
234
235 // Check again if the space is available. Another thread
236 // may have similarly failed a metadata allocation and induced
237 // a GC that freed space for the allocation.
238 if (!MetadataAllocationFailALot) {
239 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
240 if (_result != NULL) {
241 return;
242 }
243 }
244
245 if (initiate_concurrent_GC()) {
246 // For CMS and G1 expand since the collection is going to be concurrent.
247 _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype);
248 if (_result != NULL) {
249 return;
250 }
251
252 log_metaspace_alloc_failure_for_concurrent_GC();
253 }
254
255 // Don't clear the soft refs yet.
256 heap->collect_as_vm_thread(GCCause::_metadata_GC_threshold);
257 // After a GC try to allocate without expanding. Could fail
258 // and expansion will be tried below.
259 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
260 if (_result != NULL) {
261 return;
262 }
263
264 // If still failing, allow the Metaspace to expand.
265 // See delta_capacity_until_GC() for explanation of the
266 // amount of the expansion.
267 // This should work unless there really is no more space
268 // or a MaxMetaspaceSize has been specified on the command line.
269 _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype);
270 if (_result != NULL) {
271 return;
272 }
273
274 // If expansion failed, do a last-ditch collection and try allocating
275 // again. A last-ditch collection will clear softrefs. This
276 // behavior is similar to the last-ditch collection done for perm
277 // gen when it was full and a collection for failed allocation
278 // did not free perm gen space.
279 heap->collect_as_vm_thread(GCCause::_last_ditch_collection);
280 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
281 if (_result != NULL) {
282 return;
283 }
284
285 if (Verbose && PrintGCDetails) {
286 gclog_or_tty->print_cr("\nAfter Metaspace GC failed to allocate size "
287 SIZE_FORMAT, _size);
288 }
289
290 if (GC_locker::is_active_and_needs_gc()) {
291 set_gc_locked();
292 }
293 }
|
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25 #include "precompiled.hpp"
26 #include "classfile/classLoader.hpp"
27 #include "classfile/javaClasses.hpp"
28 #include "gc/shared/gcLocker.inline.hpp"
29 #include "gc/shared/genCollectedHeap.hpp"
30 #include "gc/shared/vmGCOperations.hpp"
31 #include "memory/oopFactory.hpp"
32 #include "logging/log.hpp"
33 #include "oops/instanceKlass.hpp"
34 #include "oops/instanceRefKlass.hpp"
35 #include "runtime/handles.inline.hpp"
36 #include "runtime/init.hpp"
37 #include "runtime/interfaceSupport.hpp"
38 #include "utilities/dtrace.hpp"
39 #include "utilities/macros.hpp"
40 #include "utilities/preserveException.hpp"
41 #if INCLUDE_ALL_GCS
42 #include "gc/g1/g1CollectedHeap.inline.hpp"
43 #endif // INCLUDE_ALL_GCS
44
45 VM_GC_Operation::~VM_GC_Operation() {
46 CollectedHeap* ch = Universe::heap();
47 ch->collector_policy()->set_all_soft_refs_clear(false);
48 }
49
50 // The same dtrace probe can't be inserted in two different files, so we
51 // have to call it here, so it's only in one file. Can't create new probes
52 // for the other file anymore. The dtrace probes have to remain stable.
200 G1CollectedHeap* g1h = G1CollectedHeap::heap();
201 g1h->g1_policy()->collector_state()->set_initiate_conc_mark_if_possible(true);
202
203 GCCauseSetter x(g1h, _gc_cause);
204
205 // At this point we are supposed to start a concurrent cycle. We
206 // will do so if one is not already in progress.
207 bool should_start = g1h->g1_policy()->force_initial_mark_if_outside_cycle(_gc_cause);
208
209 if (should_start) {
210 double pause_target = g1h->g1_policy()->max_pause_time_ms();
211 g1h->do_collection_pause_at_safepoint(pause_target);
212 }
213 return true;
214 }
215 #endif
216
217 return false;
218 }
219
220 void VM_CollectForMetadataAllocation::doit() {
221 SvcGCMarker sgcm(SvcGCMarker::FULL);
222
223 CollectedHeap* heap = Universe::heap();
224 GCCauseSetter gccs(heap, _gc_cause);
225
226 // Check again if the space is available. Another thread
227 // may have similarly failed a metadata allocation and induced
228 // a GC that freed space for the allocation.
229 if (!MetadataAllocationFailALot) {
230 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
231 if (_result != NULL) {
232 return;
233 }
234 }
235
236 if (initiate_concurrent_GC()) {
237 // For CMS and G1 expand since the collection is going to be concurrent.
238 _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype);
239 if (_result != NULL) {
240 return;
241 }
242
243 log_debug(gc)("%s full GC for Metaspace", UseConcMarkSweepGC ? "CMS" : "G1");
244 }
245
246 // Don't clear the soft refs yet.
247 heap->collect_as_vm_thread(GCCause::_metadata_GC_threshold);
248 // After a GC try to allocate without expanding. Could fail
249 // and expansion will be tried below.
250 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
251 if (_result != NULL) {
252 return;
253 }
254
255 // If still failing, allow the Metaspace to expand.
256 // See delta_capacity_until_GC() for explanation of the
257 // amount of the expansion.
258 // This should work unless there really is no more space
259 // or a MaxMetaspaceSize has been specified on the command line.
260 _result = _loader_data->metaspace_non_null()->expand_and_allocate(_size, _mdtype);
261 if (_result != NULL) {
262 return;
263 }
264
265 // If expansion failed, do a last-ditch collection and try allocating
266 // again. A last-ditch collection will clear softrefs. This
267 // behavior is similar to the last-ditch collection done for perm
268 // gen when it was full and a collection for failed allocation
269 // did not free perm gen space.
270 heap->collect_as_vm_thread(GCCause::_last_ditch_collection);
271 _result = _loader_data->metaspace_non_null()->allocate(_size, _mdtype);
272 if (_result != NULL) {
273 return;
274 }
275
276 log_debug(gc)("After Metaspace GC failed to allocate size " SIZE_FORMAT, _size);
277
278 if (GC_locker::is_active_and_needs_gc()) {
279 set_gc_locked();
280 }
281 }
|