I'm trying to keep floats contained in their respective sections in a book environment. However, using the placeins package, I get this weird behavior where the floats flushed at the end of the document take up an entire page, regardless of their size, as shown below:

I would like instead to have the flushed floats laid out as compactly as possible, and the next section starting on the same page, below the last float, if there's enough space. How can I achieve that?
Please note that I haven't had much luck using \afterpage{\clearpage} either.
Here is the code to reproduce that document (placeholder.png is just a square, as you can see in the first picture).
\documentclass[twoside, a4paper, 12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{pdfpages}
\usepackage[section]{placeins}
\usepackage{lipsum}
\usepackage{graphicx}
\begin{document}
\chapter{First chapter}
\section{First section}
\lipsum[1-5]
\begin{figure}[h!]
\centering
\makebox[\textwidth][c]{\includegraphics[height=15 cm]{example-image}}
\end{figure}
\begin{figure}[h!]
\centering
\makebox[\textwidth][c]{\includegraphics[width=0.5\textwidth]{example-image}}
\caption{Caption.}
\end{figure}
\section{Second section}
Second section content.
\end{document}
[h!]everywhere is simply wrong. Using!everywhere is wrong is means "ignore the user set constraints for this one special case" ignoring your own constraints in every case is weird. Usingheverywhere means floats are not allowed at top of page (not) or bottom of page (nob) or on a page of floats (nop) so basically you are not allowing them to be placed anywhere so they will all drift to the end unless you flush them out with\clearpage– David Carlisle Nov 28 '22 at 14:04articleclass andexample-image(which is available for tests) and check the problem occurs in that form – David Carlisle Nov 28 '22 at 14:09\makebox[\textwidth][c]{which are doing nothing useful. – David Carlisle Nov 28 '22 at 14:20example-image. I'm trying to fix the problem within thebookclass so I'm sticking with that. Thanks for the comments about[h!]and\makebox[\textwidth][c]{, I'll keep that in mind. Before making further comments, I'm trying to read a bit more about[h!]and figure placement, so that I can understand better what I'm supposed to do on the actual document. – Banjo Nov 28 '22 at 14:45hby itself really that problematic? I just want to have the figures as close as possible to the relevant parts of the text. Apart from the issue mentioned in my post, I have no problem with the figure placement in my actual document : they're at the top or the bottom if need be, even when using [h!] everywhere. Not sure why!is that problematic either, it just seems to lift some LaTeX restrictions. – Banjo Nov 28 '22 at 15:11hfloat, this means that there needs to be text above and below. If that is not possiblehis removed, and we look at the rest. Her we havet, now if the float is too large to be atfloat is is placed back into the queue and sincetis the only possibilitytis tested (and fails) on each page until the end of the document or the next\clearpage. This is where thepplacement is relevant. – daleif Nov 28 '22 at 15:22hhas been changed tohtso as error recovery it will be allowed at the top of later pages. But not includingpmakes it much harder to set a float and much more likely it drifts to the end. Yes!drops constraints but normally yu do not want them dropped (eg allowing just a single line of text under a float) and in cases where you do want to relax the constraints, better to set relaxed values than set strict values you then explicitly ignore – David Carlisle Nov 28 '22 at 15:22pis checked and the figure will be placed as soon as possible. – daleif Nov 28 '22 at 15:23