<div dir="ltr">Hi Christian,<div><br></div><div>Dealing with the GC in Java is always a pain. Some time ago I worked doing some JNI libraries with OSG and we had problems with the GC don't freeing "small" objects in Java (wrappers for big C++ objects). In the end the more you can do in the native part the better if you need performance and an strict memory control.</div><div><br></div><div>For the android I recommend you take a look to the osgjni sample: <a href="https://github.com/miragetech/osgAndroid/tree/master/org.openscenegraph.osgjni">https://github.com/miragetech/osgAndroid/tree/master/org.openscenegraph.osgjni</a></div><div><br></div><div>The purpose of this sample was making "complex" things that were not supported in the JNI wrapper. As you can see in the Activity:</div><div></div><div class="gmail_quote"><div dir="ltr"><br> Node node = ReadFile.readNodeFile(path);<br> MatrixTransform m = new MatrixTransform();<br> m.addChild(node);<br> mView.setSceneData(OSGConfiguration.configureScene(m));<br><br>The osgAndroid library is used for loading a node but then before setting the scene to the viewer I call to a "OSGConfiguration" class that is another JNI wrapper in the current project:</div><div dir="ltr"><a href="https://github.com/miragetech/osgAndroid/blob/master/org.openscenegraph.osgjni/src/org/openscenegraph/osgjni/OSGConfiguration.java">https://github.com/miragetech/osgAndroid/blob/master/org.openscenegraph.osgjni/src/org/openscenegraph/osgjni/OSGConfiguration.java</a><br></div><div><a href="https://github.com/miragetech/osgAndroid/blob/master/org.openscenegraph.osgjni/jni/JNIOSGConfiguration.cpp">https://github.com/miragetech/osgAndroid/blob/master/org.openscenegraph.osgjni/jni/JNIOSGConfiguration.cpp</a><br></div><div><br></div><div>This creates a simple animation with OSG in the native part, I think a similar approach could be used for you, avoiding all the GC thing. You can also create new wrappers of your own classes following the same schema used in osgAndroid if you think it could be useful :).</div><div><br></div><div>I hope it helps!</div><div><br></div><div>Regards,</div><div>Rafa</div><div dir="ltr"><br>El jue., 30 jul. 2015 a las 20:35, Christian Kehl (<<a href="mailto:christian-kehl@web.de">christian-kehl@web.de</a>>) escribió:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi to everyone,<br>
<br>
I currently extend the osgAndroid project (attempt) to do Texture Projection and Mapping with mobile images. I already extended the codebase so to get access to the geometry, Vector Arrays etc. of a loaded geometry. Now, the geometry loading function looks as follows:<br>
<br>
<br>
Code:<br>
<br>
private void _load_geometry()<br>
{<br>
Node mesh_node = ReadFile.readNodeFile(pointcloud_path);<br>
mesh = new Group(mesh_node.getNativePtr());<br>
Geode mesh_geode = new Geode(mesh.getLastChild().getNativePtr());<br>
_drawable = new Geometry(mesh_geode.getLastDrawable().getNativePtr());<br>
vertex_array = _drawable.getVertexArray();<br>
Log.i("TEXTUREMAPPING", "Vertex array loaded");<br>
geometry = new ArrayList<Point3>();<br>
Log.i("TEXTUREMAPPING", "Geometry allocated");<br>
for(int i = 0; i < vertex_array.size(); i++)<br>
{<br>
geometry.add(new Point3(vertex_array.get(i).x(), vertex_array.get(i).y(), vertex_array.get(i).z()));<br>
}<br>
Log.i("TEXTUREMAPPING", "Geometry set.");<br>
geometry_as_matrix = new MatOfPoint3f();<br>
Log.i("TEXTUREMAPPING", "Number of 3D points: " + Integer.toString(geometry.size()));<br>
}<br>
<br>
<br>
<br>
<br>
"geometry" is here an ArrayList of OpenCV's Point3. As you can see, In the for-loop I touch every element in the Vec3Array. The Vec3Array itself is a wrapped version of the original C++ class, accessed with its object pointer, as seen in this JNI wrapper code:<br>
<br>
<br>
Code:<br>
<br>
JNIEXPORT jlong JNICALL Java_org_openscenegraph_osg_core_Vec3Array_nativeCreateVec3Array(JNIEnv *, jclass)<br>
{<br>
osg::Vec3Array *a = new osg::Vec3Array();<br>
a->ref();<br>
return reinterpret_cast<jlong>(a);<br>
}<br>
<br>
<br>
<br>
<br>
Now, the trouble: with a certain geometry (test:40k vertices), the test message of actually accessed elements ["Number of 3D Points:" geometry.size(), see above] prints ~11k vertices. In the java console logs (LogCat), I see that Java's GC "cleared" its memory two times during the upper function execution.<br>
<br>
My question: How can I avoid it ? How can/should I do the per-Vertex operations ? Has anybody encountered problems with Java's GC formerly, either in former Android wrappers or in old Java platform portations ?<br>
<br>
The internet with several blogs and lectures tells me there is (especially with Android) no way to actually "control" the GC, or protect certain objects from not being freed. The only option mentioned is to code the things in JNI (C++), as the Garbage Collector obviously doesn't operate on the C++ code.<br>
<br>
Opinions and suggestions are welcome.<br>
<br>
Cheers,<br>
Christian<br>
<br>
------------------<br>
Read this topic online here:<br>
<a href="http://forum.openscenegraph.org/viewtopic.php?p=64536#64536" rel="noreferrer" target="_blank">http://forum.openscenegraph.org/viewtopic.php?p=64536#64536</a><br>
<br>
<br>
<br>
<br>
<br>
_______________________________________________<br>
osg-users mailing list<br>
<a href="mailto:osg-users@lists.openscenegraph.org" target="_blank">osg-users@lists.openscenegraph.org</a><br>
<a href="http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org" rel="noreferrer" target="_blank">http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org</a><br>
</blockquote></div></div>