From c4da08592538fc3e9a1b05a95afeac5ee582adc5 Mon Sep 17 00:00:00 2001 From: lytico Date: Thu, 18 Mar 2021 19:01:48 +0100 Subject: [PATCH] add test for https://github.com/GtkSharp/GtkSharp/issues/226 --- .../Sections/Miscellaneous/PixbufSection.cs | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 Source/Samples/Sections/Miscellaneous/PixbufSection.cs diff --git a/Source/Samples/Sections/Miscellaneous/PixbufSection.cs b/Source/Samples/Sections/Miscellaneous/PixbufSection.cs new file mode 100644 index 000000000..bbd431c6f --- /dev/null +++ b/Source/Samples/Sections/Miscellaneous/PixbufSection.cs @@ -0,0 +1,93 @@ +using System; +using System.Threading; +using Gdk; +using Gtk; + +namespace Samples +{ + + [Section (ContentType = typeof(PixbufDemo), Category = Category.Miscellaneous)] + class PixbufSection : ListSection + { + + public PixbufSection () + { + AddItem ($"Press button to run / stop {nameof(PixbufDemo)} :", new PixbufDemo ("Press me")); + } + + } + + class PixbufDemo : Button + { + + public PixbufDemo (string text) : base (text) { } + + private bool running = false; + + public void DispatchPendingEvents () + { + // The loop is limited to 1000 iterations as a workaround for an issue that some users + // have experienced. Sometimes EventsPending starts return 'true' for all iterations, + // causing the loop to never end. + + int n = 1000; + Gdk.Threads.Enter (); + + while (Gtk.Application.EventsPending () && --n > 0) { + Gtk.Application.RunIteration (false); + } + + Gdk.Threads.Leave (); + } + + protected override void OnPressed () + { + base.OnPressed (); + var count = 0; + + if (running) { + running = false; + + return; + } + + var startmem = GC.GetTotalMemory (true); + var testfile = "Textpic.png"; + + using (var writeTestFile = new Pixbuf (typeof(ImageSection).Assembly, "Testpic")) { + writeTestFile.Save (testfile, "png"); + } + + using (var heatup = new Pixbuf (testfile)) { + ApplicationOutput.WriteLine ($"{nameof(heatup)}.{nameof(Pixbuf.ByteLength)}\t{heatup.ByteLength:N0}"); + } + + startmem = GC.GetTotalMemory (true); + ApplicationOutput.WriteLine ($"{nameof(GC.GetTotalMemory)} at start: {startmem:N}"); + running = true; + + var memAllocated = 0UL; + + while (running) { + + using (var source = new Pixbuf (typeof(ImageSection).Assembly, "Testpic")) { + memAllocated += source.ByteLength; + count++; + } + + DispatchPendingEvents (); + + if (!running) + break; + + } + + var endmem = GC.GetTotalMemory (true); + ApplicationOutput.WriteLine ($"Leak:\t{(endmem - startmem):N0}\t{nameof(memAllocated)}"); + ApplicationOutput.WriteLine ($"{nameof(GC.GetTotalMemory)} at start: {startmem:N0}\tat end: {endmem:N0}\t{nameof(Pixbuf)} created: {count}"); + + } + + } + +} \ No newline at end of file