Android Xamarin实现了ByteBuffer。出错了吗?

Android Xamarin实现了ByteBuffer。出错了吗?,android,xamarin,bytebuffer,Android,Xamarin,Bytebuffer,下面是一个示例函数,它填充一个小字节缓冲区,然后将其读入一个大字节数组: // Allocate the two buffers once for all. const int byteBufferSize = 20; var byteBuffer = ByteBuffer.Allocate(byteBufferSize); /* var byteBufferArray = new byte[byteBufferSize]; var byteBuffer = ByteBuffer.Wrap(by

下面是一个示例函数,它填充一个小字节缓冲区,然后将其读入一个大字节数组:

// Allocate the two buffers once for all.
const int byteBufferSize = 20;
var byteBuffer = ByteBuffer.Allocate(byteBufferSize);
/* var byteBufferArray = new byte[byteBufferSize];
var byteBuffer = ByteBuffer.Wrap(byteBufferArray);*/
var bytes = new byte[1024 * 1024];
var bytesPos = 0;

for (;;)
{
    // Put byteBuffer in "write" mode.
    byteBuffer.Clear();

    // Write a few bytes in byteBuffer.
    for (var i = 0; i < byteBufferSize; ++i)
        byteBuffer.Put(0);

    // Put byteBuffer in "read" mode.
    byteBuffer.Flip();

    // Read the written bytes from byteBuffer.
    // DEEP BELOW, THIS CALL UNEXPECTEDLY CAUSES A NASTY ALLOCATION OF ABOUT bytes.length BYTES!
    byteBuffer.Get(bytes, bytesPos, byteBufferSize);

    bytesPos += byteBufferSize;

    Thread.Sleep(1000);
}

是我对ByteBuffer对象做了什么,还是ByteBuffer有什么问题,进入Xamarin?请注意,我确实在本地Android Java应用程序中测试了相同的代码,代码按预期运行,即没有ByteBuffer.Get触发的疯狂分配。

如果有人在研究此问题时首先看到此页面,我现在提交了一份增强请求,讨论了此问题的根本原因以及一些可能的解决方法:

05-15 09:50:28.519: D/dalvikvm(23104): GC_FOR_ALLOC freed 1024K, 48% free 10250K/19624K, paused 49ms, total 49ms
05-15 09:50:29.580: D/dalvikvm(23104): GC_FOR_ALLOC freed 1024K, 48% free 10250K/19624K, paused 48ms, total 48ms
05-15 09:50:30.622: D/dalvikvm(23104): GC_FOR_ALLOC freed 1024K, 48% free 10250K/19624K, paused 39ms, total 39ms
05-15 09:50:31.673: D/dalvikvm(23104): GC_FOR_ALLOC freed 1024K, 48% free 10250K/19624K, paused 45ms, total 45ms
05-15 09:50:32.724: D/dalvikvm(23104): GC_FOR_ALLOC freed 1024K, 48% free 10250K/19624K, paused 42ms, total 42ms
...