× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Scott,
I guess that's what I get for using absolutes :-). I was trying to say
that the objects won't be GC'ed until the program ends OR the JNI
"clean-up" methods were invoked from RPG. If it's running via Java on
the JVM objects will invoke garbage collector as the JVM/Hotspot VM
sees fit.

I do understand why Java objects created via RPG aren't freed. I just
did a very poor job of explaining it :-) You did explain it very well
though and I really appreciate that.

Just for the sake of the archives, Strings in Java are immutable. Any
time you manipulate a string a new String object is created. For
example if you invoke String.toUpperCase() it returns a newly created
String. That will leave you a reference for two strings.

--
James R. Perkins



On Mon, Mar 22, 2010 at 10:39, Scott Klement <rpg400-l@xxxxxxxxxxxxxxxx> wrote:
On 3/22/2010 10:55 AM, James Perkins wrote:
Unfortunately that will not happen since you are still invoking from
RPG. I could be wrong, but I have a feeling when you use RPG to invoke
Java NO objects will ever be GC'ed unless explicitly told to or the
JVM ends.

No, that's not my understanding at all.

James... each Java object has a "reference count" associated with it.
Each time a Java object, static method, native method, etc is using the
object, it increases the reference count.  So at any time the garbage
collector can look at an object, and the object will say "5 routines are
using me" or "2 routines are using me".   When that reference count
reaches zero, Java will remove the object from memory.

In Java, you might do something like this:

  String x;

  x = new String("foo foo");
  printit(x);

  x = new String("bar bar");
  printit(x);

When it does "new String" it reserves memory for a String object, and
creates a reference to that object.  It's in an array inside the JVM,
and might be called "string #1" (or 2 or 3 or whatever) inside that
array.  It has a reference count of 1, because the 'x' variable is
referring to that String object.

When the second "new String" is run, the reference count for the frist
string is decreased to 0, since x no longer refers to that first object.
 Instead, a second object is created, and x now refers to that object.

The next time the Java garbage collector is run, the memory for the
first string is freed up, since that object no longer has any references.

The problem with RPG:  Java knows when it's own variables (x in the
above example) no longer refer to an object.  After all, Java code does
EVERYTHING through the JVM, so the JVM knows what the Java code is
doing.  Java understands Java, and that's the way it should be.

But if an RPG routine refers to string #1, How does Java know when it's
done?  RPG doesn't execute on the JVM, it executes natively.  And the
JVM can't be expected to understand how a compiled RPG program works.

Therefore, the RPG routine must *tell* Java when it's done with a
variable.  Java can't know on it's own.  There are routines that let RPG
tell the JVM that it's done with an object, and that causes Java to
decrease the reference count, and then clean up the object normally.

So what you said is absolutely false.  If you are calling the JNI
routines that indicate you're done with the objects (as you SHOULD be)
then they will get freed up from RPG, just as they would from Java.

If you are not telling Java that you're done with the objects, then the
ref count would stick around til the JVM (or job) is ended -- but
hopefully you're not doing that!

That includes objects created via other objects. I don't
know for sure, but I think that RPG is using JNI (Java Native
Interface) to invoke the methods, which is probably why they don't get
GC'ed.

RPG is definitely using JNI to invoke the methods, but that doesn't mean
they can't be GCed.  It just means that Java has to be told when you're
done with objects.

The DeleteLocalRef() API from JNI (refered to as 'freeLocalRef' inside
HSSFR4) as well as the PopLocalFrame() API (referred to as
'end_object_group' within HSSFR4) make it easy to do that.
--
This is the RPG programming on the IBM i / System i (RPG400-L) mailing list
To post a message email: RPG400-L@xxxxxxxxxxxx
To subscribe, unsubscribe, or change list options,
visit: http://lists.midrange.com/mailman/listinfo/rpg400-l
or email: RPG400-L-request@xxxxxxxxxxxx
Before posting, please take a moment to review the archives
at http://archive.midrange.com/rpg400-l.



As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.