High memory usage in vibe.d application

Anton Fediushin fediushin.anton at yandex.com
Fri Jun 29 11:01:41 UTC 2018


On Friday, 29 June 2018 at 10:21:24 UTC, Radu wrote:
> On Friday, 29 June 2018 at 09:44:27 UTC, Anton Fediushin wrote:
>> Almost forgot, there are two timers which call this function 
>> for two different streams.
>>
>> Value of `metaint` is 16000, which means that only 16KB of 
>> memory are allocated for the `buffer`, then it reads another 
>> byte which contains length of the metadata / 16 and then it 
>> reads the metadata which is 100-200 bytes long.
>>
>> This gives us... 16KiB per one nowPlaying() call. Why doesn't 
>> it free the memory?
>
> Maybe use the 
> https://dlang.org/phobos/std_experimental_allocator_mallocator.html instead of theAllocator as it defaults to GC.

Thanks, I'll try that.

>
> Also, why you .idup the array? .array already creates a new one 
> on the heap.

It does, but it creates char[] and I need a string. I changed 
code a little bit to remove unnecessary `map` and `idup` too.

Code now:

```
@safe string nowPlaying(string url) {
	import vibe.core.stream;
	import std.experimental.allocator;
	import std.experimental.allocator.mallocator;
	import std.string;

	string r;
	url.requestHTTP(
		(scope req) {
			req.headers.addField("Icy-MetaData", "1");
		},
		(scope res) {
			RCIAllocator a = allocatorObject(Mallocator.instance);

			auto metaint = res.headers.get("icy-metaint").to!int;
			auto buffer = a.makeArray!ubyte(metaint);
			scope(exit) a.dispose(buffer);
			res.bodyReader.read(buffer, IOMode.all);

			auto lengthBuffer = a.makeArray!ubyte(1);
			scope(exit) a.dispose(lengthBuffer);
			res.bodyReader.read(lengthBuffer, IOMode.all);

			auto dataBuffer = a.makeArray!ubyte(lengthBuffer[0] * 16);
			scope(exit) a.dispose(dataBuffer);
			res.bodyReader.read(dataBuffer, IOMode.all);

			r = dataBuffer.split('\'').drop(1).front.array.assumeUTF;

			res.disconnect;
		}
	);
	return r;
}
```

I will deploy that and see if it changes anything.



More information about the Digitalmars-d-learn mailing list