You can manually delete local references at any point within a method. Save local references only in object references that you define in the LOCAL-STORAGE SECTION of a method.
Use a SET statement to convert a local reference to a global reference if you want to save a reference in any of these data items:
Otherwise, an error occurs. These storage areas persist when a method returns; therefore a local reference is no longer valid.
In most cases you can rely on the automatic freeing of local references that occurs when a method returns. However, in some cases you should explicitly free a local reference within a method by using the JNI service DeleteLocalRef. Here are two situations where explicit freeing is appropriate:
For example, in a COBOL method you loop through a large array of objects, retrieve the elements as local references, and operate on one element at each iteration. You can free the local reference to the array element after each iteration.
Use the following callable services to manage local references and global references.
| Service | Input arguments | Return value | Purpose |
|---|---|---|---|
| NewGlobalRef |
|
The global reference, or NULL if the system is out of memory | To create a new global reference to the object that the input object reference refers to |
| DeleteGlobalRef |
|
None | To delete a global reference to the object that the input object reference refers to |
| DeleteLocalRef |
|
None | To delete a local reference to the object that the input object reference refers to |
related tasks
Accessing JNI services