Java : Garbage Collection

Started by
1 comment, last by rip-off 14 years, 4 months ago
Why does java not have memory on stack? They try to be as efficient as allocation in stack by their allocation of heap for objects, but why not just have allocation on stack?
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github
Advertisement
Cause they wanted Java to be simpler than C and C++. Garbage collection allows faster development, as there's less complication for the programmer. It's a business decision they took when developing the language. I guess they weighed the cost and benefits, and decided to go with it. Java and the JRE works pretty well, and has gotten quicker over the years.
Early implementations of Java didn't have fast heap allocation. They didn't design the language to have efficient allocation, they designed it to be safe. Stack allocation brings issues relating to invalid references. C#, with the benefit of hindsight, allows for value and reference types to get around this.

This article says that modern JVMs perform escape analysis and can stack allocate (or even just use registers) in the case where an object does not live longer than a single function.

This topic is closed to new replies.

Advertisement