The speed problem of heap, stack, static and non-static methods in Java

Source: Internet
Author: User

Speed performance analysis of stacks and stacks

Heap and Stack is the JVM memory model of 2 important components, I have long ago also summed up the difference between heap and stack, basically from the storage content, storage space size, storage speed of these aspects to understand, but about the heap and stack storage speed, only know the heap storage speed is slow, the stack storage speed is fast, As for why the heap than the access speed of the stack, and no special in-depth research, from the internet also found a lot of information, but many reasons are not too agreed, here also listed some, and combined with their own understanding to analyze, if not correctly welcome correct.

1, from the perspective of distribution analysis

The size and life cycle of the stack in Java is determined during compilation (you can refer to an analysis in a JVM memory model that was written earlier this weekend, a blog of GC policies and GC collectors in this series of knowledge points), and the heap is dynamically allocated at runtime, which can take time, so from a distribution perspective, Heap is slower than stack speed.

2, from the angle of access analysis

Many articles on the internet say that access to the stack only 1 times, and access to the heap needs 2 times, once the address, the second time to access the object according to the address, this point of view I do not fully agree. We know that the virtual machine stack is stored in a stack of frames, each stack frame is stored in a number of local variables table, operand, dynamic link and return address, when access to the stack, a single access to the data can be accessed, and Java access to the heap objects in the way there are 2 main ways: Through the direct pointer and handle access, The direct pointer is somewhat similar to the first address of the array, which can be quickly found by direct pointers, with only 1 accesses. The benefit of this approach over the handle is faster, but the downside is also very detailed: when the GC is in place, the address changes, and the GC is very frequent. The other way is the handle, the handle is equivalent to a community doorman, when you want to find the neighborhood of a tenant (the tenants are very rich and wayward, living on different floors and rooms every day), you have to go to the doorman, the doorman will tell you this person he is in which building which room, and then you go to this room to find the line. In this way you will need to visit 2 times (1 times, then the doorman to find the tenants). This speed is naturally slow, but the advantage of this approach is that you will always know where the tenant is, no matter how the tenants change (the object will move frequently during the GC, causing the address to change frequently). So my understanding should be: if the heap uses a direct pointer way, from the access point of view, it should be very different, of course, if it is the way of the handle, there is some truth.

3, from the perspective of CPU hit angle analysis

We know that the CPU has a Level 3 cache, the first cache speed, near the speed of the CPU, but the first level of cache is small, two cache speed second, the space is slightly larger, three cache speed is slower, the space is larger, and the CPU reads by line to read, such as 64-bit machine each read is 64 bits, Equivalent to the length of 2 int can be read each time, each time you read a piece of data, the adjacent data may be read in, and the stack occupies a small space, so that the CPU hit rate will be higher, and the elimination rates will be lower, and the heap occupies a large space, relative to each read hit ratio is lower, The elimination rate is also higher, so from this point of view, the stack is faster than the heap to write.

Above is the storage speed difference between heap and stack, then analyze the speed comparison of static method and non-static method.

Second, static methods and non-static methods (under the premise of creating objects) perform performance analysis

In fact, the previous intuition is that the static method should be accessed faster than the non-static method, because the static method when loading the class is stored in the method area, the runtime can be called directly, and not the static method calls need to initialize the object before the call, the problem is: If the object has been initialized, How fast can I call static and non-static methods? Start thinking that the non-static method is fast, because the non-static method is stored in the virtual machine stack, and the stack access speed is relatively fast, but this is not rigorous, then come to an experiment.

Is the result of multiple runs:

First time:

Second time:

Third time:

Fourth time:

As you can see, the results of the loop 10,000 times, the non-static method execution Speed 4 times 3 times is faster than the static method. Take another 100,000 cycles to see the results:

First time:

Second time:

Third time:

4th time:

This is more obvious, so as far as experimental results are concerned, if the object is already created, the access speed of the non-static method is faster than that of the static method. But as for the reason, the above reason is still a little reluctant, still not very clear, welcome the Great God Guidance

The speed problem of heap, stack, static and non-static methods in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.