Why are beam files so large?

Bjorn Gustavsson <>
Thu Dec 7 10:15:31 CET 2000


Joe Armstrong <> writes:

[...]

> Questions:
> 
> 	1) How can I do the +compressed etc. stuff from Erlang i.e.  not the shell
> 
> 	     compile:file("foo", [compressed]). 
> 
>          Didn't work

It works for me. I get a compressed file. But you'll want to write

	compile:file("foo", [compressed,no_debug_info]).

if you want the file to get much smaller.

> 
> 	2) Can I write "strip" that takes a module compiled with default
>            options and away debug info and compresses the results ???

Yes. I've included an example. We plan to include similar capability in
beam_lib in R8.

beam_strip:release(Root) compresses all beam files in a complete OTP release.
Root should be the same sort of path which code:root_dir/0 returns.

beam_strip:files(Files) takes a list of beam files.

beam_strip:file(File) takes the name of a single beam file.

Note that beam_lib (and thus my example) can't read compressed files.
If you have compressed files including debug info, you must uncompress
them first. This can be done with gunzip or by writing some Erlang cod
to do it.


> 
>   /Joe 
>

Here is a description of the beam format:

	http://www.ericsson.se/cslab/~bjorn/beam_file_format.html

/Björn

-module(beam_strip).
-export([release/1,files/1,file/1]).
-import(lists, [foreach/2]).

release(Root) ->
    files(filelib:wildcard(filename:join(Root, "lib/*/ebin/*.beam"))).

files(Fs) ->
    foreach(fun file/1, Fs).

file(Name) ->
    {ok,{Mod,Chunks}} = beam_lib:chunks(Name, ["Atom","Code","StrT","ImpT",
					       "ExpT","Attr","CInf"]),
    Stripped = build_module(Chunks),
    {ok,Fd} = file:open(Name, [raw,binary,write,compressed]),
    ok = file:write(Fd, Stripped),
    file:close(Fd).

build_module(Chunks0) when list(Chunks0) ->
    Chunks = build_chunks(Chunks0),
    Size = size(Chunks),
    0 = Size rem 4,				% Assertion: correct padding?
    <<"FOR1",(Size+4):32,"BEAM",Chunks/binary>>.

build_chunks(Chunks) -> build_chunks(Chunks, []).
    
build_chunks([{Id,Data}|Chunks], Acc) ->
    build_chunks(Chunks, [build_chunk(Id, Data)|Acc]);
build_chunks([], Acc) -> list_to_binary(Acc).

%% Build a correctly padded chunk.

build_chunk(Id, Contents) when list(Id), length(Id) == 4 ->
    build_chunk(list_to_binary(Id), Contents);
build_chunk(Id, Contents) when size(Id) =:= 4, binary(Contents) ->
    Size = size(Contents),
    [<<Id/binary,Size:32>>,Contents|pad(Size)];
build_chunk(Id, Contents) when list(Contents) ->
    build_chunk(Id, list_to_binary(Contents)).

pad(Size) ->
    case Size rem 4 of
	0 -> [];
	Rem -> lists:duplicate(4 - Rem, 0)
    end.



-- 
Björn Gustavsson            Ericsson Utvecklings AB
      ÄT2/UAB/F/P
			    BOX 1505
+46 8 727 56 87 	    125 25 Älvsjö



More information about the erlang-questions mailing list