简体   繁体   English

TBitmap到TPngImage和内存使用情况

[英]TBitmap to TPngImage and Memory usage

I have an array of 11 white TBitmaps (32-bit 512x512 pixles = 1 MB) and I want to assign them to TPngImage array to reduce memory usage, I expect the 1 mb white bitmap to become 1.76 KB png and the memory usage drop dramatically I monitored that with task manager and the difference is just 0.824 MB why is that? 我有一个11个白色TBitmaps(32位512x512像素= 1 MB)的数组,我想将它们分配给TPngImage数组以减少内存使用量,我希望1 mb白色位图变为1.76 KB png并且内存使用率大幅下降我用任务管理器监控它,差异只有0.824 MB,为什么会这样? and what is the best/fast way to lossless compress TBitmaps in memory? 什么是在内存中无损压缩TBitmaps的最佳/快速方法?


for I := 0 to 10 do
begin
  Bitmaps[I] := TBitmap.Create;
  Bitmaps[I].PixelFormat := pf32bit;
  Bitmaps[I].Canvas.Pen.Color := clWhite;
  Bitmaps[I].SetSize(512,512);
  Bitmaps[I].Canvas.Rectangle(0,0,512,512);
end;

for I := 0 to 10 do begin Pngs[I] := TPngImage.Create; Pngs[I].Assign(Bitmaps[I]); Bitmaps[I].Free; end;


Update 更新

Form @bummi research I think the best thing to do is to save the pngs in a memory stream array, that makes the difference 9.7 MB. 形式@bummi研究我认为最好的办法是将png保存在一个内存流数组中,这相差9.7 MB。


for I := 0 to 10 do
begin
  Png :=  TPngImage.Create;
  Png.Assign(Bitmaps[I]);
  Streams[I] := TMemoryStream.Create;
  Png.SaveToStream(Streams[I]);
  Bitmaps[I].Free;
  Png.Free;
end;

When you released TBitmap you released two things: 当您发布TBitmap时,您发布了两件事:

  • memory to FastMM4 Delphi Heap manager 内存到FastMM4 Delphi堆管理器
  • HBITMAP Windows GDI Handle HBITMAP Windows GDI句柄

Sometimes Windows would shrink Working Set if you minimize all the program's windows and then restore them back, but it is not granted. 有时Windows会缩小工作集,如果您最小化所有程序的窗口然后恢复它们,但它不会被授予。 Re-arranging process memory due to relatively small today 10MB difference would not be slowing down for nothing. 由于今天相对较小的10MB差异而重新安排进程内存不会减速。

Read more starting with answer at https://stackoverflow.com/a/14380287/976391 阅读更多信息,请访问https://stackoverflow.com/a/14380287/976391

http://fastmm.sourceforge.net/ comes with readme and demo how to measure memory usage inside process, if it makes you curios. http://fastmm.sourceforge.net/附带自述文件和演示如何测量进程内存使用情况,如果它让你好奇。

But if you really want to reduce RAM usage - then do not allocate it. 但如果你真的想减少RAM的使用 - 那就不要分配它。 Just make one TBitmap and assign al PNGs from it. 只需制作一个TBitmap并从中分配al PNG。 Though again, your Widows clearly said that 10MB is not the figure to care about. 尽管如此,你的寡妇明确表示10MB不是值得关注的数字。


also, as user539848 spotted, you have 11 bitmaps, not 10. To avoid off-by-one errors you better do not use magical constants but use reliable compiler constructs. 另外,正如user539848所发现的那样,你有11个位图,而不是10.为了避免逐个错误,你最好不要使用神奇的常量,而是使用可靠的编译器结构。

for I := Low(Bitmaps) to High(Bitmaps) do
begin
  Bitmaps[I] :=...

The TPngImage keep all the pixels ready and unpacked in order to be able to quickly pain them. TPngImage保持所有像素准备就绪并解压缩,以便能够快速消除它们。 TPngImage takes about the same amount of memory as TBitmap. TPngImage占用的内存大小与TBitmap大致相同。

So, simply assigning a bitmap to the TPngImage will just copy the data. 因此,只需将位图分配给TPngImage就可以复制数据。 Thus, after freeing the bitmap your program will end up using the same amount of memory. 因此,在释放位图后,程序将最终使用相同数量的内存。

If you expect memory usage to drop to 1.76 KB png, you have to compress the PNG to a memory stream and free the TPngImage, as the code in your update does. 如果您希望内存使用率降至1.76 KB png,则必须将PNG压缩为内存流并释放TPngImage,如更新中的代码所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM