2
@Service
public class test(){
    public Map<String, String> map= new HashMap<>();
}

in a web application using spring i annotated a class with @Service and defined a global variable map and inserting values in it.

i assumed

map hold inserted values until some one restart the server or remove by using map.remove();

but my senior told me it will hold only for some time after some ideal time and garbage collector will remove it like after 2 or 3 days is that true ?

DimaSan
  • 12,264
  • 11
  • 65
  • 75
rockey 123
  • 125
  • 1
  • 2
  • 6
  • 4
    "is that true" sounds very dubious; you should ask him for an explanation (you'll learn something one way or the other: that it's an actual problem and here's why; or that you shouldn't listen to this person). But, in any case, what you are trying to do looks questionable too. – Andy Turner Oct 31 '16 at 09:02
  • hi just forget about what i am trying or requirement ! when a class annotated with @service and a global variable in it will hold the data until server stops or it will be garbage collected after few days ?? – rockey 123 Oct 31 '16 at 09:09
  • besides I dont know how you understand "global" variable (i would say it has to be static but that is not your case). Unless Spring is not releasing Bean that act as as service then no garbage collection will occure. Maybe spring kills unused service beans and simply create new one if it is neede after some time but this is beyond my klnowlage – Antoniossss Oct 31 '16 at 09:26
  • ok thanks ! but even " Maybe spring kills unused service beans and simply create "--this should not be happen because of use cases . what i know is until an object has reference no garbage collector will touch it .correct me if i am wrong – rockey 123 Oct 31 '16 at 09:31
  • No **strong reference** to be exact and yes, should not happen. – Antoniossss Oct 31 '16 at 09:40
  • By default, Spring will create your service as "singleton" scoped. See http://stackoverflow.com/questions/37605010/how-spring-singleton-scope-is-garbage-collected - these are never garbage collected. However, your application could... in theory... be using "custom" spring scopes, or forcing services to be "prototype" scope, which would change that behaviour - you would have to ask your senior whether that is the case. – David Lavender Oct 31 '16 at 09:40
  • And here is your answer. You better ask your senior fella to explain this without using some black magic :) – Antoniossss Oct 31 '16 at 09:41
  • the problem is he nows nothing and he won't explain anything until unless your a female of any living thing on earth . i will try to figure it out by myself thanks – rockey 123 Oct 31 '16 at 10:04
  • @rockey123 Can you check my answer below ? – Vasu Oct 31 '16 at 10:22
  • [A `@Service` is by default scoped as Singleton.](http://stackoverflow.com/questions/25583355/spring-service-default-scope). A Singleton is created once and then kept alive for as long as the container lives. – JimmyB Oct 31 '16 at 10:29

2 Answers2

2

(1) Does Map hold inserted values until some one restart the server or remove by using map.remove() ?

Yes, you are right, the data will be available until the server is restarted or map.remove() or map.clear() is called.

(2) It will hold only for some time after some ideal time and garbage collector will remove it like after 2 or 3 days is that true ?

No, this is wrong, Garbage Collector will not clear the object unless you call the remove() or clear() on the Map. The data can be pushed/hold in the map, until it is allowed (i.e., the maxheap size) and beyond that you will get OutOfMemory exceptions in the server.

P.S.: But, one point you need to know is, it is NOT a good idea to store the data like this into a Map inside Service, rather you have to consider Caching frameworks (Ehcache, HazelCast, etc..) for caching/storing the data, which provide advanced features like cache expiry, distributing data across the servers, etc..

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • Just a small correction: `Map` is just an interface, so it is entirely possible that an implementation will allow some entries to be GCd under certain conditions. (`java.util.WeakHashMap` is one such example.) It is the specific `Map` implementation `HashMap` that keeps a strong reference to both keys and values, preventing their garbage collection until they are removed or the map itself becomes unreachable. – biziclop Oct 31 '16 at 18:13
0

Map holds list of addresses in memory. Those addresses hold the values which you store using

map.put(object);

every time you'd like to retrieve a specific value which you have stored in memory you pass in the address of location where you stored your value at and as a result you receive your stored value.

Coming back to your question, when you remove the value from the map, what really happens is you lose the address of location where you have your value stored. And there is no other way to access that value anymore. Although the value is still in memory but you cannot use it since you have deleted the address of its location. Garbage Collector will flush this value but you cannot enforce garbage collector to run. The best you can do is call System.gc() which simply is a hint to the garbage collector that you want it to do a collection. And it will run any random time or when JVM requires more memory.

As your senior mentioned that it will be garbage collected in 2-3 days, I suppose he is just speaking hypothetically, because you never know when the garbage collector will run and clear the memory.

BountyHunter
  • 89
  • 2
  • 10
  • well my question is if i put some 10 values in map .he says those 10 values will be garbage collected after 2-3days and my view is they won't until unless we remove it by using remove() or shutdown the server or object – rockey 123 Oct 31 '16 at 10:08
  • Ah, yes! if you have the values stored in some data structure or in some object the values will not be available for garbage collection unless you call delete or remove on it, or if you recompile the code. Even if you have one instance of application running for months and you are not using any kind of database to store values but your own data structures and objects. If you don't close or restart the application your data will stay stored in objects and will not be available for garbage collection. – BountyHunter Oct 31 '16 at 10:16