rxi / microui

A tiny immediate-mode UI library
MIT License
3.54k stars 249 forks source link

RAM Footprint #62

Open DiegoJArg opened 1 year ago

DiegoJArg commented 1 year ago

Being a minimalistic immediate mode UI, I can't understand why 30 Mb of Private RAM usage is being measured on windows with ProcessHacker. I noted that SDL2 calls opengl, but I don't think that this is the reason here. The editor "exi/lite" is about 8 Mb under the same conditions. I would expect a footprint if the range of some kb.

image image

rilysh commented 1 year ago

Not only SDL but there are GL calls here and there in the demo program. If you closely see, the demo program first initializes SDL which allocates some space on the heap. Then moving on there's SDL_CreateWindow(...), SDL_GL_CreateContext(...), and some GL functions which individually allocates several amount of space. and they never get free'd by the program itself. Although the kernel will free the space automatically, there can be a situation where allocated space may swap out.

I'm unsure about Windows internals and the meaning of private bytes. If it means dynamic memory, ProcessHacker perhaps also reports leaked memory that the demo program creates.

I've patched the memory leak issues with SDL, and one other. It does satisfy sanitizer but valgrind still complains about memory leaks. Currently don't have enough time, but a deep look will reveal it. Here's the patch file: https://pastebin.com/SWUMNJ6q

darltrash commented 6 months ago

Microui uses about 264~kb, checked it.

DiegoJArg commented 6 months ago

Is 'lite' setting up a different kind of GL context with less memory footprint?

rilysh commented 6 months ago

No, OpenGL isn't a user level software. The memory usage is from SDL.

If you take a look in example, particularly here, you can see that in the demo SDL is initializing all its subsystem, which individually allocates memory. For example, the demo only needs video and a few basic contexts from SDL but it's initializing other contexts too, (e.g. an audio context).

See: https://github.com/libsdl-org/SDL/blob/efaa58732abb3faf3900b2d93a166b955fbd8ed0/include/SDL.h#L82

Even if you only initialize the video subsystem, I don't think memory footprint will be under some KiB.

DiegoJArg commented 6 months ago

Hi again,

So, I have tried today to reduce the SDL subsystems to match with the "lite" that I am comparing with. I just got a reduction from 30 to 29 Mb of RAM usage.

https://github.com/rxi/lite/blob/38bd9b3326c02e43f244623f97a622b11f074415/src/main.c#L71

  //SDL_Init(SDL_INIT_EVERYTHING);
  SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);

image

Then I thought that probably lite is not using opengl at all, but actually SDL with gdi. But this test will take some more time, since the microui example is using all GL calls.

  //window = SDL_CreateWindow( NULL,  SDL_WINDOWPOS_UNDEFINED,  SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL);
  window = SDL_CreateWindow( NULL,  SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,  width, height, SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_HIDDEN);

Is there any working example on how to use microui with GDI for normal desktop applications ?

rilysh commented 6 months ago

I'm not sure about it. Lite seems to only uses SDL for handling every graphics context and SDL under the hood uses OpenGL and GDI (Graphics Device Interface on Windows).

I don't think GDI is the main reason of being "lightweight" on memory. GDI is a legacy component in Windows, mainly used for 2D graphics. But again, I'm not sure how process hacker counts "Private Bytes" in this context.

What's the memory usage in task manager of that application? Is that exactly same as "Private Bytes"?

DiegoJArg commented 6 months ago

yes, gdi is legacy, but legacy-support also means high-portability, and that its associated libraries are preloaded by the OS. However, I have the un-expert theory that opengl interaction with graphics cards requires more buffering of textures, context and scenes, that are not required with GDI. So far, I have seen almost all smallest possible apps with opengl to take 28Mb of Ram as a bare start. I wonder if one could use microui with the bare canvas drawings of a gdi, direct2d or whatever, or is it just fixed to ogl?