Sign in to follow this  

Java : Garbage Collection

This topic is 2945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 2945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this