347 HeapWord* new_high = (HeapWord*) _virtual_space.high(); 348 MemRegion mangle_region(prev_high, new_high); 349 SpaceMangler::mangle_region(mangle_region); 350 } 351 352 // Do not attempt an expand-to-the reserve size. The 353 // request should properly observe the maximum size of 354 // the generation so an expand-to-reserve should be 355 // unnecessary. Also a second call to expand-to-reserve 356 // value potentially can cause an undue expansion. 357 // For example if the first expand fail for unknown reasons, 358 // but the second succeeds and expands the heap to its maximum 359 // value. 360 if (GC_locker::is_active()) { 361 log_debug(gc)("Garbage collection disabled, expanded heap instead"); 362 } 363 364 return success; 365 } 366 367 void DefNewGeneration::compute_new_size() { 368 // This is called after a GC that includes the old generation, so from-space 369 // will normally be empty. 370 // Note that we check both spaces, since if scavenge failed they revert roles. 371 // If not we bail out (otherwise we would have to relocate the objects). 372 if (!from()->is_empty() || !to()->is_empty()) { 373 return; 374 } 375 376 GenCollectedHeap* gch = GenCollectedHeap::heap(); 377 378 size_t old_size = gch->old_gen()->capacity(); 379 size_t new_size_before = _virtual_space.committed_size(); 380 size_t min_new_size = initial_size(); 381 size_t max_new_size = reserved().byte_size(); 382 assert(min_new_size <= new_size_before && 383 new_size_before <= max_new_size, 384 "just checking"); 385 // All space sizes must be multiples of Generation::GenGrain. 386 size_t alignment = Generation::GenGrain; 387 388 // Compute desired new generation size based on NewRatio and 389 // NewSizeThreadIncrease 390 size_t desired_new_size = old_size/NewRatio; 391 int threads_count = Threads::number_of_non_daemon_threads(); 392 size_t thread_increase_size = threads_count * NewSizeThreadIncrease; 393 desired_new_size = align_size_up(desired_new_size + thread_increase_size, alignment); 394 395 // Adjust new generation size 396 desired_new_size = MAX2(MIN2(desired_new_size, max_new_size), min_new_size); 397 assert(desired_new_size <= max_new_size, "just checking"); 398 399 bool changed = false; 400 if (desired_new_size > new_size_before) { 401 size_t change = desired_new_size - new_size_before; 402 assert(change % alignment == 0, "just checking"); 403 if (expand(change)) { 404 changed = true; 405 } 406 // If the heap failed to expand to the desired size, 407 // "changed" will be false. If the expansion failed 408 // (and at this point it was expected to succeed), 409 // ignore the failure (leaving "changed" as false). 410 } 411 if (desired_new_size < new_size_before && eden()->is_empty()) { 412 // bail out of shrinking if objects in eden 413 size_t change = new_size_before - desired_new_size; | 347 HeapWord* new_high = (HeapWord*) _virtual_space.high(); 348 MemRegion mangle_region(prev_high, new_high); 349 SpaceMangler::mangle_region(mangle_region); 350 } 351 352 // Do not attempt an expand-to-the reserve size. The 353 // request should properly observe the maximum size of 354 // the generation so an expand-to-reserve should be 355 // unnecessary. Also a second call to expand-to-reserve 356 // value potentially can cause an undue expansion. 357 // For example if the first expand fail for unknown reasons, 358 // but the second succeeds and expands the heap to its maximum 359 // value. 360 if (GC_locker::is_active()) { 361 log_debug(gc)("Garbage collection disabled, expanded heap instead"); 362 } 363 364 return success; 365 } 366 367 size_t DefNewGeneration::adjust_for_thread_increase(size_t new_size_candidate, 368 size_t new_size_before, 369 size_t alignment) const { 370 size_t desired_new_size = new_size_before; 371 372 if (NewSizeThreadIncrease > 0) { 373 int threads_count; 374 size_t thread_increase_size = 0; 375 376 // 1. Check an overflow at 'threads_count * NewSizeThreadIncrease'. 377 threads_count = Threads::number_of_non_daemon_threads(); 378 if (NewSizeThreadIncrease <= max_uintx / threads_count) { 379 thread_increase_size = threads_count * NewSizeThreadIncrease; 380 381 // 2. Check an overflow at 'new_size_candidate + thread_increase_size'. 382 if (new_size_candidate <= max_uintx - thread_increase_size) { 383 new_size_candidate += thread_increase_size; 384 385 // 3. Check an overflow at 'align_size_up'. 386 size_t aligned_max = ((max_uintx - alignment) & ~(alignment-1)); 387 if (new_size_candidate <= aligned_max) { 388 desired_new_size = align_size_up(new_size_candidate, alignment); 389 } 390 } 391 } 392 } 393 394 return desired_new_size; 395 } 396 397 void DefNewGeneration::compute_new_size() { 398 // This is called after a GC that includes the old generation, so from-space 399 // will normally be empty. 400 // Note that we check both spaces, since if scavenge failed they revert roles. 401 // If not we bail out (otherwise we would have to relocate the objects). 402 if (!from()->is_empty() || !to()->is_empty()) { 403 return; 404 } 405 406 GenCollectedHeap* gch = GenCollectedHeap::heap(); 407 408 size_t old_size = gch->old_gen()->capacity(); 409 size_t new_size_before = _virtual_space.committed_size(); 410 size_t min_new_size = initial_size(); 411 size_t max_new_size = reserved().byte_size(); 412 assert(min_new_size <= new_size_before && 413 new_size_before <= max_new_size, 414 "just checking"); 415 // All space sizes must be multiples of Generation::GenGrain. 416 size_t alignment = Generation::GenGrain; 417 418 int threads_count = 0; 419 size_t thread_increase_size = 0; 420 421 size_t new_size_candidate = old_size / NewRatio; 422 // Compute desired new generation size based on NewRatio and NewSizeThreadIncrease 423 // and reverts to previous value if any overflow happens 424 size_t desired_new_size = adjust_for_thread_increase(new_size_candidate, new_size_before, alignment); 425 426 // Adjust new generation size 427 desired_new_size = MAX2(MIN2(desired_new_size, max_new_size), min_new_size); 428 assert(desired_new_size <= max_new_size, "just checking"); 429 430 bool changed = false; 431 if (desired_new_size > new_size_before) { 432 size_t change = desired_new_size - new_size_before; 433 assert(change % alignment == 0, "just checking"); 434 if (expand(change)) { 435 changed = true; 436 } 437 // If the heap failed to expand to the desired size, 438 // "changed" will be false. If the expansion failed 439 // (and at this point it was expected to succeed), 440 // ignore the failure (leaving "changed" as false). 441 } 442 if (desired_new_size < new_size_before && eden()->is_empty()) { 443 // bail out of shrinking if objects in eden 444 size_t change = new_size_before - desired_new_size; |