• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

Does the final modifier influence the gc?

This is just for understanding.  We are using IntelliJ IDEA in our project (I'm much more an Eclipse person though, hence the question.)  I activated the 'Inspect Code' action and noticed that it often suggests that some variable can have a final modifier.  Ok that is nice but I don't always understand the reason why it chooses some variables.  

As I understand it a variable declared within a local method exists only within that method and is then later garbage collected once the method is exited, and variables passed are referenced.  The final modifier makes the variable non-changable.  That is how I understand it.

I asked some other 'more experienced' developers in my team about why and what impact this has.  (Kinda seemed nit-picky to me).  They stated that it improves the garbage collection.  That is what I don't understand, how does it improve the gc?  I can understand a tool looking at the code, seeing that a variable is not modified and then suggest setting it to final, but performing this action to improve gc is beyond my knoweldge.  They were not able to explain how or why this occurs, but I am under 'orders' to learn from their vast experience.  (Personnally, I believe that they are a bit wacked at times, for whenever something comes up that they can not explain it then becomes a pattern or concept that arose first within PowerBuilder and is therefore a further explination why Java copies from PB and that PB should be resurrected, but that is a laugh for another time.)

Anyone here able to explain how 'final' improves gc?
0
tomboshell
Asked:
tomboshell
  • 5
  • 3
  • 3
  • +2
3 Solutions
 
krakatoaCommented:
Yes - simply gc has less work to do. ;)
0
 
girionisCommented:
I never heard of such thing as that final variables help with gc. I woudl say quite the opposite. SInce a final variable cannot be changed (and therefore cannot be set to null) I would say that it will never get gc'ed. I guess IntelliJ does it for performance reasons as the java compiler can optimize final variables.

Maybe your colleagues got confused with the finalize() methdo instead of the final keyword?
0
 
zzynxSoftware engineerCommented:
I like kraktoa's vision ;°)

1) final = unchangeable
2) unchangeable = non-garbage collectable
3) non-garbage collectable = skippable for the GC
4) skippable for the GC => less work to do ;°)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
girionisCommented:
> I like kraktoa's vision ;°)

Yes true, I never thought of that :)
0
 
girionisCommented:
Some more info about the use of the "final" keyword, you can take some ideas on how optimization is done.
0
 
girionisCommented:
0
 
OrtokoboldCommented:
I think that this IntelliJ IDEA compiler gives you such advice, when local variable is set to a fixed value and it's not changed later. It just simply states that such variable may be treated as the constant, therefore you should add "final" keyword to its declaration. It won't affect GC in any way (I guess), but your design may look a little better.
0
 
krakatoaCommented:
>> I like kraktoa's vision ;°)

LOL, thanks. ;)
0
 
tomboshellAuthor Commented:
nice steps...but then wouldn't a #5 occur?
5)  Ever increasing memory usage.

But I can see how less garbage collection in a server environment may be nice.  And I guess that they are trying other performance improvements since all these classes are invoked through reflection, which does slow things down a bit, even worse when I think that it may be brought about by a minimum of three layers of reflection (a debugging nightmare for me!)
0
 
zzynxSoftware engineerCommented:
>> nice steps...but then wouldn't a #5 occur?
>> 5)  Ever increasing memory usage.
Not if the object containing that final can be gc'ed at last
0
 
krakatoaCommented:
>> Not if the object containing that final can be gc'ed at last

Exactly.
0
 
zzynxSoftware engineerCommented:
Thanks for accepting
0
 
girionisCommented:
:)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now