1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *
15 *  See the License for the specific language governing permissions and
16 *  limitations under the License.
17 */
18
19/**
20 * @author Anatoly F. Bondarenko
21 */
22
23/**
24 * Created on 04.03.2005
25 */
26package org.apache.harmony.jpda.tests.jdwp.ObjectReference;
27
28import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
29import org.apache.harmony.jpda.tests.share.SyncDebuggee;
30
31public class IsCollectedDebuggee extends SyncDebuggee {
32
33    static IsCollectedObject001_01 checkedObject_01;
34    static volatile boolean checkedObject_01_Finalized = false;
35    static IsCollectedObject001_02 checkedObject_02;
36    static volatile boolean checkedObject_02_Finalized = false;
37    static IsCollectedObject001_03 checkedObject_03;
38    static volatile boolean checkedObject_03_Finalized = false;
39
40    @Override
41    public void run() {
42        logWriter.println("--> Debuggee: IsCollectedDebuggee: START");
43
44        checkedObject_01 = new IsCollectedObject001_01();
45        checkedObject_02 = new IsCollectedObject001_02();
46        checkedObject_03 = new IsCollectedObject001_03();
47
48        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
49        String messageFromTest = synchronizer.receiveMessage();
50        if ( messageFromTest.equals("TO_FINISH")) {
51            logWriter.println("--> Debuggee: IsCollectedDebuggee: FINISH");
52            return;
53        }
54
55        checkedObject_01 = null;
56        checkedObject_02 = null;
57        checkedObject_03 = null;
58        long[][] longArray;
59        int i = 0;
60        try {
61            longArray = new long[1000000][];
62            int arraysNumberLimit = 7; // max - longArray.length
63            logWriter.println
64            ("--> Debuggee: memory depletion - creating 'long[1000000]' arrays (" + arraysNumberLimit + ")...");
65            for (; i < arraysNumberLimit; i++) {
66                longArray[i] = new long[1000000];
67            }
68        } catch ( OutOfMemoryError outOfMem ) {
69            logWriter.println("--> Debuggee: OutOfMemoryError!!!");
70            // logWriter.println("--> Debuggee: i = " + i);
71        }
72        longArray = null;
73        Runtime.getRuntime().gc();
74        System.runFinalization();  // Make sure that the finalizers are finished running.
75        // Make sure the JNI weak globals are cleared, need to do this after runFinalization.
76        Runtime.getRuntime().gc();
77        logWriter.println("--> Debuggee: AFTER System.gc():");
78        logWriter.println("--> Debuggee: checkedObject_01 = " +
79                checkedObject_01);
80        logWriter.println("--> Debuggee: checkedObject_01_UNLOADed = " +
81                checkedObject_01_Finalized);
82        logWriter.println("--> Debuggee: checkedObject_02 = " +
83                checkedObject_02);
84        logWriter.println("--> Debuggee: checkedObject_02_UNLOADed = " +
85                checkedObject_02_Finalized);
86        logWriter.println("--> Debuggee: checkedObject_03 = " +
87                checkedObject_03);
88        logWriter.println("--> Debuggee: checkedObject_03_UNLOADed = " +
89                checkedObject_03_Finalized);
90
91        String messageForTest = null;
92        if ( checkedObject_01_Finalized ) {
93            if ( checkedObject_02_Finalized ) {
94                messageForTest = "checkedObject_01 is UNLOADed; checkedObject_02 is UNLOADed;";
95            } else {
96                messageForTest = "checkedObject_01 is UNLOADed; checkedObject_02 is NOT UNLOADed;";
97            }
98        } else {
99            if ( checkedObject_02_Finalized ) {
100                messageForTest = "checkedObject_01 is NOT UNLOADed; checkedObject_02 is UNLOADed;";
101            } else {
102                messageForTest = "checkedObject_01 is NOT UNLOADed; checkedObject_02 is NOT UNLOADed;";
103            }
104        }
105        logWriter.println("--> Debuggee: Send to test message: \"" + messageForTest + "\"");
106        synchronizer.sendMessage(messageForTest);
107        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
108
109        logWriter.println("--> Debuggee: IsCollectedDebuggee: FINISH");
110
111    }
112
113    public static void main(String [] args) {
114        runDebuggee(IsCollectedDebuggee.class);
115    }
116
117}
118
119class IsCollectedObject001_01 {
120    @Override
121    protected void finalize() throws Throwable {
122        IsCollectedDebuggee.checkedObject_01_Finalized = true;
123        super.finalize();
124    }
125}
126
127class IsCollectedObject001_02 {
128    @Override
129    protected void finalize() throws Throwable {
130        IsCollectedDebuggee.checkedObject_02_Finalized = true;
131        super.finalize();
132    }
133}
134
135class IsCollectedObject001_03 {
136    @Override
137    protected void finalize() throws Throwable {
138        IsCollectedDebuggee.checkedObject_03_Finalized = true;
139        super.finalize();
140    }
141}
142