Fix new space shrinking to reset from-space.

R=lrn@chromium.org
BUG=v8:1702
TEST=cctest/test-heap/GrowAndShrinkNewSpace

Review URL: http://codereview.chromium.org/7976003

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@9346 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 50a94139
......@@ -926,10 +926,10 @@ void NewSpace::Flip() {
void NewSpace::Grow() {
ASSERT(Capacity() < MaximumCapacity());
if (to_space_.Grow()) {
// Only grow from space if we managed to grow to space.
// Only grow from space if we managed to grow to-space.
if (!from_space_.Grow()) {
// If we managed to grow to space but couldn't grow from space,
// attempt to shrink to space.
// If we managed to grow to-space but couldn't grow from-space,
// attempt to shrink to-space.
if (!to_space_.ShrinkTo(from_space_.Capacity())) {
// We are in an inconsistent state because we could not
// commit/uncommit memory from new space.
......@@ -947,10 +947,11 @@ void NewSpace::Shrink() {
RoundUp(new_capacity, static_cast<int>(OS::AllocateAlignment()));
if (rounded_new_capacity < Capacity() &&
to_space_.ShrinkTo(rounded_new_capacity)) {
// Only shrink from space if we managed to shrink to space.
// Only shrink from-space if we managed to shrink to-space.
from_space_.Reset();
if (!from_space_.ShrinkTo(rounded_new_capacity)) {
// If we managed to shrink to space but couldn't shrink from
// space, attempt to grow to space again.
// If we managed to shrink to-space but couldn't shrink from
// space, attempt to grow to-space again.
if (!to_space_.GrowTo(from_space_.Capacity())) {
// We are in an inconsistent state because we could not
// commit/uncommit memory from new space.
......
......@@ -1221,6 +1221,50 @@ TEST(TestSizeOfObjectsVsHeapIteratorPrecision) {
}
TEST(GrowAndShrinkNewSpace) {
InitializeVM();
v8::HandleScope scope;
NewSpace* new_space = HEAP->new_space();
// Explicitly growing should double the space capacity.
int old_capacity, new_capacity;
old_capacity = new_space->Capacity();
new_space->Grow();
new_capacity = new_space->Capacity();
ASSERT_EQ(2 * old_capacity, new_capacity);
// Fill up new space to the point that it exceeds old capacity.
while (new_space->SizeAsInt() <= old_capacity) {
Handle<FixedArray> filler = FACTORY->NewFixedArray(1000, NOT_TENURED);
ASSERT(HEAP->InNewSpace(*FACTORY->NewFixedArray(1000, NOT_TENURED)));
}
// Explicitly shrinking should not affect space capacity.
old_capacity = new_space->Capacity();
new_space->Shrink();
new_capacity = new_space->Capacity();
ASSERT_EQ(old_capacity, new_capacity);
// Perform scavenge to empty the new space.
HEAP->CollectGarbage(NEW_SPACE);
ASSERT_LE(new_space->SizeAsInt(), old_capacity);
// Explicitly shrinking should halve the space capacity.
old_capacity = new_space->Capacity();
new_space->Shrink();
new_capacity = new_space->Capacity();
ASSERT_EQ(old_capacity, 2 * new_capacity);
// Consecutive shrinking should not affect space capacity.
old_capacity = new_space->Capacity();
new_space->Shrink();
new_space->Shrink();
new_space->Shrink();
new_capacity = new_space->Capacity();
ASSERT_EQ(old_capacity, new_capacity);
}
class HeapIteratorTestHelper {
public:
HeapIteratorTestHelper(Object* a, Object* b)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment