Use of Java out-of-heap memory

Source: Internet
Author: User

I've often been asked about the benefits and purposes of using out-of-heap (off heap) memory in Java. I think other people who face a few choices should also be interested in the answer.

There is nothing special about out-of-heap memory. Line stacks, application code, NIO cache is used for out-of-heap memory. In fact, in C or C + +, you can only use unmanaged memory because they do not have a managed heap (managed heap) by default. Using managed memory or "heap" memory in Java is a feature of this language. Note: Java is not the only language to do so.

New Object () vs Object Pool vs out-of-heap memory new object ()

Prior to Java 5.0, object pooling was once very popular. The overhead of creating objects at that time is very expensive. However, since Java 5.0, object creation and garbage collection have become very inexpensive, and developers have discovered that performance has been improved, simplifying code, discarding object pools, and creating new objects when needed. Before Java 5.0, almost all objects, including the object pool itself, were boosted by object pooling, and after 5.0, only those particularly expensive objects needed to be pooled, such as threads, sockets, and database connections.

Object Pool

It still has some authorizing in low-latency areas, because the cyclic use of mutable objects reduces the pressure on the CPU cache, which in turn allows performance to be improved. The life cycle and structure of these objects must be as simple as possible, but after doing so you will find that the performance and jitter of the system will be greatly improved.

There is also a more appropriate area for using object pooling, such as when you need to load massive amounts of data and contain many redundant objects. Using object pooling can significantly reduce the amount of memory used and the number of objects that require GC, resulting in shorter GC times and higher throughput.

Such object pools are often designed to be lightweight, rather than simply using a synchronous hashmap, so they still have value in existence.

Take the Stringinterner class to make an example. You can pass a reusable variable StringBuilder that contains the text you want as a parameter to it, and it will return a matching string. Passing a string object directly is inefficient because you have already created the object. StringBuilder can be reused.

Note: One interesting feature of this structure is that it does not require additional thread-safe mechanisms, such as volatile or synchronized, and it is sufficient to require only the minimum thread security that is guaranteed by Java. You can correctly access the final field inside the string, at most, the inconsistent reference is read.

12345678910111213141516171819202122232425262728293031 [Java]View PlainCopy
  1. Publicclass Stringinterner {
  2. Privatefinal string[] Interner;
  3. privatefinal int mask;
  4. Publicstringinterner (intcapacity) {
  5. INTN = Maths.nextpower2 (capacity, 128);
  6. Interner = Newstring[n];
  7. Mask = n- 1;
  8. }
  9. Privatestatic boolean isequal (@NullableCharSequence S, @NotNullCharSequence cs) {
  10. if (s = = null) returnfalse;
  11. if (s.length ()! = Cs.length ()) Returnfalse;
  12. For (inti = 0; i < cs.length (); i++)
  13. if (S.charat (i)! = Cs.charat (i))
  14. Returnfalse;
  15. Returntrue;
  16. }
  17. @NotNull
  18. Publicstring Intern (@NotNullCharSequence cs) {
  19. Longhash = 0;
  20. For (inti = 0; i < cs.length (); i++)
  21. hash = 57* hash + cs.charat (i);
  22. Inth = (int) Maths.hash (hash) & mask;
  23. String s = interner[h];
  24. if (isequal (S, CS))
  25. Returns
  26. String s2 = cs.tostring ();
  27. RETURNINTERNER[H] = s2;
  28. }
  29. }


Use of out-of-heap memory

Using out-of-heap memory and object pooling can reduce GC pause times, which is their only common denominator. Variable objects with short life cycles, creation overhead, or a variable object with a long life cycle but redundancy are more appropriate for using object pooling. The life cycle is moderate, or complex objects are more suitable for processing by GC. However, the variable objects of the medium-long life cycle are tricky, and the out-of-heap memory is their dish.

The benefits of out-of-heap memory are:

    1. Can be extended to a larger memory space. For example, more than 1TB or even larger than main memory space.
    2. In theory, the GC pause time can be reduced.
    3. Can be shared among processes, reducing object duplication between JVMs, making it easier to implement a split deployment of a JVM.
    4. Its persistent storage enables fast reboots while also reproducing production data in a test environment.

From the point of view of system design, using out-of-heap memory can provide more possibilities for your design. The most important promotion is not performance, but decisive.

Out-of-heap memory and testing

One of the biggest challenges in high-performance computing is to reproduce the hidden bugs and verify that the problem has been fixed. By storing input events and data in an out-of-heap memory as persisted, you can turn your critical system into a series of complex state machines. (In simple cases there is only one state machine). In this way, the behavior and performance problems of the production environment can be reproduced in the test environment.

Many investment banks have relied on this technology to reliably reproduce the system's response to an event in the day, and to analyze why the event was so handled. More importantly, you can immediately prove that the fault on the line has been resolved, rather than finding a problem, and hopefully it is the source of the online failure. Deterministic behavior is accompanied by deterministic performance.

You can put events back and forth in the test environment at the real time, and the resulting delay distribution must also be in the production environment. Depending on the hardware, some system jitter may be difficult to reproduce, but this is quite close to the real situation in terms of data analysis. In order to avoid the occurrence of a full day of time to respond to the data of the previous days, you can also add a threshold value, for example, if the interval of two events is more than 10ms, you can just wait for 10ms. This allows you to check if your changes are improving on the delay distribution in one hours, depending on the actual time of day.

Does this have the benefit of losing the "one-compile-everywhere-execution"?

To some extent this is the case, but the effect is much smaller than you think. The closer you get to the processor, the more you rely on the behavior of the processor or operating system. Fortunately, the vast majority of systems use Amd/intel CPUs, and even arm processors are increasingly compatible with the two on the ground floor. There are also differences between operating systems, so the technology is better suited for use on Linux systems than Windows. If you are developing on Mac OS X or Windows, and then the production environment is deployed on Linux, there is no problem. We did the same in higher Frequency trading.

What new problems will be introduced using out-of-heap memory

There is no free lunch, and the outside memory is no exception. The biggest problem is that your data structure gets a little awkward. Either a simple data structure is needed to map directly to out-of-heap memory, or a complex data structure is used and serialized and deserialized into memory. It is obvious that using serialization can be a headache and a performance bottleneck. Using serialization is worse than using the heap object's performance.

In the financial world, many high-frequency data are flat, simple structures, all made up of basic types, and are well suited for mapping to out-of-heap memory. However, not all applications are like this, and there may be some deeply nested data structures, such as diagrams, that you also have to cache on the heap.

Another problem is that the JVM restricts your use of the operating system. You no longer have to worry that the JVM will overload the system. With the use of out-of-heap memory, some restrictions no longer exist, and you can use a larger data structure than main memory, but if you do, consider what disk subsystem you are using. For example, you don't want to be paged to an HDD with only a few IOPS (Input/ouput Operations per Second, io per second), preferably IOPS to 80,000 SSD hard drives, and of course, 1000x is better.

What can OPENHFT do?

OPENHFT contains a number of class libraries that shield you from the details of using local memory to store data. These data structures are persistent, and using them does not produce garbage or only a few. Applications that use it can run all day and not once minor GC.

Chronicle queue--the persisted event queue. Supports concurrent writes of multiple JVMs on the same machine, and concurrent reads between multiple machines. Microsecond latency and the ability to continuously maintain the throughput of millions of messages per second.

Chronicle the local or persisted storage of the MAP--KV table. It can be shared between different JVMs on the same machine, and the data is replicated over UDP or TCP and is remotely accessed via TCP. Microsecond delay, a single machine can maintain millions read and write operations per second.

Thread affinity--binds critical threads to a separate CPU core or logical CPU to reduce system jitter. The jitter can be reduced to the original 1 per thousand.

Which API is used?

If you need to record each event----Chronicle Queue

If you only need one of the most recent results of a unique primary key--chronicle Map

If you're more concerned with that 20 microsecond jitter--Thread Affinity

Summarize

Out-of-heap memory is a double-edged sword. The value you have already seen, can be compared with other scenarios for achieving scalability. It is easier and more efficient to use out-of-heap memory than partitioning/sharding in the heap cache or in Message Queuing or even out-of-process databases. Not only that, but some of the techniques used to improve performance are no longer needed. For example, out-of-heap memory can support synchronous writes of the operating system, and it is no longer necessary to execute asynchronously, which is also at risk of data loss. However, the biggest benefit should be the start-up time, the production system restart speed will be greatly shortened, mapping 1 TB of data only 10 milliseconds, and you can also in the test environment in order to reproduce each event in the production environment, to restore the online site. Through it you can build a reliable quality system.

Article turned from: http://it.deepinmind.com/jvm/2014/12/26/heap-vs-heap-memory-usage.html

Use of Java out-of-heap memory

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.