my goal is to reduce memory usage. should I store a hashMap value in a variable if I need to use that value multiple times?
public void upcCheck() {
you should be store a hashMap value in a variable if I need to use that value multiple times. create method bound variable is create a reference to the HashMap value, then you use reference to de equeals, when you do end of the method, JVM will be callback all memory used by this method.
The short answer is no you don't need to worry about taking up extra memory by using a variable.
When you call the method upcCheck you assign String prodUPC to the value of pHashMap.get("productUpc"). prodUPC will point to the string object. The hash map also points the string object. You are not copying the string object. You are creating a new reference to it. There is tiny tiny amount taken up to store a reference to the object but this very small like 4 bytes.
The first version is faster and more compact if only because you're not redundantly accessing the HashMap
more than once.
If you really are squeezed on memory you might look at dealiasing. It's not clear from your code snippet that it will help but the idea is if you have a large number of objects that can't or won't be modified you might be able to discard the duplicates.
Before I get flamed by people ranting how this is unnecessary and a bad idea, I am taking your memory constraint issue at its word. This is not something anyone should normally concern themselves with.
There may be more than one String object containing the value "0123456" floating around your program and if you pool them with String.intern()
String productUPC=streamThing.readThing();
productUPC=productUPC.intern();
The effect of the second line is to replace the String read in with a 'canonical' String holding that value (from an internal pool). The effect will be to reduce (possibly massively) the number of String objects in play holding the value "0123456". If you read in 2000 products there may be 1000 Strings holding the same value. NB: Based on experience that most data is uneven and the most common value usually represents 50% of the population. The effect of that would be free up 999 String objects and (something like) give you 10K of memory back.
I'll say again, that this isn't something you would normally do but it's more likely to save you space than worrying about the odd space taken up by a local variable.
Here's a post about interning Strings. Read that and the article linked to see if this is for you.
Is it good practice to use java.lang.String.intern()?
It's not redundant, and doesn't take a lot of extra memory. Extra variables don't take much memory; as this post mentions, there is no standard amount (it depends on the vm), but the amount is small enough to not worry about.
In a situation like this, if you were using a List
, it would matter, seeing how accessing that list can increase in time depending on the size of the array. This measurement of time is called the time complexity. Although, since you're using a Map
, it's not something to worry about.
I would prefer using the extra variable, since it's personally easier to read. It's always best to prefer readability. Removing the variable will not cause any noticable performance boosts, seeing how it doesn't take much memory at all, and shouldn't be something to worry about.