...

/

Using Large Numbers of Objects

Using Large Numbers of Objects

Learn which design patterns to use to achieve efficiency when using a large number of object instances.

Problem statement

Let’s imagine that we have a requirement to use many similar objects in our application. Perhaps, we’re building a distributed application based on microservices architectureIt’s the type of application architecture where applications are developed as a collection of services. and each object represents a unique connection to one of our service instances. Or maybe we’re interacting with multiple database entries, and we need an object representing a database connection for each one.

In this situation, purely using built-in language features will probably be problematic. We’ll need to instantiate every single one of these objects. Once the object is out of scope, our runtime will need to get rid of it to free up the memory. If we need a similar object again, we’ll instantiate it again.

If we follow this approach, we’ll probably experience a performance hit. Instantiating a new object each time we need to use it is a relatively expensive process. The runtime will need to allocate memory for it and populate those chunks of memory with new values.

If we’re using similar objects in different ...