|
View:
New views
13 Messages
—
Rating Filter:
Alert me
|
|
|
Basic Java learning of the dayThis doesn't work, to keep a List fixed at a certain size:
List<String> l = new LinkedList<String>(); while (true) { l.add(0, "New item"); if (l.size() > 5) { l = l.subList(0, 5); } } What can happen - especially on a system with limited memory? I found this problem on Android. |
|
|
Re: Basic Java learning of the dayI haven't programmed android, but I'll take a stab at this. As we
know, subList() does not have well-defined semantic when you try to modify the original list - like in l.add(0, "New Item"). If this doesn't throw a fail-fast ConcurrentModificationException, within the loop, then my guess would be a heap problem due to memory leak. List.subList() is just a "view" of the original list, the sublist reference could *still* hold references to the pre-existing elements in the original list. (Recall our basic LinkedList data structure implementation Node1->Node2->Node3). A not-so smart garbage collector might miss this. - James On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> wrote: > This doesn't work, to keep a List fixed at a certain size: > > List<String> l = new LinkedList<String>(); > while (true) { > l.add(0, "New item"); > if (l.size() > 5) { > l = l.subList(0, 5); > } > } > > What can happen - especially on a system with limited memory? I found > this problem on Android. > > > ------------------------------------ > > Yahoo! Groups Links > > > > -- JAMES ARNOLD A. FAELDON |
|
|
Re: Basic Java learning of the daystack overflow.
===== ANGOL ===== -----|-^_^X@^_^, =====|+^_^X++~_~,@----- "The only thing worse than a hopeless romantic is a hopeful one" Magbasa bago Mamuna. Mag-isip bago mambatikos Without Truth there is no Justice, Without Justice, there is Tyranny Semper fi Proof of Desire is Pursuit www.onthe8spot.com igan.long@... 09173822367 On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> wrote: > > > This doesn't work, to keep a List fixed at a certain size: > > List<String> l = new LinkedList<String>(); > while (true) { > l.add(0, "New item"); > if (l.size() > 5) { > l = l.subList(0, 5); > } > } > > What can happen - especially on a system with limited memory? I found > this problem on Android. > > |
|
|
Re: Basic Java learning of the dayNot sure about Android, but in jsdk, I think this eventually reach an OOM
due to the continuous creation of hard referenced objects (one for l.add(..) and another for l.subList(...)) . Furthermore, once it enters the if-statement (l.size() > 5), it will always enter it on every other while-iteration after it because the linked list will just continue to grow (the subList of LinkedList will contain a reference to the original and modifying the sublist will modify the original list).....i think :-) hehehe On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> wrote: > > > This doesn't work, to keep a List fixed at a certain size: > > List<String> l = new LinkedList<String>(); > while (true) { > l.add(0, "New item"); > if (l.size() > 5) { > l = l.subList(0, 5); > } > } > > What can happen - especially on a system with limited memory? I found > this problem on Android. > > -- Franz Allan Valencia See | Java Software Engineer franz.see@... LinkedIn: http://www.linkedin.com/in/franzsee |
|
|
Re: Basic Java learning of the daylooks like an infinite loop:)
jC On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> wrote: > > > This doesn't work, to keep a List fixed at a certain size: > > List<String> l = new LinkedList<String>(); > while (true) { > l.add(0, "New item"); > if (l.size() > 5) { > l = l.subList(0, 5); > } > } > > What can happen - especially on a system with limited memory? I found > this problem on Android. > > |
|
|
Re: Basic Java learning of the dayOut of memory in the heap I doubt if the call stack has something to do with
this. Unless there is some implementation detail at the API level not apparent. Can you explain why you think this could cause a stack overflow? On Mon, Oct 26, 2009 at 9:15 PM, Giancarlo Angulo <igan.long@...>wrote: > > > stack overflow. > > ===== > ANGOL > ===== > -----|-^_^X@^_^, =====|+^_^X++~_~,@----- > > "The only thing worse than a hopeless romantic is a hopeful one" > > Magbasa bago Mamuna. Mag-isip bago mambatikos > > Without Truth there is no Justice, > Without Justice, there is Tyranny > > Semper fi > > Proof of Desire is Pursuit > > www.onthe8spot.com > igan.long@... > 09173822367 > > > On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> wrote: > >> >> >> This doesn't work, to keep a List fixed at a certain size: >> >> List<String> l = new LinkedList<String>(); >> while (true) { >> l.add(0, "New item"); >> if (l.size() > 5) { >> l = l.subList(0, 5); >> } >> } >> >> What can happen - especially on a system with limited memory? I found >> this problem on Android. >> > > > > -- JAMES ARNOLD A. FAELDON |
|
|
Re: Basic Java learning of the daywasn't thinking . Would probably be an OutOfMemory error, malamang na heap
problem. ===== ANGOL ===== -----|-^_^X@^_^, =====|+^_^X++~_~,@----- "The only thing worse than a hopeless romantic is a hopeful one" Magbasa bago Mamuna. Mag-isip bago mambatikos Without Truth there is no Justice, Without Justice, there is Tyranny Semper fi Proof of Desire is Pursuit www.onthe8spot.com igan.long@... 09173822367 On Wed, Oct 28, 2009 at 3:25 PM, James Faeldon <jafaeldon@...> wrote: > > > Out of memory in the heap I doubt if the call stack has something to do > with this. Unless there is some implementation detail at the API level not > apparent. Can you explain why you think this could cause a stack overflow? > > > > On Mon, Oct 26, 2009 at 9:15 PM, Giancarlo Angulo <igan.long@...>wrote: > >> >> >> stack overflow. >> >> ===== >> ANGOL >> ===== >> -----|-^_^X@^_^, =====|+^_^X++~_~,@----- >> >> "The only thing worse than a hopeless romantic is a hopeful one" >> >> Magbasa bago Mamuna. Mag-isip bago mambatikos >> >> Without Truth there is no Justice, >> Without Justice, there is Tyranny >> >> Semper fi >> >> Proof of Desire is Pursuit >> >> www.onthe8spot.com >> igan.long@... >> 09173822367 >> >> >> On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> wrote: >> >>> >>> >>> This doesn't work, to keep a List fixed at a certain size: >>> >>> List<String> l = new LinkedList<String>(); >>> while (true) { >>> l.add(0, "New item"); >>> if (l.size() > 5) { >>> l = l.subList(0, 5); >>> } >>> } >>> >>> What can happen - especially on a system with limited memory? I found >>> this problem on Android. >>> >> >> >> >> > > > -- > JAMES ARNOLD A. FAELDON > > > |
|
|
Re: Basic Java learning of the dayYes this can cause a stack overflow (the overflow depends on allotted
memory for stacktrace). What happen on the code below for every subLIst invocation: The subList method will return a wrapper List object, so on second call the sublist will return a wrapper List for that wrapper list object, and this goes on and on for every subList call. @see AbstractList#SubList the wrapper sublist class Alternative Solution: instead of using subList use the remove method instead, this way no wrapper List object is created and you can save a lot of stack call invocations. On Oct 28, 2009, at 3:25 PM, James Faeldon wrote: > Out of memory in the heap I doubt if the call stack has something to > do with this. Unless there is some implementation detail at the API > level not apparent. Can you explain why you think this could cause a > stack overflow? > > > > On Mon, Oct 26, 2009 at 9:15 PM, Giancarlo Angulo > <igan.long@...> wrote: > > > stack overflow. > > ===== > ANGOL > ===== > -----|-^_^X@^_^, =====|+^_^X++~_~,@----- > > "The only thing worse than a hopeless romantic is a hopeful one" > > Magbasa bago Mamuna. Mag-isip bago mambatikos > > Without Truth there is no Justice, > Without Justice, there is Tyranny > > Semper fi > > Proof of Desire is Pursuit > > www.onthe8spot.com > igan.long@... > 09173822367 > > > On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> > wrote: > > This doesn't work, to keep a List fixed at a certain size: > > List<String> l = new LinkedList<String>(); > while (true) { > l.add(0, "New item"); > if (l.size() > 5) { > l = l.subList(0, 5); > } > } > > What can happen - especially on a system with limited memory? I found > this problem on Android. > > > > > > > -- > JAMES ARNOLD A. FAELDON > > > |
|
|
Re: Basic Java learning of the dayMiguel, what error did you get?
===== ANGOL ===== -----|-^_^X@^_^, =====|+^_^X++~_~,@----- "The only thing worse than a hopeless romantic is a hopeful one" Magbasa bago Mamuna. Mag-isip bago mambatikos Without Truth there is no Justice, Without Justice, there is Tyranny Semper fi Proof of Desire is Pursuit www.onthe8spot.com igan.long@... 09173822367 On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> wrote: > > > This doesn't work, to keep a List fixed at a certain size: > > List<String> l = new LinkedList<String>(); > while (true) { > l.add(0, "New item"); > if (l.size() > 5) { > l = l.subList(0, 5); > } > } > > What can happen - especially on a system with limited memory? I found > this problem on Android. > > |
|
|
Re: Basic Java learning of the dayOn Thu, Oct 29, 2009 at 9:25 AM, Giancarlo Angulo <igan.long@...> wrote:
> Miguel, what error did you get? I got the OutOfMemoryError, with the stack trace showing repeated nested calls to subList(). Too bad this happens - the subList() way of doing things is a clean functional style. |
|
|
Re: Basic Java learning of the dayAfaik, instantiation gets allocated in the heap not the stack. The while
loop is an iteration and the method call subList() doesn't seem to point to any recursion (direct or indirect) that would get the stack filled up. public List<E> subList(int fromIndex, int toIndex) { return (this instanceof RandomAccess ? new RandomAccessSubList<E>(this, fromIndex, toIndex) : new SubList<E>(this, fromIndex, toIndex)); } On Wed, Oct 28, 2009 at 6:35 PM, Alvin R. de Leon <alvin.deleon@...>wrote: > > > Yes this can cause a stack overflow (the overflow depends on allotted > memory for stacktrace). > > *What happen on the code below for every subLIst invocation:* > The subList method will return a wrapper List object, so on second call the > sublist will return a wrapper List for that wrapper list object, and this > goes on and on for every subList call. > > @see AbstractList#SubList the wrapper sublist class > > *Alternative Solution:* > * > * > instead of using subList use the remove method instead, this way no wrapper > List object is created and you can save a lot of stack call invocations. > > > On Oct 28, 2009, at 3:25 PM, James Faeldon wrote: > > > > Out of memory in the heap I doubt if the call stack has something to do > with this. Unless there is some implementation detail at the API level not > apparent. Can you explain why you think this could cause a stack overflow? > > > On Mon, Oct 26, 2009 at 9:15 PM, Giancarlo Angulo <igan.long@...>wrote: > >> >> >> stack overflow. >> >> ===== >> ANGOL >> ===== >> -----|-^_^X@^_^, =====|+^_^X++~_~,@----- >> >> "The only thing worse than a hopeless romantic is a hopeful one" >> >> Magbasa bago Mamuna. Mag-isip bago mambatikos >> >> Without Truth there is no Justice, >> Without Justice, there is Tyranny >> >> Semper fi >> >> Proof of Desire is Pursuit >> >> www.onthe8spot.com >> igan.long@... >> 09173822367 >> >> >> On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> wrote: >> >>> >>> >>> This doesn't work, to keep a List fixed at a certain size: >>> >>> List<String> l = new LinkedList<String>(); >>> while (true) { >>> l.add(0, "New item"); >>> if (l.size() > 5) { >>> l = l.subList(0, 5); >>> } >>> } >>> >>> What can happen - especially on a system with limited memory? I found >>> this problem on Android. >>> >> >> >> >> > > > -- > JAMES ARNOLD A. FAELDON > > > > > > > -- JAMES ARNOLD A. FAELDON |
|
|
Re: Basic Java learning of the dayHello,
Yup there are no recursion, but delegation to wrapper objects while any methods (add, size and subList) are invoke would take a stack call. the number of elements added would be equal to the number of wrapped list object, which would mean the number of stack call too. :D This also means that the parent list would always exist and would always has reference which would not be GC which may cause OOM. So this either causes an OOM or stack overflow depends on what system configuration dictates. -Xms<size> set initial Java heap size -Xmx<size> set maximum Java heap size -Xss<size> set java thread stack size Regards, Alvin On Oct 29, 2009, at 1:19 PM, James Faeldon wrote: > Afaik, instantiation gets allocated in the heap not the stack. The > while loop is an iteration and the method call subList() doesn't > seem to point to any recursion (direct or indirect) that would get > the stack filled up. > > > public List<E> subList(int fromIndex, int toIndex) { > return (this instanceof RandomAccess ? > new RandomAccessSubList<E>(this, fromIndex, toIndex) : > new SubList<E>(this, fromIndex, toIndex)); > } > > > > On Wed, Oct 28, 2009 at 6:35 PM, Alvin R. de Leon <alvin.deleon@... > > wrote: > > > Yes this can cause a stack overflow (the overflow depends on > allotted memory for stacktrace). > > What happen on the code below for every subLIst invocation: > The subList method will return a wrapper List object, so on second > call the sublist will return a wrapper List for that wrapper list > object, and this goes on and on for every subList call. > > @see AbstractList#SubList the wrapper sublist class > > Alternative Solution: > > instead of using subList use the remove method instead, this way no > wrapper List object is created and you can save a lot of stack call > invocations. > > > On Oct 28, 2009, at 3:25 PM, James Faeldon wrote: > >> >> Out of memory in the heap I doubt if the call stack has something >> to do with this. Unless there is some implementation detail at the >> API level not apparent. Can you explain why you think this could >> cause a stack overflow? >> >> >> >> On Mon, Oct 26, 2009 at 9:15 PM, Giancarlo Angulo <igan.long@... >> > wrote: >> >> >> stack overflow. >> >> ===== >> ANGOL >> ===== >> -----|-^_^X@^_^, =====|+^_^X++~_~,@----- >> >> "The only thing worse than a hopeless romantic is a hopeful one" >> >> Magbasa bago Mamuna. Mag-isip bago mambatikos >> >> Without Truth there is no Justice, >> Without Justice, there is Tyranny >> >> Semper fi >> >> Proof of Desire is Pursuit >> >> www.onthe8spot.com >> igan.long@... >> 09173822367 >> >> >> On Wed, Oct 21, 2009 at 7:44 PM, Miguel Paraz <mparaz@...> >> wrote: >> >> This doesn't work, to keep a List fixed at a certain size: >> >> List<String> l = new LinkedList<String>(); >> while (true) { >> l.add(0, "New item"); >> if (l.size() > 5) { >> l = l.subList(0, 5); >> } >> } >> >> What can happen - especially on a system with limited memory? I found >> this problem on Android. >> >> >> >> >> >> >> -- >> JAMES ARNOLD A. FAELDON >> >> > > > > > > > -- > JAMES ARNOLD A. FAELDON > > > |
|
|
Re: Basic Java learning of the day> Too bad this happens - the subList() way of doing things is a clean
> functional style. > > good catch. however the javadoc clearly states that the returned sublist is still backed by the original list. so I think that is the correct behavior based on the List API. http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html#subList(int, int) "For example, the following idiom removes a range of elements from a list: list.subList(from, to).clear();" http://www.docjar.com/html/api/java/util/ArrayList.java.html > ------------------------------------ > > Yahoo! Groups Links > > > > |
| Free embeddable forum powered by Nabble | Forum Help |