You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove Non-Bookmarked Lines - Notepad++ stuck and memory leak #13442
Notepad++ stuck and trying to eat all available RAM when using “Remove Non-Bookmarked Lines”.
Steps to Reproduce the Issue
Open some text file, in my case it's IIS log file (52Mb or 139231 in lines).
Find > Mark > check “Bookmark line” write some text to find and click “Mark All”, in my case I search “10.212.136.1” and it’s marked 315 lines.
Search > Bookmark > “Remove Non-Bookmarked Lines” - now Notepad++ get stuck and trying to eat all available RAM.
Debug Information
Notepad++ v8.5.1 (64-bit)
Build time : Mar 21 2023 - 19:16:10
Path : C:\1\npp.8.5.1.portable.x64\notepad++.exe
Command Line :
Admin mode : OFF
Local Conf mode : ON
Cloud Config : OFF
OS Name : Windows 11 Pro (64-bit)
OS Version : 22H2
OS Build : 22621.1413
Current ANSI codepage : 1251
Plugins :
mimeTools (2.9)
NppConverter (4.5)
NppExport (0.4)
This bug in Notepad++ version 8.4.6 - 8.5.1, 8.4.5 work fine.
So long as this isn't working, couldn't you just use Copy bookmarked lines instead and paste them in another tab?
That said, this is an really irritating bug and I was able to replicate it with a file as small as 10MB.
I think the issue here from a programming perspective is that the algorithm used to remove non-bookmarked lines removes their text one at a time. This may (if my high-level understanding is correct) to repeated resizing of the edit buffer and moving of the caret, which is potentially an operation that scales linearly with the size of the document. So in short, it looks like the current algorithm is potentially taking k times as long as it needs to, where k is the number of bookmarked lines. EDIT: it's more complicated than that for sure.
What we should be doing with remove non-bookmarked lines is to find all the bookmarked lines, string-join them together with the document's EOL character, clear the buffer, and set the text in the buffer to the bookmarked lines.
I'm not super sanguine about working on this myself, because this involves a lot of low-level memory management and my understanding of memory management tools in C/C++ is terrible. I will try, but I will probably fail. But I wanted to lay out my high-level reasoning in case anyone else wants to take a crack at it.
Is there any way to turn off Change History to test this theory?
I'm not convinced that's the issue, because I've tested this on a similar large file (38 MB, 770,000 lines total, 315 lines bookmarked, remove non-bookmarked lines) on versions older than 8.4.6 (last tested on 8.3.3) and I still get freezing.
This might just be an artifact of my weird setup (I have several dozen different versions of Notepad++, both 32bit and 64bit, installed in differently named directories) that might interfere with diagnosis of a problem that could only be replicated on a "pure" installation with only one version installed.
But I really think it makes a lot of sense to implement my high-level suggestion to not remove bookmarked lines one-at-a-time, and instead find the bookmarked lines, add them to a new string, and replace the entire file with the new string once that's done.
As a proof of concept, I took a file where Remove non-bookmarked lines caused NPP to freeze, used Copy marked lines instead, then did Ctrl+A to select the whole document and pasted the bookmarked lines to overwrite it. The total time required for all these operations was very short, probably a few hundred milliseconds.
Looking at the code in Notepad_plus.cpp for all the functions involving multiple marked lines, it seems like they all do this thing of
for every line in the document:
if the line is bookmarked:
replace the text with something
whereas I would prefer (and am happy to try to implement on my own and then contribute a PR)
lines_to_include_list = new list<string>
for every line in document:
if it should be included:
add it to lines_to_include_list
new_text = stringjoin lines_to_include_list with EOL character
overwrite buffer with new_text
I haven't thought too deeply about the whole idea, but one thing that immediately comes to mind is that the approach you've outlined would show that every line has now been modified (in "change history"), which isn't really desirable.
We recently had a similar problem with another command. Unfortunately replacing the whole thing (it's fast) but it also removes the bookmarks. They can be saved before deletion and restored after deletion, but then there is a problem with Undo/Redo. We have a dilemma here: either performance or greater usability. For this previous command we stayed with greater usability (unfortunately it's slower).
Thanks for explaining this to me. I'm still going to try to fool around with my re-implementation, mostly because I'm still new to C++ and this is a pretty good learning exercise, but I for one am satisfied with this explanation.
In a perfect world, the user would get some sort of notification every time it was predicted that an operation would take a super long time, so that they could opt-in to the more performant option, but obviously that would be annoying and fiddly to implement, and would likely result in some false positives that would lead angry users to demand removal of the warnings.
Notepad++ 8.5.7 has the issue as well. It allocates a stunning amount of memory (over 38,000 MB, see screenshot) while removing unbookmarked lines from a large, but not insanely large file (~25MB).
The pattern of deletions here leads to change history storing large amounts of repeated data. When a deletion removes text that already contains change history, those changes are remembered so they can be restored if the deletion is undone. Any bytes in the deletion range that were inserted since starting history are remembered along with any deletions in the deletion range except for the start of the deletion range. The start is treated specially as any deletions present at the start did not move so are still correct.
All the deletions that were remembered so they can be restored are also added to the deletion list at the start of the deletion, this allows the display of information about different deletions at this position: the user may have deleted some text (unsaved deletion mark); performed a save (saved deletion mark); then deleted more at the same position (both saved and unsaved deletion mark).
As more deletions are performed at a position, its list of deletions grows. Since deleteMarkedLines is moving backwards with almost all deletions ending at the previous deletion's start (which contains a growing list of deletions); larger and larger sets of deletions are remembered for each line. Eventually memory is filled, performance slows, and finally the process runs out of memory although this didn't appear to throw bad_alloc in testing.
If deleteMarkedLines moved forwards then the 'special treatment' mentioned above means that there won't be growing lists of deletions to remember and there won't be problems. Something similar to:
for (intptr_t i = 0; i < lastLine ;)
{
if (bookmarkPresent(i) == isMarked)
{
deleteMarkedline(i);
lastLine--;
}
else
{
i++;
}
}
There will still be a lot of time spent handling each line individually. Each deletion notifies the application and updates the scroll bars. Thus it would improve speed a lot to handle deletions in blocks: find the first line not bookmarked then loop forward over non-bookmarked lines to where a bookmarked line or the end is found, then delete the whole range of non-bookmarked lines at once before repeating for more blocks of lines.
Change history may be optimized for this case of deleting contiguous ranges backwards by compressing the remembered deletions as they often have repeated items (deletion for edition E at position P) which could be combined into a single item with a count. Its unclear how this will impact other cases as it will increase memory use when changes are more random or dispersed.
Reacted by Yaron10, xomx, Dum4G, Don HO and Michael Heath
Activity
molsonkiko commentedon Apr 7, 2023
So long as this isn't working, couldn't you just use
Copy bookmarked linesinstead and paste them in another tab?That said, this is an really irritating bug and I was able to replicate it with a file as small as 10MB.
I think the issue here from a programming perspective is that the algorithm used to remove non-bookmarked lines removes their text one at a time. This may (if my high-level understanding is correct) to repeated resizing of the edit buffer and moving of the caret, which is potentially an operation that scales linearly with the size of the document. So in short, it looks like the current algorithm is potentially taking
ktimes as long as it needs to, wherekis the number of bookmarked lines. EDIT: it's more complicated than that for sure.What we should be doing with
remove non-bookmarked linesis to find all the bookmarked lines, string-join them together with the document's EOL character, clear the buffer, and set the text in the buffer to the bookmarked lines.I'm not super sanguine about working on this myself, because this involves a lot of low-level memory management and my understanding of memory management tools in C/C++ is terrible. I will try, but I will probably fail. But I wanted to lay out my high-level reasoning in case anyone else wants to take a crack at it.
ArkadiuszMichalski commentedon Apr 7, 2023
https://github.com/notepad-plus-plus/notepad-plus-plus/wiki/Changes#846
I assume it's because
Change Historyis enabled?molsonkiko commentedon Apr 7, 2023
Is there any way to turn off
Change Historyto test this theory?I'm not convinced that's the issue, because I've tested this on a similar large file (38 MB, 770,000 lines total, 315 lines bookmarked,
remove non-bookmarked lines) on versions older than 8.4.6 (last tested on 8.3.3) and I still get freezing.This might just be an artifact of my weird setup (I have several dozen different versions of Notepad++, both 32bit and 64bit, installed in differently named directories) that might interfere with diagnosis of a problem that could only be replicated on a "pure" installation with only one version installed.
ArkadiuszMichalski commentedon Apr 7, 2023
Turn off it here:

alankilborn commentedon Apr 7, 2023
Isolate by grabbing and extracting the "portable" package for whatever version you want to test.
molsonkiko commentedon Apr 7, 2023
@ArkadiuszMichalski , your suggestion didn't help.
@alankilborn , thanks, I'll try that now.
But I really think it makes a lot of sense to implement my high-level suggestion to not remove bookmarked lines one-at-a-time, and instead find the bookmarked lines, add them to a new string, and replace the entire file with the new string once that's done.
As a proof of concept, I took a file where
Remove non-bookmarked linescaused NPP to freeze, usedCopy marked linesinstead, then didCtrl+Ato select the whole document and pasted the bookmarked lines to overwrite it. The total time required for all these operations was very short, probably a few hundred milliseconds.Looking at the code in
Notepad_plus.cppfor all the functions involving multiple marked lines, it seems like they all do this thing ofwhereas I would prefer (and am happy to try to implement on my own and then contribute a PR)
alankilborn commentedon Apr 7, 2023
@molsonkiko said:
I haven't thought too deeply about the whole idea, but one thing that immediately comes to mind is that the approach you've outlined would show that every line has now been modified (in "change history"), which isn't really desirable.
ArkadiuszMichalski commentedon Apr 7, 2023
We recently had a similar problem with another command. Unfortunately replacing the whole thing (it's fast) but it also removes the bookmarks. They can be saved before deletion and restored after deletion, but then there is a problem with Undo/Redo. We have a dilemma here: either performance or greater usability. For this previous command we stayed with greater usability (unfortunately it's slower).
molsonkiko commentedon Apr 7, 2023
Ohhhh, that makes sense. 🤔
Thanks for explaining this to me. I'm still going to try to fool around with my re-implementation, mostly because I'm still new to C++ and this is a pretty good learning exercise, but I for one am satisfied with this explanation.
In a perfect world, the user would get some sort of notification every time it was predicted that an operation would take a super long time, so that they could opt-in to the more performant option, but obviously that would be annoying and fiddly to implement, and would likely result in some false positives that would lead angry users to demand removal of the warnings.
There ain't no such thing as a free lunch.
ArkadiuszMichalski commentedon Apr 7, 2023
@molsonkiko
You could read discussion on this PR 12739 and check both slower/faster versions.
burnchar commentedon Sep 28, 2023
Notepad++ 8.5.7 has the issue as well. It allocates a stunning amount of memory (over 38,000 MB, see screenshot) while removing unbookmarked lines from a large, but not insanely large file (~25MB).
Yaron10 commentedon Sep 30, 2023
#14188.
nyamatongwe commentedon Oct 2, 2023
The pattern of deletions here leads to change history storing large amounts of repeated data. When a deletion removes text that already contains change history, those changes are remembered so they can be restored if the deletion is undone. Any bytes in the deletion range that were inserted since starting history are remembered along with any deletions in the deletion range except for the start of the deletion range. The start is treated specially as any deletions present at the start did not move so are still correct.
All the deletions that were remembered so they can be restored are also added to the deletion list at the start of the deletion, this allows the display of information about different deletions at this position: the user may have deleted some text (unsaved deletion mark); performed a save (saved deletion mark); then deleted more at the same position (both saved and unsaved deletion mark).
As more deletions are performed at a position, its list of deletions grows. Since
deleteMarkedLinesis moving backwards with almost all deletions ending at the previous deletion's start (which contains a growing list of deletions); larger and larger sets of deletions are remembered for each line. Eventually memory is filled, performance slows, and finally the process runs out of memory although this didn't appear to throwbad_allocin testing.If
deleteMarkedLinesmoved forwards then the 'special treatment' mentioned above means that there won't be growing lists of deletions to remember and there won't be problems. Something similar to:There will still be a lot of time spent handling each line individually. Each deletion notifies the application and updates the scroll bars. Thus it would improve speed a lot to handle deletions in blocks: find the first line not bookmarked then loop forward over non-bookmarked lines to where a bookmarked line or the end is found, then delete the whole range of non-bookmarked lines at once before repeating for more blocks of lines.
Change history may be optimized for this case of deleting contiguous ranges backwards by compressing the remembered deletions as they often have repeated items (deletion for edition E at position P) which could be combined into a single item with a count. Its unclear how this will impact other cases as it will increase memory use when changes are more random or dispersed.
Yaron10 commentedon Oct 2, 2023
@nyamatongwe,
👍
Thank you for the detailed analysis and suggestions.
Significantly reduce memory used for deleting contiguous ranges backw…
Significantly reduce memory used for deleting contiguous ranges backw…
Update Scintilla to v5.3.8 & Lexilla to v5.2.8
Update Scintilla to v5.3.8 & Lexilla to v5.2.8
Significantly reduce memory used for deleting contiguous ranges backw…