• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Is it object allocation always done on Heap or Java has some other optimization techniques too?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I have understood and theoretically objects are always assigned memory on Young Generation space of Heap and then if the objects stay longer, it is promoted to the Old Space. But is this really true that object is always assigned on the heap memory in Java? Are there any other locations where objects are allocated space other than Heap?
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aniruddha Tekade wrote:Are there any other locations where objects are allocated space other than Heap?



I don't know why this isn't discussed much, but Java does support escape analysis on methods. And as an optimization, I believe that it will actually place objects on the stack, instead of the heap. I wish I can find a detailed whitepaper on this, but as mentioned, it is not discussed much...

Henry
 
Enthuware Software Support
Posts: 4906
60
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per section 2.5.3. of JVM specification:


The Java Virtual Machine has a heap that is shared among all Java Virtual Machine threads. The heap is the run-time data area from which memory for all class instances and arrays is allocated.


That said, I do remember reading somewhere that in certains situations an object may be allocated in stack space as a matter of optimization but semantically that will not make any difference.
 
Marshal
Posts: 80618
469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch

Does that question ever come up on the exam?
 
Saloon Keeper
Posts: 28654
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am virtually 100% certain that Java does not allocate objects on the stack. At the Java source level, I can't think of any possible way to indicate it. The syntax that would do so in C is interpreted in Java as defining a reference, not an actual object. You can ONLY construct Java objects via operator "new" or its underlying method newInstance().

Plus providing such an option even as an undercover VM optimization technique would be a non-trivial affair. The JVM would have to understand the difference between references to on-the-stack object instances and general object references, and since one of the major points of Java over C and C++ is to eliminate the latter's infamous ability to for developers to create pointer bugs, I just can't see it. Plus, an optimizing compiler can only track the handles on an object so far without risking objects leaking out into areas where you'd have an object reference to a stack object that has been popped off.

I prefer not to consider where my objects are allocated. At the Java source level, it's immaterial whether they come from a traditional "heap", are maintained in pools, or beam down from Antares. I've seen quite enough grief just from PermGen space.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic