escape analysis friendly very small objects

Andrew Haley aph at
Wed Sep 6 09:59:39 UTC 2017

On 05/09/17 13:35, Andy Nuss wrote:

> I have a variety of classes which just contain a couple scalar
> primitives and possibly a reference to an object that is clearly on
> the heap, all of these small number of data members are private and
> all of them mutable.  Much like the Point class in Brian Goetz's
> article on escape analysis on whether the vm uses the stack or heap
> for class instances.
> There are two use cases for these objects: (1) create with new such
> a small object as a local variable and use it thriughout the
> function, mutating it, etc.  (2) create a class with lots of these
> small objects as private or possibly protected members.

> The hope in the first case is under what conditions will hotspot put
> the object's 2 or 3 fields onto the stack and ideally without the
> hidden headers needed to make it a heap object.  hashCode, equals,
> and clone are not implemented or used if that is important.
> I.e. will hotspot ever make it a simple C++ like object on the
> stack.  Does it help if I do defensive copy if I return one of these
> small objects from the function?

Possibly.  It'd help to have an example that we could talk aout.  Then
we could try it and look at the generated code, and look at why it
does (or does not) eliminate constructors.  HotSpot is remarkably good
at this, but occasionally fails because of unhandled corner cases or
because an object seems not to escape but really does.

> In the second case, my hope is that for a class that contains 5 such
> small objects, and would definitely be on the heap, hotspot would be
> smart enough not to create 5 small objects on the heap and then 5
> references to them in the containing class.  Instead, it would
> explode into the containing class ala C++ just the 2 or 3 primitive
> datamembers of the object, and again, ideally without headers.

HotSpot doesn't do that.

Andrew Haley
Java Platform Lead Engineer
Red Hat UK Ltd. <>
EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035 332F A671

More information about the hotspot-compiler-dev mailing list