• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

does local variables which are object and final are still stored in stack?

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Please have a look at following snippet, there is a BatchExecutionContext which is declared as final and will be sent to a thread pool inside a thread.

My question is whether this object, in this case, is stored in stack or it is its reference that is stored in stack and the object itself is in the heap?

My concern is that because the thread pool is still alive after the method invocation, the JVM will store this object (namely "context") in the stack rather than the heap. if so, in action we will encounter a StackOverflow exception because there are lots of threads. Also the parameter "job" that declared as final is another problem.

 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also the Callable<T> which is an anonymous class is the point of my concerns. if they are stored in the Stack what should I do to prevent that?

thanks
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Java 7 -- and Java 7 only -- objects can be stored on the stack if and only if they are used only within a single method, and no reference to that object is passed to any other method. Since that's not the case here, the BatchExecutionContext will be allocated on the heap, as usual.
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By Java 7, Ernest, did you mean Java 6?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:By Java 7, Ernest, did you mean Java 6?



Escape analysis is an experimental option you can turn on in the most recent Java 6 patchlevels; my understanding was that it's on by default in Java 7, although honestly I am not sure of that. So I did mean Java 7, but it is indeed something you can have in Java 6 if you want. The poster does not want this, so I didn't bring it up!
 
Campbell Ritchie
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So we're both right.

But why would one need to know that in the first place?
 
Mohammad Norouzi
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ernest Friedman-Hill wrote:In Java 7 -- and Java 7 only -- objects can be stored on the stack if and only if they are used only within a single method, and no reference to that object is passed to any other method. Since that's not the case here, the BatchExecutionContext will be allocated on the heap, as usual.



Thanks Ernest
Can you please refer me to a link that confirm your statement. I need this to provide to our management to assure them actually. Please don't take it that I don't trust you

And one more question, as you said in Java 7 no reference to that object is passed to any other method, then how to implement such methods as above?

thanks again
 
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic