大多数现代语言都内置了垃圾收集(GC).例如Java,.NET语言,Ruby等.事实上,GC以多种方式简化了应用程序开发.
我很想知道用GCed语言编写应用程序的限制/缺点.假设GC实施是最佳的,我只是想知道我们可能会受到GC的限制,无法做出一些优化决策.
你应该怎么处理BitmapSource?
// this wont work because BitmapSource doesnt implement IDisposable
using(BitmapSource bitmap = new BitmapImage(new Uri("myimage.png")))
{
}
Run Code Online (Sandbox Code Playgroud) 之前,当我使用win32时,我使用FreeImage来加载和保存比特深度大于8位的位图.这是我合作的每一张图片,因为我正在进行医学成像,在任何人说什么之前,是的,我和我的客户花了很多钱在高亮度,高对比度的显示器上,具有11或12位动态范围.实际上,如果您感到好奇,ACR对运行乳房X线照相术的要求会使显示器的动态范围至少为10位.
我刚刚切换到x64来获取内存开销,并将我的所有开发都集中到一个平台和编译模式上.我宁愿不回到win32,我的客户就在我身边(并且真的强迫改变).FreeImage不能在64位窗口上编译; 它在编译器无法处理的代码中有一个_asm指令.
我以为我会在Microsoft类中尝试本机.NET支持.简而言之:它们不起作用,并且在错误消息非常有限的情况下失败.我怀疑这是因为Microsoft仍然不支持Format16bppGrayScale类.
也许我的代码中存在问题.这是我写的代码:
Bitmap theBitmap = new Bitmap(inImage.XSize, inImage.YSize, PixelFormat.Format16bppGrayScale);
//have to go with lockbits
Rectangle rect = new Rectangle(0, 0, theBitmap.Width, theBitmap.Height);
System.Drawing.Imaging.BitmapData bmpData =
theBitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
PixelFormat.Format16bppGrayScale);
IntPtr ptr = bmpData.Scan0;
int theByteSize = theBitmap.Width * theBitmap.Height *2;
byte[] theByteBuffer = new byte[theByteSize];
System.Buffer.BlockCopy(inImage.Data, 0, theByteBuffer, 0, theByteSize);
System.Runtime.InteropServices.Marshal.Copy(theByteBuffer, 0, ptr, theByteSize);
theBitmap.UnlockBits(bmpData);
theBitmap.Save(inDirectory + "\\" + inName);
theBitmap.Dispose();
Run Code Online (Sandbox Code Playgroud)
此代码使程序崩溃
An unhandled exception of type
'System.Runtime.InteropServices.ExternalException' occurred in
System.Drawing.dll
Additional information: A …Run Code Online (Sandbox Code Playgroud) 我一直在想如果有一种方法可以加速释放.NET中的内存.我正在用.NET创建一个游戏(只有托管代码),不需要重要的图形,但我仍然想要正确编写它,以免失去性能.
例如,为不再需要的对象分配空值是否有用?我在互联网上的一些样本中看到了这一点.
.net ×2
wpf ×2
.net-3.5 ×1
16-bit ×1
64-bit ×1
bitmap ×1
bitmapimage ×1
c# ×1
image-load ×1
managed-code ×1