Efficient Use of the Vector Class

Efficient Use of the Vector Class

The Vector class in java.util is one of the most frequently used classes in Java development. It almost makes the problem of memory management non-existent, unlike a fixed-size array. However, using this class doesn’t come without a cost. At instantiation, a Vector object allocates memory for 10 object references by default. This leads us to ask, “What happens when we add an 11th element to the vector?” Take a look at the following method for populating a Vector with 200 elements:

  1. public void vectorCapacity () {
  2. Vector v = new Vector();
  3. int capacity = v.capacity();
  4. int elementNo = 0;
  5. // Add 200 elements
  6. for (int i = 0; i < 200; i++) {
  7. v.addElement("FOO");
  8. // If capacity changes, print out new capacity
  9. if (capacity != v.capacity()) {
  10. capacity = v.capacity();
  11. System.out.println(i + "th element, Capacity = " +
  12. v.capacity());
  13. }
  14. }
  15. }

Calling this method produces the following output:

 10th element, Capacity = 2020th element, Capacity = 4040th element, Capacity = 8080th element, Capacity = 160160th element, Capacity = 320

When a Vector exceeds 10 objects, its capacity is increased by 10. The new capacity also defines the chunk of memory for the next aggregation. In other words, the capacity of a Vector doubles with each memory reallocation. In the example given above, the memory had to reallocate five times in order to store 200 elements.

You can avoid this problem by estimating in advance how many objects will be stored in the Vector. To increase the chunk of memory that gets allocated each time the Vector needs to increase its capacity. You can accomplish this by inserting the following between lines 3 and 4 in the code snippet above:


The output now shows:

 100th element, Capacity = 200

The number of memory reallocations is reduced to one instead of five. If you plan to use the Vector for a large number of objects, try to estimate the chunk of memory that you would want during each reallocation.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes