nheko icon indicating copy to clipboard operation
nheko copied to clipboard

High memory usage?

Open alphapapa opened this issue 8 years ago • 22 comments

Hi again,

After building from master again and running strip on the binary to get it down to 7.1 MB, I wondered if it would use less memory. But looking at the nheko process now, sitting in 6 rooms, only 2 of which have had any event history loaded since starting the program, the RSS is at 220 MB, with 53.8 MB shared, which seems high.

Is there something about the Qt5 controls that nheko uses that's causing this, or is there a bug? 220 MB (or even 170) just seems excessive for how much actual data should currently be resident in the program.

Thanks.

alphapapa avatar Jan 17 '18 19:01 alphapapa

Running nightly from today gives me

ohno

on my @Half-Shot:half-shot.uk account. Which contains about 276 joined rooms.

Half-Shot avatar Jan 17 '18 20:01 Half-Shot

massif might help.

mujx avatar Jan 17 '18 20:01 mujx

Ok, I ran with massif:

  1. valgrind --tool=massif nheko
  2. Waited for window to appear.
  3. Clicked on each room in the room list.
  4. Tray icon->Quit.

Here's the output: massif.out.txt

alphapapa avatar Jan 17 '18 22:01 alphapapa

I think the working theory is that this is caused by creating widgets for every single event, rather than just the ones in view in the room you're currently looking at.

Ralith avatar Jan 18 '18 07:01 Ralith

Here's the output: massif.out.txt

Using massif visualizer you can see that 52 MB are the user avatars 67MB the state events. Peak memory usage was 162MB.

One optimization could be to store the user avatars in cache.

mujx avatar Jan 18 '18 07:01 mujx

I think the working theory is that this is caused by creating widgets for every single event, rather than just the ones in view in the room you're currently looking at.

This has to be tested that is not affecting performance (by removing and restoring widgets). Also there are other issues like remembering scrollbar position etc. But probably that's the best bet to keep memory low.

mujx avatar Jan 18 '18 07:01 mujx

I have account with 150+ rooms (see also mujx/nheko#89) and I have pretty same results as what I have with Riot (except CPU usage where nheko is better): screenshot from 2018-02-23 22-59-31 I think it's that so because avatar of hundreds of rooms with thousands of users all stored in RAM uncompressed. Maybe some algorithm could be implemented to compress loaded avatars (and load big ones when you look at avatar closer, zooming it) and unload cached avatars from rooms which weren't visited in last N minutes, something like that. Am I right?

unclechu avatar Feb 23 '18 18:02 unclechu

After next run, without initial sync, it eats less than 500 MiB: screenshot from 2018-02-24 00-57-53 Could it be memory leak? Or is it just reserved free memory by OS?

unclechu avatar Feb 23 '18 19:02 unclechu

@unclechu The avatars are downloaded on demand (i.e only those that are visible when you open a room). I am still investigating to find out exactly what data are responsible for such high memory.

mujx avatar Feb 25 '18 10:02 mujx

There are some improvements regarding the memory usage currently in the master branch. Let me now if those updates bring memory usage back to normal levels.

mujx avatar Apr 22 '18 11:04 mujx

@mujx I tried to build it by make release but it failed, see last parts of log: nheko-2018-04-22-1642f3c-build-error.log

unclechu avatar Apr 22 '18 12:04 unclechu

@unclechu This is issue #298. Check the readme for the supported compilers. You can grab an nightly appimage just to test this out.

mujx avatar Apr 22 '18 13:04 mujx

After first start (setting up cache was mentioned in log) of nighlty release (nheko-862.nightly-1.x86_64.rpm) it ate a lot (I have 150+ rooms): screenshot from 2018-04-22 19-40-29 But after restart it's okay: screenshot from 2018-04-22 19-42-03 As I remember I was having about 250MiB approximately, not it's about 135MiB.

unclechu avatar Apr 22 '18 14:04 unclechu

I have a problem with nheko running in the system tray for a long time. Then the window want show up and needs a restart.

ghost avatar Apr 30 '18 18:04 ghost

@notTito Is it related to high memory usage? If not open a new issue with some info about the version you're using and your system.

mujx avatar Apr 30 '18 18:04 mujx

I will try. Thing is it takes time to reproduce but it happens reliably. OS is Manjaro Linux, nheko build φrom AUR.

ghost avatar Apr 30 '18 18:04 ghost

screenshot from 2018-05-07 14-01-09 3.3 GiB after initial sync with 0.4.0 from repos of Fedora 28 Workstation. screenshot from 2018-05-07 14-21-16 And about 150 MiB after second run (to compare Riot in Firefox 59 usually eats about 2 GiB).

unclechu avatar May 07 '18 09:05 unclechu

I caught it do the same but memory usage is normal 50mb, but all I see is a window decoration with an empty content.

ghost avatar May 07 '18 17:05 ghost

@notTito it depends on how many rooms do you have. In my case it's about 150 rooms.

unclechu avatar May 07 '18 20:05 unclechu

So during initial sync the huge json has to be parsed into a json type and then copied again to a strongly typed representation.

That will cause a spike in memory but on Linux that reserved memory might not be released back to the OS. That doesn't mean that nheko still uses that. After a restart, memory appears to be normal.

On the other hand on macOS (and Windows probably?) the memory is released immediately so it won't be noticeable.

Some solutions:

  • Use a more memory-efficient json library or struct representation.
  • Protocol improvements to lazy load data.

mujx avatar May 21 '18 06:05 mujx

On the other hand on macOS (and Windows probably?) the memory is released immediately so it won't be noticeable.

Why would that be the case, compared to Linux? If that were so, it would seem that Linux systems would be generally short of memory compared to macOS/Windows systems for the same tasks.

alphapapa avatar May 24 '18 07:05 alphapapa

Why would that be the case, compared to Linux? If that were so, it would seem that Linux systems would be generally short of memory compared to macOS/Windows systems for the same tasks.

It's up to the memory allocator on each system to decide how the memory is managed. This doesn't mean that memory will always be reserved.

From https://www.gnu.org/software/libc/manual/html_node/Freeing-after-Malloc.html#Freeing-after-Malloc

Occasionally, free can actually return memory to the operating system and make the process smaller. Usually, all it can do is allow a later call to malloc to reuse the space. In the meantime, the space remains in your program as part of a free-list used internally by malloc.

mujx avatar May 24 '18 07:05 mujx