Acceptable memory usage as a function of input size

I know that if a computation takes linear or linearithmic time based on the size of the input, that’s good, and if it takes quadratic time, then that’s not so good.

However, what about memory usage? Suppose a program takes a file as input and does something with that file. Is it okay for the memory usage to be linear in the size of the file or should it be constant?

3

If you’re reading a file, it’s pretty hard to it to be a constant. In general, these rules aren’t so strict. If your data is always really small, having quadratic (+) computation/memory usage isn’t really that bad. If it’s good enough for your situation, it’s fast enough to not need refactoring.

In general though, you want polynomial time computation/memory because anything above that gets way too slow with even relatively small inputs.

1

If memory usage increase is linear then there’s always the possibiliy of running out of memory, if the input is very very large. Then you have to code around that either by swapping some pieces of data to disc (though you might be able to rely on the operating system to do this for you, but either way it will slow down processing time), or changing the way you process data. What you really have to ask is:

Is it likely that my input will be so large that the amount of memory necessary to process it will exceed the available memory?

There might be a way to calculate this before you begin processing, it probably depends on the specific problem you’re working on.

…or buy more memory. 😉

I would distinguish two main categories of procedures / approaches:

  1. A procedure loads some data into main memory, performs some operation, and saves the result to the disk.
  2. A procedure runs as a filter reading a data stream and producing a new data stream as output, which is saved on the fly. When the end of the input stream is reached, the procedure terminates.

In the first case (e.g. reading, editing, saving a document), memory usage can be linear (or even quadratic or more) in the size of the input data: the largest size for the input data will be determined by the amount of available memory. You can use this approach when your input data is small enough wrt the available memory.

In the seconds case (e.g. filtering relevant information from a large log file) even a linear memory usage can be undesirable, since it is easy to run out of memory as soon as the input stream is large enough. For problems of category 2 I would only accept a solution that can run in constant (stack and heap) memory.

Whether your solution falls into category 1 or 2 can depend on the (expected) size of the data. Take for example sorting. If you need to sort 100 MB of strings you can just load the data into main memory and use an in-memory algorithm. On the other hand, if you need to sort 1 TB of data, you should rather consider an algorithm that uses constant main memory (like some implementations of merge sort).

The problem of file input possibly exceeding available memory is an old, well known, and basically solved issue. This is what a buffered file reader is all about. Your buffer size is the maximum amount of memory your file will take up at a time. The idea is to read the input up to the maximum buffer size, process that chunk, then read in the next chunk. This makes for constant memory usage (at worst).

What if your process requires more than what’s available in a chunk? There are a bunch of algorithms out there for dealing with that sort of thing too. Worst case, you may need a larger buffer and possibly more memory, but it still should have a constant maximum.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật