Why is there no way to get the actual stack size?

I understand that there is no way to get the stack size of a stream in Java at runtime (see Is it possible to get the actual stack size used by a stream in Java after a while? ).

For example, if we create java.lang.Thread by specifying a stack size of 64 * 1024, the JVM can provide us with a thread with any stack size.

However, I believe that actual knowledge of the actual stack size is very useful for some applications that require such information.

What is the reason that we do not have a method that tells us the actual number of bytes used for the stack?

Is there some kind of architecture limitation that makes it impossible to get the actual number of bytes for a stream?

+6
source share
1 answer

The only things that are stored on the stack are primitive types and references. Objects are always created on the heap, so the types of data that will be stored on the stack

  • Local variables of type byte, char, int, long, float, double, the longest of which is double, which is 8 bytes.
  • References to objects: 4 bytes on 32 bits vm, 8 bytes on 64 bits vms (possibly shorter, 48-bit links are used to save space).

Note that arrays are types of objects, so they are stored on the heap, also if you have a primitive that is a field in the class, then the primitive is part of the object and therefore will be stored on the heap.

So it's pretty hard to break out of the stack space for a thread if you don't have runway recursion. I would suggest that this is why Java developers do not give you a way to find out the size of the stack.

Another reason for not specifying the stack size is that it will be implementation information with which you cannot do anything, you cannot change the size of the stack of threads after creating the thread, and you can't do pointer arithmetic to determine the stack size?

+3
source

Source: https://habr.com/ru/post/908227/


All Articles