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.GcMarker;
29import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
30import org.apache.harmony.jpda.tests.share.SyncDebuggee;
31
32public class IsCollectedDebuggee extends SyncDebuggee {
33
34    static IsCollectedObject001_01 checkedObject_01;
35    static volatile boolean checkedObject_01_Finalized = false;
36    static IsCollectedObject001_02 checkedObject_02;
37    static volatile boolean checkedObject_02_Finalized = false;
38    static IsCollectedObject001_03 checkedObject_03;
39    static volatile boolean checkedObject_03_Finalized = false;
40
41    static GcMarker marker;
42
43    @Override
44    public void run() {
45        logWriter.println("--> Debuggee: IsCollectedDebuggee: START");
46
47        checkedObject_01 = new IsCollectedObject001_01();
48        checkedObject_02 = new IsCollectedObject001_02();
49        checkedObject_03 = new IsCollectedObject001_03();
50
51        marker = new GcMarker();
52        marker.add(checkedObject_01);
53        marker.add(checkedObject_02);
54        marker.add(checkedObject_03);
55
56        synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
57        String messageFromTest = synchronizer.receiveMessage();
58        if ( messageFromTest.equals("TO_FINISH")) {
59            logWriter.println("--> Debuggee: IsCollectedDebuggee: FINISH");
60            return;
61        }
62
63        int numberOfExpectedFinalizations = 3;
64        if (messageFromTest.equals(JPDADebuggeeSynchronizer.SGNL_CONTINUE + "2")) {
65          numberOfExpectedFinalizations = 2;
66        } else if (!messageFromTest.equals(JPDADebuggeeSynchronizer.SGNL_CONTINUE)) {
67          logWriter.println("--> Unexpected message: \"" + messageFromTest + "\"");
68          return;
69        }
70
71        checkedObject_01 = null;
72        checkedObject_02 = null;
73        checkedObject_03 = null;
74
75        long[][] longArray;
76        int i = 0;
77        try {
78            longArray = new long[1000000][];
79            int arraysNumberLimit = 7; // max - longArray.length
80            logWriter.println
81            ("--> Debuggee: memory depletion - creating 'long[1000000]' arrays (" + arraysNumberLimit + ")...");
82            for (; i < arraysNumberLimit; i++) {
83                longArray[i] = new long[1000000];
84            }
85        } catch ( OutOfMemoryError outOfMem ) {
86            logWriter.println("--> Debuggee: OutOfMemoryError!!!");
87            // logWriter.println("--> Debuggee: i = " + i);
88        }
89        longArray = null;
90
91        marker.waitForGc(numberOfExpectedFinalizations);
92
93        logWriter.println("--> Debuggee: AFTER System.gc():");
94        logWriter.println("--> Debuggee: checkedObject_01 = " +
95                checkedObject_01);
96        logWriter.println("--> Debuggee: checkedObject_01_UNLOADed = " +
97                checkedObject_01_Finalized);
98        logWriter.println("--> Debuggee: checkedObject_02 = " +
99                checkedObject_02);
100        logWriter.println("--> Debuggee: checkedObject_02_UNLOADed = " +
101                checkedObject_02_Finalized);
102        logWriter.println("--> Debuggee: checkedObject_03 = " +
103                checkedObject_03);
104        logWriter.println("--> Debuggee: checkedObject_03_UNLOADed = " +
105                checkedObject_03_Finalized);
106
107        String messageForTest = null;
108        if ( checkedObject_01_Finalized ) {
109            if ( checkedObject_02_Finalized ) {
110                messageForTest = "checkedObject_01 is UNLOADed; checkedObject_02 is UNLOADed;";
111            } else {
112                messageForTest = "checkedObject_01 is UNLOADed; checkedObject_02 is NOT UNLOADed;";
113            }
114        } else {
115            if ( checkedObject_02_Finalized ) {
116                messageForTest = "checkedObject_01 is NOT UNLOADed; checkedObject_02 is UNLOADed;";
117            } else {
118                messageForTest = "checkedObject_01 is NOT UNLOADed; checkedObject_02 is NOT UNLOADed;";
119            }
120        }
121        logWriter.println("--> Debuggee: Send to test message: \"" + messageForTest + "\"");
122        synchronizer.sendMessage(messageForTest);
123        synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
124
125        logWriter.println("--> Debuggee: IsCollectedDebuggee: FINISH");
126
127    }
128
129    public static void main(String [] args) {
130        runDebuggee(IsCollectedDebuggee.class);
131    }
132
133}
134
135class IsCollectedObject001_01 {
136    @Override
137    protected void finalize() throws Throwable {
138        IsCollectedDebuggee.checkedObject_01_Finalized = true;
139        super.finalize();
140    }
141}
142
143class IsCollectedObject001_02 {
144    @Override
145    protected void finalize() throws Throwable {
146        IsCollectedDebuggee.checkedObject_02_Finalized = true;
147        super.finalize();
148    }
149}
150
151class IsCollectedObject001_03 {
152    @Override
153    protected void finalize() throws Throwable {
154        IsCollectedDebuggee.checkedObject_03_Finalized = true;
155        super.finalize();
156    }
157}
158