Home | History | Annotate | Download | only in art
      1 /*
      2  * Copyright (C) 2016 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 package art;
     18 
     19 import java.util.ArrayList;
     20 
     21 public class Test908 {
     22   public static void run() throws Exception {
     23     doTest();
     24   }
     25 
     26   public static void doTest() throws Exception {
     27     // Use a list to ensure objects must be allocated.
     28     ArrayList<Object> l = new ArrayList<>(100);
     29 
     30     setupGcCallback();
     31 
     32     enableGcTracking(true);
     33     run(l);
     34 
     35     enableGcTracking(false);
     36     run(l);
     37   }
     38 
     39   private static void run(ArrayList<Object> l) {
     40     allocate(l, 1);
     41     l.clear();
     42 
     43     Runtime.getRuntime().gc();
     44 
     45     printStats();
     46 
     47     // Note: the reporting will not depend on the heap layout (which could be unstable). Walking
     48     //       the tag table should give us a stable output order.
     49     for (int i = 10; i <= 1000; i *= 10) {
     50       allocate(l, i);
     51     }
     52     l.clear();
     53 
     54     Runtime.getRuntime().gc();
     55 
     56     printStats();
     57 
     58     Runtime.getRuntime().gc();
     59 
     60     printStats();
     61   }
     62 
     63   private static void allocate(ArrayList<Object> l, long tag) {
     64     Object obj = new Object();
     65     l.add(obj);
     66   }
     67 
     68   private static void printStats() {
     69       System.out.println("---");
     70       int s = getGcStarts();
     71       int f = getGcFinishes();
     72       System.out.println((s > 0) + " " + (f > 0));
     73   }
     74 
     75   private static native void setupGcCallback();
     76   private static native void enableGcTracking(boolean enable);
     77   private static native int getGcStarts();
     78   private static native int getGcFinishes();
     79 }
     80