Home | History | Annotate | Download | only in gc
      1 /*
      2  * Copyright (C) 2011 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  */
     16 
     17 #include "common_runtime_test.h"
     18 #include "gc/accounting/card_table-inl.h"
     19 #include "gc/accounting/space_bitmap-inl.h"
     20 #include "handle_scope-inl.h"
     21 #include "mirror/class-inl.h"
     22 #include "mirror/object-inl.h"
     23 #include "mirror/object_array-inl.h"
     24 #include "scoped_thread_state_change.h"
     25 
     26 namespace art {
     27 namespace gc {
     28 
     29 class HeapTest : public CommonRuntimeTest {};
     30 
     31 TEST_F(HeapTest, ClearGrowthLimit) {
     32   Heap* heap = Runtime::Current()->GetHeap();
     33   int64_t max_memory_before = heap->GetMaxMemory();
     34   int64_t total_memory_before = heap->GetTotalMemory();
     35   heap->ClearGrowthLimit();
     36   int64_t max_memory_after = heap->GetMaxMemory();
     37   int64_t total_memory_after = heap->GetTotalMemory();
     38   EXPECT_GE(max_memory_after, max_memory_before);
     39   EXPECT_GE(total_memory_after, total_memory_before);
     40 }
     41 
     42 TEST_F(HeapTest, GarbageCollectClassLinkerInit) {
     43   {
     44     ScopedObjectAccess soa(Thread::Current());
     45     // garbage is created during ClassLinker::Init
     46 
     47     StackHandleScope<1> hs(soa.Self());
     48     Handle<mirror::Class> c(
     49         hs.NewHandle(class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/Object;")));
     50     for (size_t i = 0; i < 1024; ++i) {
     51       StackHandleScope<1> hs(soa.Self());
     52       Handle<mirror::ObjectArray<mirror::Object>> array(hs.NewHandle(
     53           mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), c.Get(), 2048)));
     54       for (size_t j = 0; j < 2048; ++j) {
     55         mirror::String* string = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello, world!");
     56         // handle scope operator -> deferences the handle scope before running the method.
     57         array->Set<false>(j, string);
     58       }
     59     }
     60   }
     61   Runtime::Current()->GetHeap()->CollectGarbage(false);
     62 }
     63 
     64 TEST_F(HeapTest, HeapBitmapCapacityTest) {
     65   byte* heap_begin = reinterpret_cast<byte*>(0x1000);
     66   const size_t heap_capacity = kObjectAlignment * (sizeof(intptr_t) * 8 + 1);
     67   std::unique_ptr<accounting::ContinuousSpaceBitmap> bitmap(
     68       accounting::ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
     69   mirror::Object* fake_end_of_heap_object =
     70       reinterpret_cast<mirror::Object*>(&heap_begin[heap_capacity - kObjectAlignment]);
     71   bitmap->Set(fake_end_of_heap_object);
     72 }
     73 
     74 }  // namespace gc
     75 }  // namespace art
     76