The Artima Developer Community
Sponsored Link

Java Answers Forum
ArrayList Performance

3 replies on 1 page. Most recent reply: Apr 2, 2003 4:15 PM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Sanjay

Posts: 6
Nickname: sanjay
Registered: Mar, 2002

ArrayList Performance Posted: Apr 1, 2003 3:04 AM
Reply to this message Reply
Advertisement
I am using ArrayList collection. I have two threads. One of the threads is inserting objects in the ArrayList(always appending) and another thread continuosly monitors the arraylist and gets objects from it at index 0. It also removes the object from ArrayList after getting the object. The thread inserting objects in ArrayList inserts at a very high speed around 13000 objects per second. A total of 2 Lakh objects are inserted into the ArrayList.

When I run this program it runs very well for sometimes and rate of insertion and removal of Objects from ArrayList is almost equal i.e. 13000 per second.But sometimes the processing slows down a lot in between from the end where the thread is getting the objects from ArrayList.The performance drops down to around 600 objets per second. This happens irregularly i.e sometimes program runs well and other times it slows down

Can anyone help?


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: ArrayList Performance Posted: Apr 1, 2003 8:37 AM
Reply to this message Reply
Sounds like a LinkedList might be better for this sort of task, since adding and removing items from either end of it is relatively quick. Adding or removing or adding items to or from the beginning of an ArrayList is a costly operation.

Fortunately, you are probably using the base interface, so the change to your code is trivial.

If you code looks like this:
   List lakhObjects = new ArrayList();

Try changing it to this:
   List lakhObjects = new LinkedList();

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: ArrayList Performance Posted: Apr 2, 2003 10:55 AM
Reply to this message Reply
By the way, if you do try this out, or find another solution, I'd be interested to know the results (how much faster it is).

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: ArrayList Performance Posted: Apr 2, 2003 4:15 PM
Reply to this message Reply
Arrays memory must be alloacted contiguously. ArrayList uses array of Object class to store all its elements internally. Event though the real elements, which are objects to be stored can be allocated space anywhere on heap the refernces that will refer to all of them must have contiguous memory. As long as OS is able to serve JVM's contiguos (big) memory request fast your application runs fast. Since you are adding and removing elements from arrayList in bulk , there is a lot of array copying going on inside placing frequent memory allocation demands. In fact, the performance of your application depends on many factors beyond your control. Most of them are memory allocation/delocation. If you use linked list then memory allocation will be fast because it doesn't have to be contigous and in bulk.

Flat View: This topic has 3 replies on 1 page
Topic: REALLY NEED HELP PLEASE ON AN ASSIGNMENT Previous Topic   Next Topic Topic: tomcat and mod_jk2 problems

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use