Recycling of out-of-heap memory is seen in Heapbytebuffer and Directbytebuffer and recycling Directbytebuffer
Basic Type Length
There are a number of basic types in Java, such as:
Different types are stored according to their own number of bits, and can be automatically converted for promotion. Byte, char, short can be automatically promoted to int, and if the operand has a long, it is automatically promoted to Long,float and double.
big-endian small end
The current memory, more than a byte to access the smallest unit, when a logical address must be divided into a number of physical units when there is the problem of who put who, so the end (endian) problem arises, for different storage methods, there is a big (Big-endian) and small End ( Little-endian) of two descriptions.
Byte sorting is divided into big and small ends, the concept is as follows
For example, a char is made up of two bytes, and the two byte storage may appear as follows, such as the character A:
Now the mainstream Cpu,intel series is the use of little endian format to store data, and the Motorola series of CPUs using big endian,arm while supporting big and little, network programming, tcp/ IP unification uses the big-endian way of transmitting data, so sometimes we also call the big-endian network byte order. In particular, it is important to note that the sequence of data stored in a program written in the C + + language is related to the CPU on which the build platform resides, while Java-written programs only use the big endian to store data.
For example, my machine is a 32-bit Windows system, and the processor is AMD. For an int type number 0x12345678, for convenience, here is a 16 binary representation. This number is stored in the same order as the CPUs stored in different byte order:
0x12345678 16 Binary, two count is one byte
High efficient bytes--Low Valid bytes: 12 34 56 78
Low Address bit
Big- endian :
Small End : 78
Read and write Data
In direct memory, direct memory is requested via allocatedirect (int byte_length). This memory can be understood as a normal byte-based array, so both insertions and reads are similar to normal arrays.
It simply provides an insertion method based on different data types, such as:
-
Put (byte) inserts a byte
-
Put (byte[]) inserts a byte array
-
Putchar (char) Insert character
-
Putint (int) Insert int
-
Putlong (long) Insert long
Wait a minute.
Basic Property Values
It has several key indicators:
Mark-->position-->limit-->capacity
In addition, there are remaining=limit-position.
Current position--position,position is a pointer to the current array, indicating the current data position. Position can be obtained by position () or by position (int). Note: The location of position is smaller than the limit, larger than mark.
space Capacity--capacity,capacity is the capacity of the current application of direct memory, it is not changed after the application. Capacity can be obtained by means of the capacity () method.
Limit size--limit, we may want to change the size of this direct memory, so we can set it through a property called limit. Limit can be obtained by limit () and set by the limit (int). Note that limit is bigger than mark and position, smaller than capacity.
Mark position--mark, Mark, which is a mark, records the value of the current position. A common scenario is to record where the data is inserted at a time, making it easy to retrace the next time.
-
Can be tagged with the mark () method, Mark method marks the current position, default is-1
-
Use the Reset () method to clear, the Reset method resets the location of the position to Mark's location, Mark cannot be less than 0, or an error occurs
-
Use the Rewind () method to initialize, reset mark to -1.position to 0
Use case
Bytebuffer buffer = Bytebuffer.allocatedirect (1024); Buffer.putchar (A); Buffer.putchar (C); System.out.println ("Insert Data" +buffer); Buffer.mark ();//record Mark's locationBuffer.position (30);//set the position must be bigger than mark, otherwise Mark can't resetSYSTEM.OUT.PRINTLN ("Reset before" +buffer); Buffer.reset ();//Reset Reset , reset after the Position=markSYSTEM.OUT.PRINTLN ("after reset" +buffer); Buffer.rewind ();//clear mark, Position becomes 0,mark-1System.out.println ("After clear tag" +buffer); You can see the following running results: Insert data Java.nio.directbytebuffer[pos=4 lim=1024 cap=1024]reset before Java.nio.directbytebuffer[pos=30 lim=1024 cap=1024]reset after Java.nio.directbytebuffer[pos=4 lim=1024 cap=1024] Java.nio.directbytebuffer[pos After the mark is cleared=0 lim=1024 cap=1024]
The remaining space is--remaing, andremaing indicates the current remaining space
Common Methods
Clear (), This method is used to clear mark and position, as well as the position of limit.
Flip (), This method is mainly used to change the current position to limit, mainly for reading operations.
Compact (), This method is commonly used when reading part of the data. It moves the current position to 0 and then position+1 to 1, which is similar to the substring (current position) effect.
Isdirect () This method is used to determine if it is direct memory. If true, returns False if it is not.
Rewind () This method is used to reset the mark mark.
Use of Java out-of-heap memory