blob: ac1e131d99fab20f839e6df65cef00889c512f35 [file] [log] [blame]
Hans Boehmc7b28de2018-03-09 17:05:28 -08001/*
2 * Copyright (C) 2018 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
17import java.lang.ref.Reference;
18import java.lang.ref.WeakReference;
19
20public class Main {
21 public static void main(String[] args) {
22 System.out.println("Starting");
23 WeakReference wrefs[] = new WeakReference[5];
24 String str0 = generateString("String", 0);
25 String str1 = generateString("String", 1);
26 String str2 = generateString("String", 2);
27 String str3 = generateString("String", 3);
28 String str4 = generateString("String", 4);
29 wrefs[0] = new WeakReference(str0);
30 wrefs[1] = new WeakReference(str1);
31 wrefs[2] = new WeakReference(str2);
32 wrefs[3] = new WeakReference(str3);
33 wrefs[4] = new WeakReference(str4);
34 // Clear a couple as a sanity check.
35 str1 = null;
36 str2 = null;
37 // str<n> dead here; in the future we will possibly reuse the registers.
38 // Give the compiler something to fill the registers with.
39 String str5 = generateString("String", 5);
40 String str6 = generateString("String", 6);
41 String str7 = generateString("String", 7);
42 String str8 = generateString("String", 8);
43 String str9 = generateString("String", 9);
44 Runtime.getRuntime().gc();
45 for (int i = 0; i < 5; ++i) {
46 if (wrefs[i].get() != null) {
47 System.out.println("Reference " + i + " was live.");
48 }
49 }
50 Reference.reachabilityFence(str0);
51 Reference.reachabilityFence(str1);
52 Reference.reachabilityFence(str2);
53 Reference.reachabilityFence(str3);
54 Reference.reachabilityFence(str4);
55 System.out.println("Finished");
56 }
57
58 private static String generateString(String base, int num) {
59 return base + num;
60 }
61}