1 /******************************************************************************
2  * $Id: OGRTestGC.java 355b41831cd2685c85d1aabe5b95665a2c6e99b7 2019-06-19 17:07:04 +0200 Even Rouault $
3  *
4  * Name:     OGRTestGC.java
5  * Project:  OGR Java Interface
6  * Purpose:  A sample app for demonstrating the caveats with JNI and garbage collecting...
7  * Author:   Even Rouault, <even dot rouault at spatialys.com>
8  *
9  ******************************************************************************
10  * Copyright (c) 2009, Even Rouault
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a
13  * copy of this software and associated documentation files (the "Software"),
14  * to deal in the Software without restriction, including without limitation
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16  * and/or sell copies of the Software, and to permit persons to whom the
17  * Software is furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included
20  * in all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28  * DEALINGS IN THE SOFTWARE.
29  *****************************************************************************/
30 
31 import org.gdal.ogr.ogr;
32 import org.gdal.ogr.Feature;
33 import org.gdal.ogr.FeatureDefn;
34 import org.gdal.ogr.Geometry;
35 
36 /* This test should run fine as we use a smarter GC for */
37 /* Geometry and Feature objects */
38 
39 public class OGRTestGC
40 {
main(String[] args)41     public static void main(String[] args)
42     {
43         FeatureDefn featureDefn = new FeatureDefn();
44 
45         Geometry point1 = new Geometry(ogr.wkbPoint);
46         Geometry multipoint1 = new Geometry(ogr.wkbMultiPoint);
47         multipoint1.AddGeometryDirectly(point1);
48         try
49         {
50             /* Just to show that we are smart ! */
51             multipoint1.AddGeometryDirectly(point1);
52             System.err.println("should not reach that point");
53         }
54         catch(RuntimeException re)
55         {
56         }
57 
58         Geometry multipoint2 = new Geometry(ogr.wkbMultiPoint);
59         multipoint2.AddGeometry(multipoint1.GetGeometryRef(0));
60         try
61         {
62             /* Just to show that we are smart ! */
63             multipoint2.AddGeometryDirectly(multipoint1.GetGeometryRef(0));
64             System.err.println("should not reach that point");
65         }
66         catch(RuntimeException re)
67         {
68         }
69 
70         Geometry point3 = new Geometry(ogr.wkbPoint);
71         new Feature(featureDefn).SetGeometryDirectly(point3);
72 
73         multipoint1 = null;
74 
75         for (int i = 0; i < 500000; i++)
76         {
77             if ((i % 100000) == 0) System.out.println(i);
78             Feature feat = new Feature(featureDefn);
79             feat.SetGeometryDirectly(new Geometry(ogr.wkbMultiPoint));
80             feat.SetGeometry(null);
81             feat.GetGeometryRef();
82         }
83 
84         // Add features
85         for (int i = 0; i < 1000000; i++)
86         {
87             if ((i % 100000) == 0) System.out.println(i);
88             Feature feat = new Feature(featureDefn);
89             feat.SetGeometry(new Geometry(ogr.wkbPoint));
90 
91             Geometry point = new Geometry(ogr.wkbPoint);
92             Geometry multipoint = new Geometry(ogr.wkbMultiPoint);
93             multipoint.AddGeometryDirectly(point);
94         }
95 
96         /* Check that the objects are still alive despite their */
97         /* Java containers and associated native objects */
98         /* would have been finalized without a trick */
99         System.out.println(point1.ExportToWkt());
100         System.out.println(point3.ExportToWkt());
101 
102     }
103 }
104