Java – calculate method call stack size to check stackoverflowexception

This morning I answered a question related to stackoverflowexception This person has asked when a stackoverflow exception occurred

See this link simple ways to cause stack overflow in C #, C + + and Java

So my question is what method can dynamically calculate the method call stack size in our program, and then apply a check before calling a method, which checks whether the method call stack has space to accommodate it to prevent stackoverflowexception

Because I'm a Java person, I'm looking for Java, but I'm also looking for an explanation related to this concept. There is no boundary of any programming language

Solution

For 32-bit JVMs, the total memory available to the JVM is about 2-4GB, and the size of 64 bit JVMs is about 4-16eb The JVM decomposes its memory into:

>Heap memory (allocation controlled by JVM options - XMS and - Xmx)

>Constructed object and array instances > static class and array data (including object / array instances) > thread instances (object instances, runtime data and metadata, including thread object monitor lock references)

>Non heap memory

>Stack memory

>Per thread stack memory (allocation per thread controlled by JVM option - XSS): method call frame, parameters, return value, locally declared primitive & object reference

>Static constants (primitives) > string instance pool java code: load classes and metadata > memory used inside the JVM (JVM code and data structure)

see http://docs.oracle.com/javase/7/docs/api/java/lang/management/MemoryMXBean.html and http://www.yourkit.com/docs/kb/sizes.jsp

>Java Se / Java EE does not contain a standard method to get the actual memory usage of each thread stack. > There is a standard way to get aggregate non heap memory: memorymxbean getNonHeapMemoryUsage(). There is no standard way to get a call stack without memory use: thread getStackTrace() ThreadMxBean. getThreadInfo()& ThreadInfo. getStackTrace()

I suggest you not to ask questions in your proposal because:

>Without some complex JVM specific APIs, you can't use instrumentation / internal information in dynamic thread stack memory usage – where can I find such an API? > Each thread stack usually consumes very little memory relative to the entire JVM, so it is usually easy to allocate enough data to adapt to your algorithm (for example, the default value of windows 64 bit JVM is 128KB stack size, and 2GB memory may have been budgeted for the entire JVM) > this will be very limited power: if your logic actually needs to call a method, But you can't run out of memory, so your program will be broken at that moment Stackoverflow exception is actually the best response. > What you want to do may be an anti - Design anti - pattern The "correct" method is to specify the program requirements and the required runtime environment (including minimum / required memory!), The program is designed accordingly to obtain the best performance and memory use

Antipattern is not to think about these things properly in the process of design and development, but to imagine some magic of runtime reflection to make up for this There may be some (rare) applications with high performance requirements that need to greatly rearrange the algorithms at run time to match the discovered resources exactly – but this is complex, ugly and expensive

Even so, it may be better to drive dynamic algorithm changes at the macro level from the "- XSS" parameter, rather than the micro level of the exact stack memory consumption at the location in the code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>