Java Virtual machine's heap prison

Source: Internet
Author: User

In the Java virtual machine, I am a high-weight big housekeeper, they are very afraid of me, especially those Java objects, I put them into a "prison" called heap, strict management, life and death in control.

The Chinese have translated the stack into stacks, translated the heap into heaps, and some will translate the stack into stacks, alas, I don't know what they think, but it's all over the years, you know.

It happens that I do garbage collection of Java objects in the heap, and this "heap" always reminds me of the garbage heap.

When it comes to garbage collection, it's a huge burden, for the simple reason that human beings who write Java programs just give the object to new and throw it into the heap, but never forget to delete it, and the responsibility to delete those objects falls on my head, and I don't strictly manage it.

Sometimes I envy C and C + +, have to manually allocate and release memory, out of the wrong is the programmer to back the pot.

In my case, if I let these object objects arbitrary, my low-capacity, Java Virtual machine can not be changed after the start of the heap "prison" will soon be filled, so I have to send my right-hand, specifically to find and clean up the unused Java objects, the space they occupy to release.

In order to find these troublemakers, I invented a called "Accessibility analysis" algorithm, this algorithm is estimated that most people already know, I am no longer verbose, the following figure illustrates the idea behind, smart you can see that the Orange object is unreachable object, can be recycled.

I protested many times, let him revise, he said the public number can only change five words, can't change, alas, really can't.

Heap Prison
Well, now I'll give you a detailed look at the heap "prison" that I manage.

You can think of it as a large space, for ease of management, I divide the heap "prison" into multiple areas, and then move those Java objects around.

I set the rule is: The new guys are going to enter the Cenozoic to stay, the next generation live, I sent the cleanup to garbage collection (Minor GC), after the recovery still live, then the older old men rushed to the nursing home (old age) go.

Each Java object in the heap I will set an age counter, each time the Java object through a GC, the age plus 1, if a certain degree, sorry, please enter the nursing home (old age). In fact, I will do dynamic age judgments, which are not on the table.

You may wonder why the strange areas of Eden, Survivor1, Survivor2 are separated in the Cenozoic.

That's because I want to implement a so-called "copy" algorithm here.

At the earliest, I was dividing an area of memory into two areas of equal size, one at a time.

Area 1 run out, I'll do the garbage collection and move the surviving to another area.

Note: After moving in, they will all live together tightly, so that the removed red fragments will be re-formed into a large space for later use, especially for bulky objects.

The use of two areas upside down, though efficient and free of debris, is a huge waste of space: only half a time.

Later, humans found that most of the objects in the new generation will not live for much longer, basically a garbage collection is almost deleted.

So it improved this only half of the replication algorithm, the new generation into three parts: Eden, Survivor1, Survivor2, their proportion is 8:1:1.

Use only Eden and one of the survivor at a time, and when garbage is collected, copy the objects that are still alive in the two areas to another survivor, and if the survivor doesn't fit, go to the nursing home (old age).

If it's unfortunate that even the nursing home is full, it's a very slow operation and you'd better pray that it doesn't happen too often.

"Prison" is a promising
Although I can abusive domineering attitudes in the heap prison, sometimes I have to touch the world outside the prison.

Once to send the data out through the socket, I had the data ready, in my heap, but the JVM boss actually copied the data into memory outside the heap before it could be sent through the socket.

I asked him what the hell is going on, why should I be superfluous, is it not reassuring to me the great housekeeper of the heap prison?

JVM boss said really is not assured that the bottom of the socket is written in C, the attention is the address of the physical memory, you garbage collection when the Java object in what Eden, Survivor, old generation moved to move, the address of the object will change to change, How can I tell someone exactly which address to send the data ah?

Think of this is the reason, there will be lost, you programmers do not have to manage the memory, but the underlying also has to deal with the memory, and there is an additional process: Copy.

The boss also said: "Perhaps you do not know, in addition to your heap prison, in fact, I in the Java process is also called" Off-heap Memory "place, the data will be copied here. In order to be apart from you, I call it the memory outside the heap. ”

I didn't think there was an enclave that I couldn't even do!

But it has nothing to do with me, let it go.

But a few days later, the JVM boss again brought me a "surprise."

He said: "Copying the data is too much trouble, I think of a way, can be directly allocated in Java code a piece of memory belonging to Off-heap." ”

I feel scalp Fameng: "Directly in the heap out of memory allocation?" How exactly is it distributed? ”

The boss gave me a piece of code: "Look, this does not allocate 128M heap external memory?" The read and write operation to this buffer will be written directly to the out-of-heap memory and not copied by you. ”

Bytebuffer buffer = bytebuffer.allocatedirect (1024x768128);

Damn, interface-oriented programming, the Bytebuffer allocated out of the heap memory, like a normal Java object in use, the slightest can not see it in the heap or outside the heap.

This memory is completely out of my control.

The eldest brother saw that I was in a bad mood, comforting: "This buffer is also a Java object, which is stored in your heap, except that it preserves the information of the 128M memory." ”

That's the same! Since it's a Java object, it has to be put in my heap, controlled by me!

It can be imagined that when this object is garbage collected, the direct memory it points to will be released.

I suddenly have an evil idea: if such objects are more and more, and have not been garbage collected, then the corresponding direct memory will not be released, and then out of memories?

The boss seems to see through my mind: "For these objects, you have to be very careful, be sure to release." ”

The ability to directly allocate out-of-heap memory has been rolled out, and I've found that allocating out-of-heap memory is a bit slower than memory in the heap, and I'm guessing there aren't many people using it. What is not expected is that it is particularly suitable for those scenarios where the number of allocations is small and the read and write operations are frequent. So it was warmly welcomed by the netty of these communication systems.

To reduce the overhead of creating out-of-heap memory, Netty also introduces the technique of object pooling, which, like the database connection pool, allocates some out-of-heap memory and then continually re-uses them.

I did not think that the heap outside memory can play so many tricks, but the thought they are still Java program, still have to use the Java object wrapper, anyway can not jump out of my palm, also relieved.

Java Virtual machine's heap prison

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.