Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what are weak global references ? How it is different from a global reference?

What are weak global references in JNI ? How it is different from a global reference and a local reference ?

like image 270
saplingPro Avatar asked May 08 '12 13:05

saplingPro


People also ask

Why do you need weak references?

A weak reference permits the garbage collector to collect the object while still allowing the application to access the object. A weak reference is valid only during the indeterminate amount of time until the object is collected when no strong references exist.

What is JNI global references?

A JNI global reference is a reference from "native" code to a Java object managed by the Java garbage collector. Its purpose is to prevent collection of an object that is still in use by native code, but doesn't appear to have any live references in the Java code. A JFrame is a java. awt.

What is WeakRef JS?

A WeakRef object contains a weak reference to an object, which is called its target or referent. A weak reference to an object is a reference that does not prevent the object from being reclaimed by the garbage collector. In contrast, a normal (or strong) reference keeps an object in memory.


1 Answers

I think that the answer to your questions can be found here: http://java.sun.com/docs/books/jni/html/refs.html

As its written:

Local and global references have different lifetimes. Local references are automatically freed, whereas global and weak global references remain valid until they are freed by the programmer.

The difference between local references and global references is: the context

The local reference is just a local variable. The underlying object will be destroyed once you get out of its context (like returning from the native function that defined it).

Like global references, weak global references remain valid across native method calls and across different threads. Unlike global references, weak global references do not keep the underlying object from being garbage collected.

The difference between weak global references and global references is that the object referenced by the weak one can be garbaged collected if needed (in case of lack of memory).

like image 88
Mesop Avatar answered Sep 23 '22 05:09

Mesop