2

I have a class defined as follows

final public class Results {
    THashSet<String> filteredHashtags;

Constraints: I know that declaring a variable as static or non-static is a design problem and shouldn't be governed by memory usage but the HashSet filteredHashtags takes up significant memory (>1Gb) so I can afford slightly lower readability at the cost of lower memory usage.

Options

  1. Non-static: As of now I've kept it non-static for the following reason: I create an instance of class, use constructor to assign value to filteredHashtags. Since I'm creating only one instance of the class, it doesn't really mater in terms of memory used by the class. When the object is no longer referred, the memory used by the variable gets freed.

  2. Static: In terms of readability of code, I would prefer keeping it static as it relates better to the physical quantity it represents. However in this case, I need to assign value to the static variable using a function, let's say setValues(...).

Questions:

  1. Is my assumption that in the static case, the memory associated with the variable will never be freed until the program terminates?
  2. If yes, is there a better way to free memory other than setting filteredHashtags = null;
7
  • 1
    As per your question one when you do what you have done in question 2 then GC will free it up. Commented Sep 16, 2014 at 18:28
  • Thanks @Aeshang I do know of that. Was wondering if there's a neater way of doing it. Commented Sep 16, 2014 at 18:31
  • 1
    Just confirming, why do u say that for non static declaration the memory will not be freed until the program is terminated?? DO you mean static ? Commented Sep 16, 2014 at 18:35
  • Apologies. It was a typo. Yes, I meant static Commented Sep 16, 2014 at 18:37
  • Yes you are correct ... check this ... stackoverflow.com/questions/25820853/… Commented Sep 16, 2014 at 18:39

1 Answer 1

2

Rishi, your assumption that 'in the static case, the memory associated with the variable will never be freed until the program terminates' is not correct. Static belongs to the class, and classes are loaded by loaders. Hence, memory used by static variables can be reclaimed.

Sign up to request clarification or add additional context in comments.

6 Comments

"can be reclaimed" - how?
Memory will be reclaimed when 'Unloading' of the class happens. Refer the JLS 3.0 section 12.7 for unloading details.
stackoverflow.com/a/148707/1866159 seems to suggest that the only way that a Class can be unloaded is if the Classloader used is garbage collected, which would happen at the termination of program in my case
Sorry, I can't go through that whole post. But, unloading of classes & GCing of statics is real.
I think that point to be noted in this regard is that a static variable of a class that is being referenced by some other class will not be removed. The JVM marks the resources that cant be accessed and those classes are eligible candidates to be removed from the Java Heap.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.