Reputation: 298
Use case:
Fetch all the clips URL from s3 as list of strings . Then Submit the list clip to summary.
public static void main(String[] args) {
List<String> randomStringList = getListOfRandomeStrings();
}
static List<String> getListOfRandomeStrings() {
List<String> randomStringList = new ArrayList<>();
// Add 2k strings to randomStringList
return randomStringList;
}
static void iterateList(List<String> randomStrings) {
// Iterate and print strings
}
Questions
1) When the Strings from string pool will be garbage collected?
2) Is there any way to clean the strings?
3) Does using Weak-reference helps?
Refereed this link, but not still not clear When will a string be garbage collected in java
Upvotes: 1
Views: 181
Reputation: 2051
One way to do this is to store the strings in a ByteBuffer using direct memory and then use sun.misc.Cleaner to clear it out as soon as you are done.
From java 9 on, the Cleaner has moved to into java.lang.ref (detailed here).
There are use cases where you need to handle a large number of Strings for a short while and not stress the gc.
I've used the Cleaner when it was in sun.misc, instead of posting my own code I'll provide this rather detailed example.
Upvotes: 0
Reputation: 7127
1) There is no way to predict Garbage Collection in JVM.
2) There is no way to force Garbage Collection in JVM.
3) Storing them as Weak-references will help to clear them (possibly) earlier:
A weakly referenced object is cleared by the Garbage Collector when it’s weakly reachable.
Weak reachability means that an object has neither strong nor soft references pointing to it. The object can be reached only by traversing a weak reference.
First, the Garbage Collector clears a weak reference, so the referent is no longer accessible. Then the reference is placed in a reference queue where we can obtain it from.
You may call System.gc()
to suggest JVM to Collect Garbage, but there are no strong guarantees.
If you're running out of memory, theoretically, weak and soft references are first candidates for removal.
2000
of avegare url-strings will kepp approximately several Megabytes of memory (What is a "meh" even for a regular machine).
So I would rather suggest to do in in a natural way - put them out of scope when you don't need them and don't bother until you really need to care about memory optimization.
Upvotes: 4
Reputation: 140613
Hmm, why do you want things to be collected immediately?!
The JVM is responsible for managing its memory. It will do garbage collection when it considers that to be necessary.
Of course: the sooner your references go "out of life", the quicker objects become eligible for garbage collection, and of course, weak references can help with that.
But the real answer: don't get into premature optimisation. If you think there is a problem, then do proper benchmarking. You are right now making assumptions about a hypothetical problem, and a hypothetical solution. That is rarely a good starting point!
And please note: Strings are created in string pool. That is only true for literal strings from source code. Your strings are all created via new String()
in the end. See here for further reading.
Upvotes: 3