• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
 
See where your hand is? Not there. It's next to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic