Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Objects on the stack memory in java ?

This is just a simple theoretical question out of curiosity. I have always been like a java fan boy. But one thing makes me wonder why java does not provide mechanism for creating objects on the stack ? Wouldn't it be more efficient if i could just create small Point(int x,int y ) object on the stack instead of the heap like creating a structure on C# . Is there any special security reason behind this restriction in java ? :)

like image 375
adn.911 Avatar asked Sep 18 '14 02:09

adn.911


People also ask

Can I create object in stack in Java?

When the method which created the object exits what do you point to? That being said object are created on the stack in Java, it's just being done behind your back using the escape analysis which makes sure the above scenario doesn't occur.

Can we create object in stack?

Now one thing that in Java you cannot create an object in the stack, always objects are created in the heap memory by only using the 'new' keyword but C++ gives you an option whether you can create an object in the stack or whether you want it in heap.

Can you store objects in a stack Java?

Yes, an object can be stored on the stack. If you create an object inside a function without using the “new” operator then this will create and store the object on the stack, and not on the heap.


3 Answers

The strategy here is that instead of leaking this decision into the language, Java lets the JVM/Hotspot/JIT/runtime decide where and how it wants to allocate memory.

There is research going on to use "escape analysis" to figure out what objects don't actually need to go onto the heap and stack-allocate them instead. I am not sure if this has made it into a mainstrem JVM already. But if it does, it will be controlled by the runtime (thing -XX:something), not the developer.

The upside of this is that even old code can benefit from these future enhancements without itself being updated.

If you like to manually manage this (but still have the compiler check that it stays "safe"), take a look at Rust.

like image 93
Thilo Avatar answered Sep 30 '22 13:09

Thilo


This will tentatively be coming to Java, there is no real ETA set for this so you could only hope it will come by Java 10.

The proposal is called Value Types and you can follow it in the mailing list of Project Valhalla.

I do not know if there were any prior reasons as to why it wasn't in the language in the first place, maybe originally it was thought of as unneeded or there was simply no time to implement this.

like image 38
skiwi Avatar answered Sep 30 '22 11:09

skiwi


A common problem would be to initialize some global reference with an object created on the stack. When the method which created the object exits what do you point to?

That being said object are created on the stack in Java, it's just being done behind your back using the escape analysis which makes sure the above scenario doesn't occur.

like image 20
Mateusz Dymczyk Avatar answered Sep 30 '22 13:09

Mateusz Dymczyk