Home > Mobile >  Could you tell me how Android release ByteBuffer. AllocateDirect application memory
Could you tell me how Android release ByteBuffer. AllocateDirect application memory

Time:01-19

I'm making a 3 d test application and encounter a problem, this is a 3 d earth model application, use ByteBuffer. AllocateDirect application heap memory, after repeatedly in and out of the application, OOM phenomenon will occur and check on the net a lot about ByteBuffer. AllocateDirect information, didn't find a solution, consult the master how to solve?

Application memory:
ByteBuffer VBB=ByteBuffer. AllocateDirect (are) length * 4);
VBB. Order (ByteOrder. NativeOrder ());//sets the byte order
MVertexBuffer=VBB. AsFloatBuffer ();//converted to type int buffer
MVertexBuffer. Put (are);//to the buffer into the vertex coordinates data
MVertexBuffer. Position (0);//set the buffer starting position

Release:
1, the first one is that trigger a full gc will be recycled, but each time to enter and exit the application calls System. The gc () didn't work, said data outside of the JVM heap memory can't through the System. The gc (), so I didn't test this method successfully,
2, the second statement: import sun. Nio. Ch. DirectBuffer, (DirectBuffer VBB). The cleaner (). The clean (), but can't find the sun. Nio. Ch. DirectBuffer this class, can't import,
3, the third is that reflection mechanism, but the runtime discovery getMethod (" cleaner ") of the cleaner can't find the error
Method cleanerMethod=buffer. GetClass (). GetMethod (" cleaner ");
CleanerMethod. SetAccessible (true);
Object the cleaner=cleanerMethod. Invoke (buffer);
Method cleanMethod=the cleaner. GetClass (). GetMethod (" clean ");
CleanMethod. SetAccessible (true);
CleanMethod. Invoke (cleaner);

AndroidRuntime Exception:
09-01 15:46:46. 212 29144 5690 E AndroidRuntime: FATAL EXCEPTION: GLThread 7970
09-01 15:46:46. 212 29144 5690 E AndroidRuntime: Process: com. Test. Earth, PID: 29144
09-01 15:46:46. 212 29144 5690 E AndroidRuntime: Java. Lang. OutOfMemoryError: Failed to the allocate a 3110412 byte allocatio
N with 1374136 free bytes and 1341 KB until OOM
09-01 15:46:46. 212 29144 5690 E AndroidRuntime: at com.test.earth.com ponent. TestActivity $Ball. & lt; init> (TestActivity. Java: 562)
09-01 15:46:46. 212 29144 5690 E AndroidRuntime: at com.test.earth.com ponent. TestActivity $SceneRenderer.
OnSurfaceCreated (TestActivity. Java: 273)
09-01 15:46:46. 212 29144 5690 E AndroidRuntime: at android.. Opengl GLSurfaceView $GLThread. GuardedRun (GLSurfaceView. Java: 1516)
09-01 15:46:46. 212 29144 5690 E AndroidRuntime: at android. Opengl. The GLSurfaceView $GLThread. Run (GLSurfaceView. Java:
1259).

CodePudding user response:

I search to the http://blog.csdn.net/aitangyong/article/details/39323125 article said ByteBuffer allocateDirect system will automatically release the memory, I according to the article made an infinite loop application memory, print the result is really out of memory does not decrease,
It's embarrassing, I don't 3 d code will be " unless ByteBuffer objects in the heap memory leaks of memory due to error code "? That seems to find the problem root cause, need not entanglements of the above problems, it's a pity that the blogger wonderful articles to an abrupt end, why can not find the because of error code lead to memory leaks, still hope everybody feel free to advise, thank you!

1, first of all, we look at the offer of NIO ByteBuffer
Import the Java. Nio. ByteBuffer;

Public class TestDirectByteBuffer
{
//- verbose: gc - XX: XX: + PrintGCDetails MaxDirectMemorySize=40 m
Public static void main (String [] args) throws the Exception
{
While (true)
{
ByteBuffer buffer=ByteBuffer. AllocateDirect (10 * 1024 * 1024);
}
}
}
We will most of the memory set to 40 m, run this code will find: the program can run forever, won't quote OutOfMemoryError, if use verbose: gc - XX: + PrintGCDetails, frequent garbage collection activities will find program, so we can draw the conclusion: ByteBuffer. AllocateDirect allocated heap memory does not need our hand release, but also in the ByteBuffer didn't provide the API manual release, also that said, using ByteBuffer don't have to worry about the release of heap memory problems, unless the ByteBuffer objects in the heap memory there is a memory leak, because of error code

CodePudding user response:

Finally how to solve
  • Related