Timeline for Stack and Heap memory in Java
Current License: CC BY-SA 2.5
16 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 16, 2017 at 14:49 | comment | added | Pratik Ambani | Do we have any tool to analyze these memory allocation on heap and stack at time? | |
| Jul 13, 2016 at 21:27 | review | Suggested edits | |||
| Jul 13, 2016 at 23:59 | |||||
| Jun 6, 2016 at 12:27 | comment | added | wulfgarpro | An object argument is a value type in Java; that is, references are passed by value. You can test this by re-assigning a methods object argument and checking if it indeed alters the original object reference. This would mean the object arguments to a method are stored on the stack. | |
| Jan 12, 2015 at 0:20 | comment | added | back2dos | @hagubear: It depends. In some cases the JIT (or even the compiler) is able to avoid allocation (and the overhead it incurs). But unlike in C for example, you are not actually explicitly saying where the value should be allocated. | |
| Jan 11, 2015 at 22:58 | comment | added | ha9u63a7 |
If I say int[] s = new int[]{1,2,3,4} you are saying that it will still be created in stack? I thought as soon as we use new, we are asking the compiler to allocate memory in heap?
|
|
| Nov 23, 2014 at 10:42 | comment | added | back2dos | Some Java runtimes are not JITted, particularly embedded ones, where performance does matter a lot more, in which case the answer you propose can be downright detrimental. The simplest answer to performance is "love thy profiler" and nothing else. Still, this question is not about performance and nowhere did I make any claims on performance. I think the understanding of heap and stack should be acquired, before it can be transcended. This question is about scope and life cycle and for that I think the model is suitable. | |
| Nov 23, 2014 at 4:35 | comment | added | user40980 | Yes... but the simplest answer is 'it doesn't really matter - stop worrying and love the vm.' Trying for simplicity without accuracy may perpetuate the myths that are out there about performance and how things (don't) work. I'd contend the classic simple model is more complex than the modern simple model of "its where it is fastest to access and the compiler is smarter than you." | |
| Nov 23, 2014 at 0:05 | comment | added | back2dos | You are right of course. Half a year before this answer I made the same point about C#. This question however seems to ask for a basic mental model to understand the different roles of the heap and the stack, and to that end I think simplicity beats accuracy ;) | |
| Nov 22, 2014 at 23:45 | comment | added | user40980 | As I found in this article, Java may store objects on the stack (or even in registers for small short lived objects). The JVM can do quite a bit under the covers. Its not exactly correct to say "Now Java only stores primitives on the stack." | |
| Nov 22, 2014 at 23:29 | comment | added | Stephen C | Reference: docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.2 | |
| Nov 22, 2014 at 22:42 | comment | added | Stephen C |
The terminology of this Answer is wrong. According to the Java Language Specification, references are NOT primitives. The gist of what the Answer says is correct though. (While you can make an argument that references are "in a sense" primitive is by the by. The JLS defines the terminology for Java, and it says that the primitive types are boolean, byte, short, char, int, long, float and double.)
|
|
| Apr 2, 2013 at 9:35 | comment | added | Sune Rasmussen |
@Geek: In terms of data, you may view any of the primitive data types - including references - as numbers. Even chars are numbers and can be used interchangeably as so. References are also just numbers referring to a memory address, either 32 or 64 bits long (although they cannot be used as such - unless you're messing around with sun.misc.Unsafe).
|
|
| Jul 31, 2012 at 8:52 | comment | added | back2dos | @Geek: Because the common definition of primitive data types applies: "a data type provided by a programming language as a basic building block". You might also notice that references are listed among the canonical examples further down in the article. | |
| Jul 31, 2012 at 8:33 | comment | added | Geek | "and only references (which in turn are primitives) " Why do say that references are primitives ? Can you clarify please? | |
| Apr 10, 2011 at 20:14 | vote | accept | Vinoth Kumar C M | ||
| Apr 5, 2011 at 12:33 | history | answered | back2dos | CC BY-SA 2.5 |