• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clone versus creating new Object

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,
I have an application. For example
***************************************
public class A{
ArrayList _arrayList;

public A(){
_arrayList = new ArrayList();
}


public method1(){
ArrayList _arr = new ArrayList();
_arr(someValue); // which is changes everytime.
_arrayList.add(_arr);
}

}

********************************

I my application i need to call my method1() more than 50,000 times. So it create 50,000 times create new _arr object.

So my question is that if i use following method2(), so is this efficient memory wise. IS this not create new Object on memory.


public class B{
ArrayList _arrayList, _arr;

public B(){
_arrayList = new ArrayList();
_arr = new ArrayList();
}


public method2(){
ArrayList _arr1 = _arr.clone();
_arr1 (someValue); // which is changes everytime.
_arrayList.add_arr1 );
}

}
***************************************

Please give me some suggestion which one is better, using clone method in class B, instead of creating new object in class A


thanks in advance
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd avoid clone() unless it was really required. Try displaying current time in millis before and after a loop that creates 50,000 ArrayLists and let us know just how long it takes in your setup.
 
ghazanfar khan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks James,
But i just give you an exmaple, actually i am getting real time data from 5 different exchnages. The arraylist i am creating inside method1(), it content around 40 cloumns and then added into Main arrayList.

Can please put some light on why you avoid clone. What is the reason.
Because my application running fine but at the end of the market close. It is taking too much memory but my CPU usage is around 5 to 9% only which is good but memory wise it is creating problem, like if i want to minimize my application frame or try to write some email nothing is working.

Please give me some advise.

method1(){
creating new arrylist // scope wise it is within the method scope but i am storing this arraylist reference into main reference.
might be thst is the reason.

anyways thanks, and please tryo to put some light on "why you avoid clone"

thanks
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
clone() creates a new object, but copies the contents of an existing object afterwards; that means clone() does more work than simply using "new". The memory usage of your application would not be affected at all by using clone(), although it might be slower.

Perhaps instead of storing all these lists of lists in memory, you could look into using a database!
 
ghazanfar khan
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for your reply Ernest, but i can not go for the database(only when at the end of the day i go for database). Because is there another suggestion besides Database.

thanks in advance
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A flat file, I suppose; or maybe you can find a way to make the objects smaller, or use something more efficient than lists of lists.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic