blob: a85ad4d040a718a340c21b630e27cfed3cfe9586 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Brian Carlstrom1f870082011-08-23 16:02:11 -070016
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "common_runtime_test.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070018#include "gc/accounting/card_table-inl.h"
19#include "gc/accounting/space_bitmap-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/class-inl.h"
21#include "mirror/object-inl.h"
22#include "mirror/object_array-inl.h"
Ian Rogers1f539342012-10-03 21:09:42 -070023#include "sirt_ref.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070024
25namespace art {
Ian Rogers1d54e732013-05-02 21:10:01 -070026namespace gc {
Brian Carlstrom1f870082011-08-23 16:02:11 -070027
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080028class HeapTest : public CommonRuntimeTest {};
Brian Carlstrom1f870082011-08-23 16:02:11 -070029
jeffhaoc1160702011-10-27 15:48:45 -070030TEST_F(HeapTest, ClearGrowthLimit) {
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080031 Heap* heap = Runtime::Current()->GetHeap();
32 int64_t max_memory_before = heap->GetMaxMemory();
33 int64_t total_memory_before = heap->GetTotalMemory();
34 heap->ClearGrowthLimit();
35 int64_t max_memory_after = heap->GetMaxMemory();
36 int64_t total_memory_after = heap->GetTotalMemory();
jeffhaoc1160702011-10-27 15:48:45 -070037 EXPECT_GE(max_memory_after, max_memory_before);
38 EXPECT_GE(total_memory_after, total_memory_before);
39}
40
Brian Carlstrom693267a2011-09-06 09:25:34 -070041TEST_F(HeapTest, GarbageCollectClassLinkerInit) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070042 {
43 ScopedObjectAccess soa(Thread::Current());
44 // garbage is created during ClassLinker::Init
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070045
Ian Rogers98379392014-02-24 16:53:16 -080046 SirtRef<mirror::Class> c(soa.Self(), class_linker_->FindSystemClass(soa.Self(),
47 "[Ljava/lang/Object;"));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070048 for (size_t i = 0; i < 1024; ++i) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080049 SirtRef<mirror::ObjectArray<mirror::Object> > array(soa.Self(),
Mathieu Chartier590fee92013-09-13 13:46:47 -070050 mirror::ObjectArray<mirror::Object>::Alloc(soa.Self(), c.get(), 2048));
Ian Rogers00f7d0e2012-07-19 15:28:27 -070051 for (size_t j = 0; j < 2048; ++j) {
Mathieu Chartier590fee92013-09-13 13:46:47 -070052 mirror::String* string = mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello, world!");
53 // SIRT operator -> deferences the SIRT before running the method.
Sebastien Hertzd2fe10a2014-01-15 10:20:56 +010054 array->Set<false>(j, string);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 }
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070056 }
57 }
Elliott Hughesb3bd5f02012-03-08 21:05:27 -080058 Runtime::Current()->GetHeap()->CollectGarbage(false);
Brian Carlstrom1f870082011-08-23 16:02:11 -070059}
60
Mathieu Chartier61c9cb52012-07-03 14:39:54 -070061TEST_F(HeapTest, HeapBitmapCapacityTest) {
62 byte* heap_begin = reinterpret_cast<byte*>(0x1000);
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070063 const size_t heap_capacity = kObjectAlignment * (sizeof(intptr_t) * 8 + 1);
64 UniquePtr<accounting::ContinuousSpaceBitmap> bitmap(
65 accounting::ContinuousSpaceBitmap::Create("test bitmap", heap_begin, heap_capacity));
Ian Rogers1d54e732013-05-02 21:10:01 -070066 mirror::Object* fake_end_of_heap_object =
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070067 reinterpret_cast<mirror::Object*>(&heap_begin[heap_capacity - kObjectAlignment]);
Ian Rogers1d54e732013-05-02 21:10:01 -070068 bitmap->Set(fake_end_of_heap_object);
Mathieu Chartier61c9cb52012-07-03 14:39:54 -070069}
70
Ian Rogers1d54e732013-05-02 21:10:01 -070071} // namespace gc
Ian Rogers0cfe1fb2011-08-26 03:29:44 -070072} // namespace art