Is it worth clearing out local Collections/Maps after creating and populating them in a method?

218 views
Skip to first unread message

Ashwin Jayaprakash

unread,
Feb 20, 2015, 11:45:57 AM2/20/15
to mechanica...@googlegroups.com
Hi, I wanted to know if there is any benefit to clearing out a Set/Map/List that is created, populated and then discarded - all within the context of a method.

Let's say I have a method foo() which:
  1. Creates a HashMap, and then pulls some data from the network or disk (let's say fixed size byte arrays)
  2. It uses this HashMap to buffer/classify the data stream temporarily under certain "computed" keys
  3. After a certain threshold, the "re-classified" contents of the map are transformed and written to another file or endpoint like a message queue
  4. The method then returns

Assuming that the method does not borrow that HashMap from an object pool but creates a new instance, at the end of the method, does it help the Garbage Collector if the method cleared up that map before returning? 

Since the JVM does not do reference counting anymore, but relies on GC roots, is there any point in iterating through such transient maps or collections to nullify references? Would it be much simpler and such as fast if the method just returned without doing anything more? Or by clearing out such data structures, would I be helping the collector which it would've done otherwise in a minor GC by traversing through such reference graphs?

Thanks.

Martin Thompson

unread,
Feb 20, 2015, 11:49:51 AM2/20/15
to mechanica...@googlegroups.com
Consider that when the method returns then the stack frame does not exist so it is no longer reachable as a GC root. 

Don't waste time clearing out the map would be my recommendation. Write simple straight forward code. :-)

--
You received this message because you are subscribed to the Google Groups "mechanical-sympathy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vitaly Davidovich

unread,
Feb 20, 2015, 11:51:29 AM2/20/15
to mechanica...@googlegroups.com
+ 1 Explicitly clearing it will actually hurt you (i.e. you're going to waste cycles on useless work).

Jan van Oort

unread,
Feb 20, 2015, 11:59:57 AM2/20/15
to mechanical-sympathy
+1 Your code's readability will suffer from gettering cluttered, by clearing out something that's *already* on a discarded stack frame. Moreover, clearing out will hurt the latency on that method. Why bother ? 



Fortuna audaces adiuvat - hos solos ? 

Ashwin Jayaprakash

unread,
Feb 20, 2015, 12:34:18 PM2/20/15
to mechanica...@googlegroups.com
Yeah that's what I thought, thanks. Just wanted to make sure that I wasn't imagining it :)

Aleksey Shipilev

unread,
Feb 20, 2015, 5:53:21 PM2/20/15
to mechanica...@googlegroups.com
+1

Moreover, at least HotSpot can routinely collect local variable
references even without waiting till the end of the method (which
probably means the relevant local variable slots in rootsets are also
zeroed). Crafted an example here:
http://cr.openjdk.java.net/~shade/scratch/LocalCollect.java

-Aleksey

On 02/20/2015 07:49 PM, Martin Thompson wrote:
> Consider that when the method returns then the stack frame does not
> exist so it is no longer reachable as a GC root.
>
> Don't waste time clearing out the map would be my recommendation. Write
> simple straight forward code. :-)
>
> On 20 February 2015 at 16:45, Ashwin Jayaprakash
> <ashwin.ja...@gmail.com <mailto:ashwin.ja...@gmail.com>> wrote:
>
> Hi, I wanted to know if there is any benefit to clearing out a
> Set/Map/List that is created, populated and then discarded - all
> within the context of a method.
>
> Let's say I have a method foo() which:
>
> 1. Creates a HashMap, and then pulls some data from the network or
> disk (let's say fixed size byte arrays)
> 2. It uses this HashMap to buffer/classify the data stream
> temporarily under certain "computed" keys
> 3. After a certain threshold, the "re-classified" contents of the
> map are transformed and written to another file or endpoint like
> a message queue
> 4. The method then returns
>
> Assuming that the method does not borrow that HashMap from an object
> pool but creates a new instance, at the end of the method, does it
> help the Garbage Collector if the method cleared up that map before
> returning?
>
> Since the JVM does not do reference counting anymore, but relies on
> GC roots, is there any point in iterating through such transient
> maps or collections to nullify references? Would it be much simpler
> and such as fast if the method just returned without doing anything
> more? Or by clearing out such data structures, would I be helping
> the collector which it would've done otherwise in a minor GC by
> traversing through such reference graphs?
>
> Thanks.
>
> --
> You received this message because you are subscribed to the Google
> Groups "mechanical-sympathy" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to mechanical-symp...@googlegroups.com
> <mailto:mechanical-symp...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "mechanical-sympathy" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mechanical-symp...@googlegroups.com
> <mailto:mechanical-symp...@googlegroups.com>.
signature.asc

Vitaly Davidovich

unread,
Feb 20, 2015, 5:57:32 PM2/20/15
to mechanica...@googlegroups.com

Some depth on this: .NET specific but hotspot same way: http://blogs.msdn.com/b/oldnewthing/archive/2010/08/10/10048149.aspx

sent from my phone

To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.

Ashwin Jayaprakash

unread,
Feb 20, 2015, 8:37:52 PM2/20/15
to mechanica...@googlegroups.com
Thanks all. Here's an old newsletter article that may have helped spread this confusion - http://www.javaspecialists.eu/archive/Issue174.html.
>     For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "mechanical-sympathy" group.
> To unsubscribe from this group and stop receiving emails from it, send

Vitaly Davidovich

unread,
Feb 20, 2015, 9:44:21 PM2/20/15
to mechanica...@googlegroups.com

Interesting.  To me this sounds like some bug/anomaly in how the interpreter oopmap is built - anyone care to try this on java 8? :)

sent from my phone

To unsubscribe from this group and stop receiving emails from it, send an email to mechanical-symp...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages