• Post Reply Bookmark Topic Watch Topic
  • New Topic

Garbage Collection  RSS feed

 
satyam pat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have getter for the map field where i set all the latest value of the other fields.

I am creating new object every time getSomeMap() is called, i don't want to use containsKey every time for each field to keep things simple.
My worry is this getter is called many times and would create too many hashMap objects.

Since I am assinging new obj reference, old object should be eligible for garbage collection ? or do i need to explicitly make to null to optimize it?

Thanks in advance.


 
satyam pat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I don't think i am make someMap static, that will land me in threading issues and synchronizing it wont be a good idea i guess.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satyam pat wrote:Since I am assinging new obj reference, old object should be eligible for garbage collection ? or do i need to explicitly make to null to optimize it?

An object is eligible for garbage collection as soon as there is nothing referencing the object anymore. Explicitly setting the variable to null will not do anything to aid garbage collection.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you need the map inside the class itself? Otherwise you can remove the field and make it a local field instead.

If you do need it inside the class you should initialize it in the constructor instead, then only return the existing map from this method. This will allow outsiders to modify the map though; if you don't want this you can make it unmodifiable, which means that nobody can change its contents:
This one line creates a new wrapper around the map that blocks modification. Because you still have a reference to the original map you can use that to modify it, but modifications are now limited to the class itself, not outside it.
 
satyam pat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:
An object is eligible for garbage collection as soon as there is nothing referencing the object anymore. Explicitly setting the variable to null will not do anything to aid garbage collection.


Thanks, that cleared my doubt.
 
satyam pat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made it simple, hashmap is created just once the class object is created and getSomeMap() updates the latest values when its invoked.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code still talks about "someMap" and "logMap" - two different maps. Is that a copy-&-paste mistake, do you really mean that you have just one map?
 
satyam pat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
copy paste error, sorry about confusion.

here's latest

private Map<String, String> someMap = new HashMap<String, String>();
public Map getSomeMap() {
someMap.put("value1", Integer.toString(value1));
someMap.put("value2", Integer.toString(value2));
someMap.put("value3", Integer.toString(value3));
return someMap;
}
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!