-
Notifications
You must be signed in to change notification settings - Fork 197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Keep weak references to eager resources in session #229
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...flow-core/tensorflow-core-api/src/main/java/org/tensorflow/internal/WeakPointerScope.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package org.tensorflow.internal; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.Set; | ||
import java.util.WeakHashMap; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
import org.bytedeco.javacpp.Pointer; | ||
|
||
/** | ||
* A minimalist pointer scope only keeping weak references to its elements. | ||
* | ||
* <p>As opposed to {@link org.bytedeco.javacpp.PointerScope}, instances of this class will not | ||
* prevent the garbage collector to free the memory of a pointer that is no longer reachable, even | ||
* if it has been attached to the scope.</p> | ||
* | ||
* <p>When the scope is closed, all pointers that are still valid will be automatically deallocated | ||
* while those already garbage-collected will be ignored.</p> | ||
*/ | ||
public class WeakPointerScope implements AutoCloseable { | ||
|
||
/** | ||
* Attach a pointer to this scope. | ||
* | ||
* <p>Pointers attached to the scope will be automatically freed once the scope is closed, unless | ||
* they have been already released by the garbage collector</p> | ||
* | ||
* @param pointer pointer to attach | ||
*/ | ||
public void attach(Pointer pointer) { | ||
pointers.add(pointer.retainReference()); | ||
} | ||
|
||
/** | ||
* Detach a pointer from this scope. | ||
* | ||
* <p>Detaching a pointer from the scope will prevent its memory to be freed when closing the | ||
* scope.</p> | ||
* | ||
* <p>If this {@code pointer} is not attached to this scope, this method has no effect.</p> | ||
* | ||
* @param pointer pointer to detach | ||
*/ | ||
public void detach(Pointer pointer) { | ||
if (pointers.remove(pointer)) { | ||
pointer.releaseReference(); | ||
} | ||
} | ||
|
||
@Override | ||
public synchronized void close() { | ||
pointers.forEach(Pointer::releaseReference); | ||
pointers.clear(); | ||
} | ||
|
||
private final Set<Pointer> pointers = Collections.newSetFromMap(new WeakHashMap<>()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This close method doesn't prevent the object from being reused. Do we want it to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it is mandatory but it does not make much sense reusing it also. I can reset the
pointers
to null on close if you prefer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking a boolean
closed
which is checked, but setting the pointers to null is probably fine. The latter will crash if closed multiple times though.