这是一个填充小字节的示例函数,然后将其读取为大字节数组:
// 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);
}
当我在Nexus 5运行Android 5.0上运行此代码时,似乎很好。但是,当我在三星Galaxy S3上运行Android 4.3时,每次bytebuffer。get fele。原木看起来像:
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
...
是我对字节式对象做的事情,还是bytebuffer.get in xamarin中有问题?请注意,我确实在本机Android Java应用程序中测试了相同的代码。
以防万一在研究此问题时首先遇到此页面,我现在提出了一个增强请求,讨论了此问题的根本原因,以及一些可能的解决方法:
https://bugzilla.xamarin.com/show_bug.cgi?id=31260