Netty reference count document see http://netty.io/wiki/reference-counted-objects.html
Why reference counting is referenced, and isn't there a GC thread in Java that helps us reclaim objects? I personally understand the following
1:netty in order to achieve zero copy using direct buffer, the Buffer from the native memory allocation, allocation and recovery efficiency is much lower than on the Java heap objects, so the general full GC to control, Direct memory detects the condition itself and calls System.GC (), which can be used to control the release of the buffer by using a reference count. See the implementation of Platformdependent.freedirectbuffer (buffer) in detail.
This piece of content please see: http://blog.csdn.net/xieyuooo/article/details/7547435
The 2:netty uses the pooled function. A bit like the object pool of C + +, first allocate a chunk of memory, and then cut out a piece of it from the inside for you to use, the reference count of 0 into the pool for later use.
If you do not use these two objects by reference counting rules, you will cause a memory leak. So the test needs to be tested when there is a memory leak.
The implementation of the Netty reference count is in the ABSTRACTREFERENCECOUNTEDBYTEBUF code.
Private Static FinalAtomicintegerfieldupdater<abstractreferencecountedbytebuf>Refcntupdater; Static{Atomicintegerfieldupdater<AbstractReferenceCountedByteBuf> Updater =Platformdependent.newatomicintegerfieldupdater (abstractreferencecountedbytebuf.class, "refcnt"); if(Updater = =NULL) {Updater= Atomicintegerfieldupdater.newupdater (abstractreferencecountedbytebuf.class, "refcnt"); } refcntupdater=Updater; } Private volatile intrefcnt = 1;
Volatile is a thread-visible variable that is stored in the JVM's main memory, not in the thread's working memory. The reference count is then atomically updated using the JDK's atomic library.
Atomicintegerfieldupdater,atomiclongfieldupdater,atomicreferencefieldupdater updates the field of an object by reflecting atoms, the field being updated is required
1: Must be a volatile type,
2: Note The access descriptor (public, protected, defualt,private).
3: Only instance variables cannot be class variables
4: For Atomicintegerfieldupdater and Atomiclongfieldupdater only fields of type Int/long can be modified, their wrapper type (Integer/long) cannot be modified. If you want to modify the packing type, you need to use Atomicreferencefieldupdater
Public classUpdater { Public Static voidMain (string[] args)throwsinterruptedexception {Container C=NewContainer (); for(inti=0;i<1000;i++) {Task T=NewTask (c); T.start (); T.join (); //The join method is called after start, and the calling thread waits for the called thread to execute after completion} System.out.println ("============="+C.index); }}classcontainer{ Public volatile intIndex=0;}classTaskextendsThread {PrivateAtomicintegerfieldupdater<container> Updater =Atomicintegerfieldupdater.newupdater (Container.class, "index"); PrivateContainer C; PublicTask (Container c) { This. C =C; } @Override Public voidrun () {System.out.println (Updater.getandincrement (c)); System.out.println (Updater.getanddecrement (c)); }}
Reference count for Netty