Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Memory allocation ?

Started by Endemoniada Mar 17, 2005 at 1:43 PM 3 replies 1.3k views
Original Post
Endemoniada
Endemoniada
Hi guys, I'm use to C(++) and can't really understand what's going on here: while(bRun){ int iSize=ComputeSize(); // my function byte[]=new byte[iSize]; // some stuff; } If a new sized array gets allocated after one iteration of the loop does the original one get cleaned up ? Is what I'm doing safe ? Thanks.
aidan_walsh
aidan_walsh
The originally referenced byte array will be left for the garbage collector. What you're doing is quite safe, as long as the byte arrays are not uber large.
www.aidanwalsh(.net)(.info)
Optus
Optus
I maybe mistaken (someone come in here and correct me! I always need to be learning about Java memory management) I think in this situation the GC won't even get called for the array of bytes, it will be created and destroyed on the heap when out of the scope of the variable. So, yea it's safe to do this.
HellCreator
HellCreator
It's okay :) it is save
gc called periodicaly when app in low res or references count to allocated memmory point geting zero thats mean no object hold this object,
but if in while loop at end You have finished with array and dont whant to wait to gc
just set byte=null; then System.gc(); or Runtime.getRuntime().gc();
it is only suggestion to vm
Can't be root?Reboot!mount -r /home/hell
CaptainJester
CaptainJester
Quote:
Original post by Optus
I maybe mistaken (someone come in here and correct me! I always need to be learning about Java memory management) I think in this situation the GC won't even get called for the array of bytes, it will be created and destroyed on the heap when out of the scope of the variable. So, yea it's safe to do this.

Half right. Heap memory is always reclaimed by the gc. Stack memory is reclaimed as soon as the method it was created for returns.
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.