Improve model performance with AnyLogic’s memory analyzer: use cases overview

Optimization of random access memory usage with AnyLogic memory analyzer

We all expect our software to run smoothly and swiftly; there is no room or time for crashes or freezes. For model developers, this means prioritizing model performance and using optimization techniques.

Join us as we dive into two compelling examples that showcase how this innovative tool can improve your model’s performance.

Contents:

  1. Overview of the memory analyzer in AnyLogic
  2. Use cases of applying the memory analyzer
  3. Best optimization techniques for simulation models
  4. Improving model performance with AnyLogic

Overview of the memory analyzer in AnyLogic

With the recent release of AnyLogic 8.9, we've introduced the memory analyzer. It is a powerful built-in tool designed to help you spot performance bottlenecks, identify memory leaks, and fine-tune your models by applying different optimization techniques.

To start using the memory analyzer, run your model and activate the analyzer. In the top menu, choose the Model section and click on Memory dump.

Location of memory analyzer in the AnyLogic interface
Route to the memory analyzer in the AnyLogic interface

Once activated, the memory analyzer tracks how different components of your model consume memory during execution. It generates detailed reports highlighting which parts of your model use the most memory, including specific objects, classes, and instances that take up significant amounts of random access memory (RAM).

Use cases of applying the memory analyzer to improve model performance

In complex processes, effective model optimization requires managing elements that demand a lot of RAM. After building a model, it can be challenging to identify which parts use the most memory.

The memory analyzer helps you pinpoint these resource-intensive components, allowing you to optimize them to improve model performance. Let's go through an example of how the memory analyzer can help you find such an issue.

Case 1: Memory bottleneck identification

Creating a separate agent to handle statistics across different parts of your model is a common practice in simulation modeling. Let's call this agent StatisticsAgent and add the necessary statistical elements.

The statistical elements: loading time, unloading time, resources utilization, transporter speed, breakdowns quantity, and waiting time
The necessary statistical elements inside StatisticsAgent

You can easily drag and drop this StatisticsAgent into another agent's markup space to see its elements in action.

The structure of the model used in this example
The structure of the model used in this example
The AnyLogic interface showing lack of RAM
Memory bottleneck that hampers the model performance

However, when you run this simple model, you might notice that it uses up almost all the allocated memory.

Thanks to the memory analyzer, you can quickly identify which object consumes the most RAM and improve the model’s performance. In this case, the Top Consumers section shows that the cause is in the StatisticsAgent—specifically, 1,500 of them.

Top Consumers chart showing the breakdown of elements affecting model performance
Top Consumers chart showing the breakdown of elements affecting model performance
The AnyLogic interface with StatisticsAgent wrong setup
StatisticsAgent wrong setup

The StatisticsAgent was set to create a Population of agents instead of a Single agent. This mistake created 1,499 unnecessary StatisticsAgent objects, which used up all the memory and prevented the model from running.

Fortunately, removing these extra agents reduced memory usage to just 10%. This highlights how crucial model optimization is for improving model performance.

Even simple models can quickly use all available memory and crash if they are not built properly. This example showed how a small mistake can lead to poor memory use.

Case 2: Memory leak fixing

Now, let's look at a common issue in model development—Java-related memory leaks. A memory leak happens when unused objects remain in the program's memory and the garbage collector can't remove them. This leads to unnecessary RAM use, which affects the model. Through a practical example, let’s figure out how the memory analyzer can help you detect and fix these leaks to improve model performance.

In AnyLogic, you can create agents using code. For example, let's define a Storage agent as one that holds a collection of part numbers. To keep it simple, we'll assume all the part numbers are identical. When you add a few of these agents, the model quickly uses all available memory and crashes.

Properties for adding Storage agent
Properties for adding Storage agent
Memory analyzer referring to defined storage collection
Memory analyzer referring to defined storage collection

Using the memory analyzer, we can see that the storage collections cause the issue. Even after removing the agents and their collections, the memory isn't fully released.

It turns out that the collection type is an ArrayList, which uses an array internally. When you clear the array, its elements turn null, and the Java garbage collector will eventually remove them. However, the array's size doesn't change, so it consumes memory.

To fix this, when you remove a collection with a large number of objects, you should not only clear the collection but also adjust the array size using the correct function. Here is the code for removing an agent with a huge collection:

for (Storage agent: storages){
agent.partNumbers.clear();
agent.partNumbers.trimToSize();
}
while(!storages.isEmpty()){
remove_storages (storages.get(0));
}
System. gc (); 

This action ensures proper memory release, improving model performance.

Best optimization techniques for simulation models

In the use cases above, we have reviewed two common issues affecting model performance. If you are looking for some other optimization techniques, here are several options to consider:

  1. Minimize the number of agents
    Keep only the essential agents in your model. Too many agents can waste memory and processing power.
  2. Stick to the simplified logic
    Use simple logic in your model. Avoid complex decision trees and unnecessary calculations. More straightforward logic improves model performance and makes it easier to understand and debug.
  3. Efficient data handling
    Store only the data you truly need, and use efficient data structures. Large datasets and poor data handling affect model optimization. Consider using indexed collections or hash maps for faster data access.
  4. Leverage built-in functions
    Use AnyLogic's built-in functions and libraries, as they are designed for optimal model optimization.
  5. Run batch experiments
    A batch experiment is a good option to test multiple scenarios and parameters automatically. It helps you find the best settings to improve model performance.
  6. Optimize resource usage
    Manage memory and CPU usage carefully. Use tools like the memory analyzer to find and fix memory leaks or bottlenecks.
  7. Utilize parallel processing
    If possible, use parallel processing to run several simulations simultaneously. This reduces computation time and improves overall model performance.

Improving model performance with AnyLogic

Model optimization is essential for ensuring that it operates smoothly and efficiently. AnyLogic has introduced a powerful memory analyzer that enhances your model’s efficiency by identifying and addressing its weaknesses.

By leveraging this tool and following optimization techniques, you can significantly improve your model's performance and be sure that it operates well and provides you with realistic data. We hope this post will encourage you to test the AnyLogic memory analyzer in your projects.


Benefit from the best modeling technology with AnyLogic software.

Get the latest version

Postes connexes