Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3439150
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:16:26+00:00 2026-05-18T08:16:26+00:00

I am writing a gen_server which I want to hold an ets table as

  • 0

I am writing a gen_server which I want to hold an ets table as a state, then ets table was created somewhere else. How should I add this to the state of the gen_server?

I want to use the ets table rather than create a new dictionary for it because I want to save memory.

Also, How does one iterate through a ets table? I want to iterate or read each value in the table and check the value, then I want to do one of two options depending on the value.

Would it be easier just to turn the ets table into a list and traverse the list?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:16:27+00:00Added an answer on May 18, 2026 at 8:16 am

    Some suggestions:

    • Read the ETS man page: erl -man ets
    • An ETS table is identified either by its name (in the case of the named_table option) or by its table id. Pass that information to the gen_server and keep it in the state:

      -record(state, { ..., tbl = none }).
      
      
      init([TableID]) ->
          ...,
          {ok, #state { tbl = TableID }}.
      

    ETS will perhaps not save that much memory. There is a new flag coming up for a later Erlang/OTP release where ETS tables can be compressed so their contents are compressed before storage and uncompressed upon reads (there is a computational overhead to this).

    To iterate through an ETS table you have several options. ets:first/1 ets:next/2 is one such interface. ets:foldl/3 ets:foldr/3 another. ets:match/3 gives you a continuation (a cursor) to walk with. ets:select is even more general than match.

    Would it be easier to turn it into a list? This depends. The power of an ETS table is that they have an option {keypos, N} defining a key on which elements are stored. ets:lookup(?TAB, Key) is extremely fast so you have fast lookups on keys. It is not so with lists. But on the other hand, if you always traverse all of the list it may be a simpler solution (as long as you don’t pass the large list between processes).

    Turning the whole table into a list and traversing it should perhaps be avoided. You will generate the list in memory and then traverse it which is expensive. It is way better to traverse it a bit at a time so the amount of live memory is low.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing an iPhone app in which I want to save the user the grief
I'm writing a gen_server, which we'll just call gen_server_db, with nothing too terribly special
I'm writing http session manager (gen_server based). That server creates and removes session from
Writing a python program, and I came up with this error while using the
I am writing an application which encrypts and decrypts the user notes based on
Writing a SQL query that should return the average charges for a preceding 91
Writing a program in which I need to split strings from a struct linked
I am writing a Clojure class using gen-class . I have a state field
This is my first ever attempt at writing a recursive function in c. The
I'm writing this after a good while of frustrating research, and I'm hoping someone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.