no_one

no_one

Very high memory usage when streaming file with Phoenix

Hello, I have discovered Elixir recently and I have decided to do some projects with it (and Phoenix).

I need to take file from one server and stream it to client through my server. I wanted to do it with streams. My HttpStream looks almost exactly as this: https://labs.civilcode.io/elixir/2018/05/03/stream-files.html.

Streaming works but when I want to access bigger file (for example ~4 GB movie) my application consumes large amounts of memory, and after ~20 seconds video stops playing.

My controller’s function looks like this:

def show(conn, _params) do
    url = "http://localhost:8080/Bigfile.mp4"
    %{headers: proxy_headers} = HTTPoison.head!(url)
    proxy_headers = for {k, v} <- proxy_headers, do: {String.downcase(k), v}, into: %{}

    chunked_conn =
      conn
      |> put_resp_content_type("video/mp4")
      |> put_resp_header("content-length", proxy_headers["content-length"])
      |> send_chunked(200)

    url
    |> HttpStream.stream()
    |> Stream.map(fn n -> chunk(chunked_conn, n) end)
    |> Stream.run()
  end

It’s not beautiful but I’ll improve that later. From my observations chunk/2 is called too fast (if I insert :timer.sleep(1) before stream_next(resp) memory usage is normal but it’s much slower).

This is memory usage from :observer (images are from imgur and they appear to be cropped here):
https://imgur.com/SSOjNie

And very interesting thing is that one process has very long message queue:
https://imgur.com/leANyk4

How to fix this?

Marked As Solved

wanton7

wanton7

Nice I accidentally deleted my reply and forum says “You’ve performed this action too many times. Please wait 23 hours before trying again.” when I try to undelete. I’m pretty sure I haven’t undeleted anything before and post will deleted 24 hours :slight_smile:

Here are those links again
https://github.com/elixir-plug/plug_cowboy/issues/10
https://github.com/ninenines/cowboy/issues/1376

Also Liked

no_one

no_one

I’ve implemented this like in linked issue and now it works.

wanton7

wanton7

Tried your code and downgraded {:plug_cowboy, "~> 2.0"} to {:plug_cowboy, "~> 1.0"} in deps and it seems to fix this problem and memory doesn’t increase anymore.

no_one

no_one

Still nothing, it loads whole file into memory (it uses 4.6 GB of RAM, almost exact size of my video)

EDIT: Maybe I should use something else than conn, but what and how integrate it into Phoenix app?

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement