Difference between revisions of "Thread:Talk:ScalarNext/Version History/Java 8 Stack Allocation"

From Robowiki
Jump to navigation Jump to search
(New thread: Java 8 Stack Allocation)
 
m
 
Line 9: Line 9:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
a could be allocated on stack, while
+
<code>a</code> could be allocated on stack, while
  
 
<syntaxhighlight>
 
<syntaxhighlight>
Line 17: Line 17:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
can not be allocated on stack.
+
<code>b</code> can not be allocated on stack.

Latest revision as of 04:28, 24 February 2018

It seems that when an object is allocated in a function, and never escapes that, Java would replace the allocation to stack if the reference is not modified, which means Java would not reuse previously allocated memory for new objects in this case.

e.g.

void stack() {
  Point2D.Double a = new Point2D.Double(0, 0);
  a.setLocation(1, 1);
}

a could be allocated on stack, while

void heap() {
  Point2D.Double b = new Point2D.Double(0, 0);
  b = new Point2D.Double(1, 1);
}

b can not be allocated on stack.