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;
Rishi Dua
  • 2,296
  • 2
  • 24
  • 35
  • 1
    As per your question one when you do what you have done in question 2 then GC will free it up. – StackFlowed Sep 16 '14 at 18:28
  • Thanks @Aeshang I do know of that. Was wondering if there's a neater way of doing it. – Rishi Dua Sep 16 '14 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 ? – SamDJava Sep 16 '14 at 18:35
  • Apologies. It was a typo. Yes, I meant static – Rishi Dua Sep 16 '14 at 18:37
  • Yes you are correct ... check this ... http://stackoverflow.com/questions/25820853/initializing-static-final-variable-in-declaration-vs-initializing-during-applica/25821048#25821048 – StackFlowed Sep 16 '14 at 18:39
  • As far as I understand, this onCreate() approach is only for Android. So my assumption is correct. It might be a good idea to go for non-static implementation then. – Rishi Dua Sep 16 '14 at 18:45
  • Non static is always the best way to move forward while designing application. Unless obviously we need a data that has to be shared among all other classes. – SamDJava Sep 16 '14 at 19:06

1 Answers1

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.

Prakash
  • 461
  • 1
  • 5
  • 21
  • "can be reclaimed" - how? – Rishi Dua Sep 16 '14 at 18:44
  • Memory will be reclaimed when 'Unloading' of the class happens. Refer the JLS 3.0 section 12.7 for unloading details. – Prakash Sep 16 '14 at 18:48
  • http://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 – Rishi Dua Sep 16 '14 at 18:50
  • Sorry, I can't go through that whole post. But, unloading of classes & GCing of statics is real. – Prakash Sep 16 '14 at 18:55
  • 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. – SamDJava Sep 16 '14 at 19:04
  • Thanks @Prakash. Your answer pointed me to the right direction – Rishi Dua Sep 18 '14 at 10:26