Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

how is it possible to allocate objects on stack?  RSS feed

 
Ranch Hand
Posts: 1102
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As said earlier in Java objects are created in the heap. The programming language does not offer the possibility to let the programmer decide if an objects should be generated in the stack. But in certain cases it would be desirable to allocate an object on the stack, as the memory allocation on the stack is cheaper then the memory allocation in the heap, deallocation on the stack is free and the stack is efficiently managed by the runtime.
The JVM uses therefore internally escape analysis to check if an object is used only with a thread or method. If the JVM identify this it may decide to create the object on the stack, increasing performance of the Java program.

Reference: http://www.vogella.com/articles/JavaPerformance/article.html#Escape Analysis

My Question is:
how do JVM uses internally escape analysis to check if an object is used only with a thread or method, and how can we let the jvm to allocate the memory on the stack for objects??
 
Bartender
Posts: 9493
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JVM doesn't perform escape analysis. The Java compiler does. It just checks whether you save a reference to a newly created object in a member variable, pass it to a method which does so, or refer to it from a local/anonymous class. This is just speculation from my part, but that should roughly be what happens. In any other case, the object should only be used for the duration of the method invocation, and can be allocated on the stack.

The compiler is pretty smart in this. It would be unnecessary and dangerous to allow the programmer to make this decision.
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd expect that the size of the object being allocated plays some role in the decision too. A large array is probably not allocated on stack, even if it doesn't escape the method.

Which in turn means the optimization might be slightly less common than one would expect. For example, an ArrayList instance might get allocated on the stack, but its underlying array won't.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Punit Jain wrote:But in certain cases it would be desirable to allocate an object on the stack, as the memory allocation on the stack is cheaper then the memory allocation in the heap, deallocation on the stack is free and the stack is efficiently managed by the runtime.


Possibly, in theory; but I highly doubt it would be visible in practise (that really does sound like micro-optimization to me). I also suspect it goes against the grain of a memory-managed language, since you now have 2 paradigms for garbage-collection.

You want to test 'em all?

Winston
 
Punit Jain
Ranch Hand
Posts: 1102
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Punit Jain wrote:But in certain cases it would be desirable to allocate an object on the stack, as the memory allocation on the stack is cheaper then the memory allocation in the heap, deallocation on the stack is free and the stack is efficiently managed by the runtime.


Possibly, in theory; but I highly doubt it would be visible in practise (that really does sound like micro-optimization to me). I also suspect it goes against the grain of a memory-managed language, since you now have 2 paradigms for garbage-collection.

You want to test 'em all?

Winston



Yes i want to test them, how can i do that?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Punit Jain wrote:Yes i want to test them, how can i do that?


Get a book on garbage-collection, and then add its possibilities to the scenarios for "stack-based deallocation of objects".

Winston
 
Punit Jain
Ranch Hand
Posts: 1102
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Punit Jain wrote:Yes i want to test them, how can i do that?


Get a book on garbage-collection, and then add its possibilities to the scenarios for "stack-based deallocation of objects".

Winston



okay
 
Ranch Hand
Posts: 3170
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The JVM doesn't perform escape analysis. The Java compiler does.



Since JDK 6u14, there's been an experimental JVM flag, -XX:+DoEscapeAnalysis. See the release notes. One can argue that it's being done by a compiler, namely the JIT, which is part of the JVM. But it's definitely not part of javac. I don't know if javac does any escape analysis, though I would think it could. But Sun and Oracle's general approach has been to keep fancy optimizations out of javac, and put them in the JIT instead. I guess this lets them keep the bytecode relatively simple.
 
Mike Simmons
Ranch Hand
Posts: 3170
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say it's a micro-optimization for programmers to do this themselves, yes. But having the JVM do it can be a good thing, usually a minor improvement but sometimes a big win. I imagine there are also cases where it has no benefit or is even detrimental. But you can certainly try out different programs with and without escape analysis, and see what happens.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!