Mathieu Chartier | bba47a4 | 2012-05-30 10:53:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | import java.util.ArrayList; |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Collections; |
| 20 | import java.util.Map; |
| 21 | import java.util.Random; |
| 22 | |
| 23 | public class ConcurrentGC { |
| 24 | private static final int buckets = 16 * 1024; |
| 25 | private static final int bufferSize = 1024; |
| 26 | |
| 27 | static class ByteContainer { |
| 28 | public byte[] bytes; |
| 29 | } |
| 30 | |
| 31 | public static void main(String[] args) throws Exception { |
| 32 | try { |
| 33 | ByteContainer[] l = new ByteContainer[buckets]; |
| 34 | |
| 35 | for (int i = 0; i < buckets; ++i) { |
| 36 | l[i] = new ByteContainer(); |
| 37 | } |
| 38 | |
| 39 | Random rnd = new Random(123456); |
| 40 | for (int i = 0; i < buckets / 256; ++i) { |
| 41 | int index = rnd.nextInt(buckets); |
| 42 | l[index].bytes = new byte[bufferSize]; |
| 43 | |
| 44 | // Try to get GC to run if we can |
| 45 | Runtime.getRuntime().gc(); |
| 46 | |
| 47 | // Shuffle the array to try cause the lost object problem: |
| 48 | // This problem occurs when an object is white, it may be |
| 49 | // only referenced from a white or grey object. If the white |
| 50 | // object is moved during a CMS to be a black object's field, it |
| 51 | // causes the moved object to not get marked. This can result in |
| 52 | // heap corruption. A typical way to address this issue is by |
| 53 | // having a card table. |
| 54 | // This aspect of the test is meant to ensure that card |
| 55 | // dirtying works and that we check the marked cards after |
| 56 | // marking. |
| 57 | // If these operations are not done, a segfault / failed assert |
| 58 | // should occur. |
| 59 | for (int j = 0; j < l.length; ++j) { |
| 60 | int a = l.length - i - 1; |
| 61 | int b = rnd.nextInt(a); |
| 62 | byte[] temp = l[a].bytes; |
| 63 | l[a].bytes = l[b].bytes; |
| 64 | l[b].bytes = temp; |
| 65 | } |
| 66 | } |
| 67 | } catch (OutOfMemoryError e) { |
| 68 | } |
| 69 | } |
| 70 | } |