Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Stacks in Java, Array Memory Usage Rate Topic: -----

#1 paser  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 2
  • View blog
  • Posts: 126
  • Joined: 30-May 09


Dream Kudos: 0

Share |

Stacks in Java, Array Memory Usage

Posted 14 December 2009 - 01:56 PM

Im doing a software project, and I would like to use a stack. Ive written two version of a Stack class, one that uses an expanding array and a linked list implementation. My question is, is a linked list the better implementation of a Stack in java? Since im only poping and pushing I have no need for the random access capabilities of an array. I know a linked list uses more memory per reference so im not sure. I will use this stack to hold a large number of alphanumeric values parsed in from a text file. Which makes me think that an array might be better, but im not sure. Also is there a difference in the amount of memory allocated between:

TestObject[] test = new TestObject[10];


and

TestObject[] test = new TestObject[10];

for (int i = 0; i < 10; i++)
	 test[i] = new TestObject();


basically when you create an Array, does it allocate enough memory to hold just the references or both the references and the objects?
Was This Post Helpful? 0
  • +
  • -


#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • Icon

Reputation: 105
  • View blog
  • Posts: 670
  • Joined: 15-October 09


Dream Kudos: 300

Re: Stacks in Java, Array Memory Usage

Posted 14 December 2009 - 02:22 PM

In Java, memory is (usually) not something you really need to worry about, unless you're working on portable devices where memory is paramount, so I'd be tempted to say convenience is pretty much the driver, hence I'd go for a list (probably not linkedlist, but at least array list). If memory is paramount, then yeah go for an array.

Regarding the other question about memory allocated, when you call:

TestObject[] test = new TestObject[10];


you allocate space for the test reference with 10 references to TestObject, all null. But you don't allocate memory for the objects that are going into it until you call the constructor for this object, like in the second example you gave.

Hope this answers your question.
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • Icon

Reputation: 424
  • View blog
  • Posts: 2,654
  • Joined: 19-September 08


Dream Kudos: 0

Re: Stacks in Java, Array Memory Usage

Posted 14 December 2009 - 02:47 PM

A LinkedList would be better, since it's optimised for pointer-like operations and push and pop are the easiest of those. As you say, you don't need random access, so that would rule out not only arrays, but ArrayList too, since that's backed by an array and in most cases is more expensive to maintain
Was This Post Helpful? 1
  • +
  • -

#4 paser  Icon User is offline

  • D.I.C Head
  • PipPip

Reputation: 2
  • View blog
  • Posts: 126
  • Joined: 30-May 09


Dream Kudos: 0

Re: Stacks in Java, Array Memory Usage

Posted 14 December 2009 - 04:38 PM

thanks guys, both of you were helpful
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • Java Lover who loves defau1t:
  • Icon

Reputation: 1997
  • View blog
  • Posts: 13,284
  • Joined: 06-March 08


Dream Kudos: 550

Re: Stacks in Java, Array Memory Usage

Posted 14 December 2009 - 04:41 PM

View Postpaser, on 14 Dec, 2009 - 01:56 PM, said:

basically when you create an Array, does it allocate enough memory to hold just the references or both the references and the objects?

just the reference (4 bytes I presume)
Was This Post Helpful? 0
  • +
  • -

#6 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • Icon

Reputation: 105
  • View blog
  • Posts: 670
  • Joined: 15-October 09


Dream Kudos: 300

Re: Stacks in Java, Array Memory Usage

Posted 15 December 2009 - 01:27 AM

View Postpbl, on 14 Dec, 2009 - 11:41 PM, said:

just the reference (4 bytes I presume)


Interestingly enough, it is more than that, as the following snapshot shows:

Attached Image

Even an array of size 0 takes 16B:

Attached Image

This confirms the findings of this article (which is a bit of a relief :) ).

This post has been edited by EdwinNameless: 15 December 2009 - 01:28 AM

Was This Post Helpful? 0
  • +
  • -

#7 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • Icon

Reputation: 105
  • View blog
  • Posts: 670
  • Joined: 15-October 09


Dream Kudos: 300

Re: Stacks in Java, Array Memory Usage

Posted 15 December 2009 - 04:09 AM

View Postg00se, on 14 Dec, 2009 - 09:47 PM, said:

A LinkedList would be better, since it's optimised for pointer-like operations and push and pop are the easiest of those.


Ok, I have had some time to test this assertion, and to my own surprise, it is actually not true... When implementing a stack with an ArrayList, you pop and push at the end, and adding at the end of an ArrayList does perform faster than a LinkedList.

You can see my findings here... Quite surprising. I guess the overhead of a node creation is greater than the actual array assignment (and the occasional resizing of the internal array).

This post has been edited by EdwinNameless: 15 December 2009 - 04:11 AM

Was This Post Helpful? 1
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users