[Qt-interest] QSslSocket: massive difference in app memory usage when encryption enabled

Markus Goetz Markus.Goetz at nokia.com
Wed Oct 14 16:49:54 CEST 2009


ext Stephen Collyer wrote:
> I'm using Qt 4.5.0 on Opensuse 11.1
>
> I have an HTTP/HTTPS implementation based on QTcpServer and QSslSocket.
>
> When emitting a file in response to a GET request, memory usage is
> minimal when handling an unencrypted http request. However,
> when handling an encrypted https request, memory usage increases
> dramatically, and I get the impression that Qt is buffering the whole
> file before any writes to the underlying socket takes place.
>
> The code in skeleton looks like this:
>
> class HTTPReader : public QSSlSocket
> {
> ...
> };
>
> In the ctor we connect readyRead():
>
> HTTPReader::HTTPReader()
> {
> ...
> connect(this, SIGNAL( readyRead() ),    this, SLOT( read_client() ));
> }
>
> and in read_client() we eventually handle GET requests, with code like 
> this:
>
> // each time we write some data, emit the next chunk
> connect(this, SIGNAL( bytesWritten(qint64) ), SLOT( emit_file_chunk() ));
> // and emit the first chunk ..
> emit_file_chunk();
>
> where emit_file_chunk() does the following loop:
>
> void HTTPReader::emit_file_chunk()
> {
> ...
> while (bytesToWrite() < http_socket_buffer_length_)
> {
>     bytes_read = GET_file_->read(http_get_file_buffer_, 
> http_get_file_buffer_length_);
>     if (bytes_read > 0)
>     {
>         bytes_written = write(http_get_file_buffer_, bytes_read);
>
>         if (bytes_written == -1)
>         {
>             GET_file_->close();
>             close();
>             break;
>         }
>     }
>     else if (bytes_read == 0)
>     {
>         GET_file_->close();
>         break;
>     }
> }
>
> This code is supposed to top up the QSSlSocket buffer to the tunable
> value of http_socket_buffer_length_, then to return to yield to the
> event loop, when it will be run again the next time bytesWritten() is 
> emitted.
>
> This seems to work fine for non-encrypted sockets but has an unacceptable
> memory footprint for encrypted sockets. There seems to be a fundamental
> difference in buffering when QSSlSocket is encrypted, to the extent 
> that the
> app's VSZ reaches the size of the file being sent in response to the GET.
>
> Does anyone have any insight into what's going on here ?
Yes, Maybe.

1. With a QSslSocket, you'd have to check encryptedBytesToWrite() since 
those are the bytes going out on the actual TCP socket. So, to be on the 
safe side do check for (encryptedBytesToWrite() + bytesToWrite() < 
http_socket_buffer_length_)

2. You should also connect encryptedBytesWritten() signal to your 
emit_file_chunk.

I hope that helps! If yes/no, please report back:)
Markus



More information about the Qt-interest-old mailing list