Sorry for my poor english :)
Thank you all.
Here’s a example. i use create 1000 0000 instance of class or struct.
and then i run top command to see it’s memory usage.
public static def main(args:Rail[String]) {
val W = 1000;
val H = 10000;
val locs = new Rail[Location2](W*H);
for(var i:Long=0;i<W;i++) {
for(var j:Long=0;j<H;j++) {
// use struct
//val loc = new Location(i, j);
//locs(i) = loc;
// use class
val loc2 = new Location2(i, j);
locs(i) = loc2;
}
}
System.sleep(10000);
}
static struct Location {
val i:Long;
val j:Long;
public def this(i:Long, j:Long) {
this.i = i;
this.j = j;
}
}
static class Location2 {
val i:Long;
val j:Long;
public def this(i:Long, j:Long) {
this.i = i;
this.j = j;
}
}
The result:
COMMAND MEM RPRVT
a.out 155M 155M // use struct
a.out 410M 410M // use class
As you can see 100 0000 instances cost 400+M memory and i need to hold them all.
So i can’t afford it when there’s more and more instances.
Like Josh said, i tried to use several array to store all fields separately.
But i think it not looks too good and clean.
On the other side, i wish to use HashSet to store all the instances.
And it’s kind of tricky to use array to represent the fields.
在 2014年10月19日,下午10:30,Josh Milthorpe <josh.miltho...@gmail.com> 写道:
> Hi Wang,
>
> if I understand correctly, your question is how to eliminate the memory
> overhead of storing as objects rather than structs; specifically, the cost of
> storing a pointer for each data element, and any padding. If you're using
> Native X10, the standard C++ advice about order of data members will help
> reduce padding overhead
> (http://www.cprogramming.com/tutorial/size_of_class_object.html ). However
> this will not eliminate pointer overhead.
>
> Can you convert your data from an array of classes to a class with array
> members? (In C++ these layouts are usually referred to as Array of Structs
> [AoS] versus Struct of Arrays [SoA].)
>
> For example, instead your existing class containing a single data item:
>
> class Data {
> val i:Int;
> val j:Int;
> var flag: Boolean;
> var value: Int;
> // other fields and methods
> }
> val data = new Data[N]();
>
> you could have a single class containing all data:
>
> class AllData {
> val i:Rail[Int]; // i coords for all data (size N)
> val j:Rail[Int]; // j coords in same order
> val flag:Rail[Boolean];
> val value:Rail[Int];
> // etc.
> }
> val allData = new AllData(N);
>
> As X10 Rails are just like C++ arrays, no pointers are required for an
> individual data item. Note that all of the fields of AllData must be simple
> scalar/struct types - a Rail of object types would still require pointers.
>
> Of course, the fields for each data item are now widely spaced in memory
> across the different arrays. How this affects cache use will depend on how
> your application accesses the data.
>
> Do you think this would work?
>
> Cheers,
>
> Josh
>
>
> On 19 October 2014 09:40, WangChen <wang...@163.com> wrote:
> Thanks for your quick response.
>
> But I don’t think intern will suits here.
> I mean my program need to create 1 billion instances of Data class. And all
> of them are different.
>
>
> 在 2014年10月19日,下午9:22,Vijay Saraswat <vi...@saraswat.org> 写道:
>
> > Not sure I am following you.
> >
> > Which of the billion instances do u want to eliminate?
> >
> > You could write a small piece of code to intern objects yourself, but
> > since you also have a field you need to mutate you need to think
> > carefully how u are going to manage mutation and sharing.
> >
> > (Interning is fairly well understood for Java-like languages -- u can
> > start with
> > http://stackoverflow.com/questions/7035659/good-pattern-for-creating-an-object-that-supports-interning
> > )
> >
> > On 10/19/14, 8:48 AM, WangChen wrote:
> >> Hi,
> >>
> >> I have a class named Data and it has some basic field like this:
> >>
> >> class Data {
> >> val i:Int;
> >> val j:Int;
> >> var flag: Boolean;
> >> var value: Int;
> >> // other fields and methods
> >> }
> >>
> >> My program will create many instances of Data, lets say 1 billion, which
> >> will cost a lot of memory since every instance need extra spaces.
> >>
> >> And i can’t use structs here because they are immutable but i need to
> >> update fields of it.
> >>
> >> So what can i do here ?
> >> Is there a way to manage memory explicitly?
> >>
> >>
> >>
> >>
> >> ------------------------------------------------------------------------------
> >> Comprehensive Server Monitoring with Site24x7.
> >> Monitor 10 servers for $9/Month.
> >> Get alerted through email, SMS, voice calls or mobile push notifications.
> >> Take corrective actions from your mobile device.
> >> http://p.sf.net/sfu/Zoho
> >> _______________________________________________
> >> X10-users mailing list
> >> X10-users@lists.sourceforge.net
> >> https://lists.sourceforge.net/lists/listinfo/x10-users
> >
> >
> > ------------------------------------------------------------------------------
> > Comprehensive Server Monitoring with Site24x7.
> > Monitor 10 servers for $9/Month.
> > Get alerted through email, SMS, voice calls or mobile push notifications.
> > Take corrective actions from your mobile device.
> > http://p.sf.net/sfu/Zoho
> > _______________________________________________
> > X10-users mailing list
> > X10-users@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/x10-users
>
>
>
> ------------------------------------------------------------------------------
> Comprehensive Server Monitoring with Site24x7.
> Monitor 10 servers for $9/Month.
> Get alerted through email, SMS, voice calls or mobile push notifications.
> Take corrective actions from your mobile device.
> http://p.sf.net/sfu/Zoho
> _______________________________________________
> X10-users mailing list
> X10-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/x10-users
>
> ------------------------------------------------------------------------------
> Comprehensive Server Monitoring with Site24x7.
> Monitor 10 servers for $9/Month.
> Get alerted through email, SMS, voice calls or mobile push notifications.
> Take corrective actions from your mobile device.
> http://p.sf.net/sfu/Zoho_______________________________________________
> X10-users mailing list
> X10-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/x10-users
------------------------------------------------------------------------------
Comprehensive Server Monitoring with Site24x7.
Monitor 10 servers for $9/Month.
Get alerted through email, SMS, voice calls or mobile push notifications.
Take corrective actions from your mobile device.
http://p.sf.net/sfu/Zoho
_______________________________________________
X10-users mailing list
X10-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/x10-users