[osg-users] How to avoid Javas Garbage Collection to free large data array elements ?

Christian Kehl christian-kehl at web.de
Thu Jul 30 11:26:49 PDT 2015


Hi to everyone,

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:


Code:

private void _load_geometry()
{
  Node mesh_node = ReadFile.readNodeFile(pointcloud_path);
  mesh = new Group(mesh_node.getNativePtr());
  Geode mesh_geode = new Geode(mesh.getLastChild().getNativePtr());
  _drawable = new Geometry(mesh_geode.getLastDrawable().getNativePtr());
  vertex_array = _drawable.getVertexArray();
  Log.i("TEXTUREMAPPING", "Vertex array loaded");
  geometry = new ArrayList<Point3>();
  Log.i("TEXTUREMAPPING", "Geometry allocated");
  for(int i = 0; i < vertex_array.size(); i++)
  {
    geometry.add(new Point3(vertex_array.get(i).x(), vertex_array.get(i).y(), vertex_array.get(i).z()));
  }
  Log.i("TEXTUREMAPPING", "Geometry set.");
  geometry_as_matrix = new MatOfPoint3f();
  Log.i("TEXTUREMAPPING", "Number of 3D points: " + Integer.toString(geometry.size()));
}




"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:


Code:

JNIEXPORT jlong JNICALL Java_org_openscenegraph_osg_core_Vec3Array_nativeCreateVec3Array(JNIEnv *, jclass)
{
  osg::Vec3Array *a = new osg::Vec3Array();
  a->ref();
  return reinterpret_cast<jlong>(a);
}




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.

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 ?

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.

Opinions and suggestions are welcome.

Cheers,
Christian

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=64536#64536








More information about the osg-users mailing list