Author Topic: out of memory when downloading objects  (Read 3963 times)

Offline arianaa30

  • byte
  • *
  • Posts: 44
    • View Profile
out of memory when downloading objects
« on: July 15, 2012, 10:54:31 am »
Hi,
In my demo, I've got almost 20 objects and corresponding textures. Everything at this moment works, but when I just change one of these objects to be downloaded from somewhere, I get out of memory error. Is there any tips for that?
(Finally I have to download all of these 20 objects, similar to waterput)

Here after getting the .md2 file, I add it to the world.
Code: [Select]
waterput = Loader.loadMD2(stream("waterput.md2"),1f);
tm.addTexture("waterput", new Texture(res.openRawResource(R.raw.waterputtex)));
waterput.setTexture("waterput");
waterput.clearAnimation();
waterput.translate(170, -35, -130);
world.addObject(waterput);
waterput.strip();

This is the method which gets a name, receives the object from a URL and returns it.
Code: [Select]
BufferedInputStream stream(String name) throws IOException{
//TODO
String objURL = "http://www.xxx.edu/xxx/"+name;
URLConnection conn = new URL(objURL).openConnection();
conn.connect();
BufferedInputStream bis = new BufferedInputStream(conn
.getInputStream());
Loader.clearCache();

return bis;
}
btw, I do not need all the keyframes in the .md2 files, so I call clearAnimation() to remove them.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory when downloading objects
« Reply #1 on: July 15, 2012, 11:28:22 am »
Have you tried not to use a BufferedInputStream but the InputStream from the connection instead? The loader actually does it's own buffering, so this shouldn't be needed and will consume additional memory (not sure if this is a problem though).

Offline arianaa30

  • byte
  • *
  • Posts: 44
    • View Profile
Re: out of memory when downloading objects
« Reply #2 on: July 15, 2012, 06:55:17 pm »
I corrected it; It should have some effects, however I still get the error. Maybe that is because of this specific object.
I thing I want to know is that does it make much difference in terms of memory for object reading if we are getting them from the raw resources (using openRawResource) or we get them using network connection? Since you know, we do not actually download the object to store it somewhere; but we just get the inputStream.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory when downloading objects
« Reply #3 on: July 15, 2012, 09:09:54 pm »
For the loader, this makes no difference. If there is a difference, it comes from the network stuff and is outside of the scope of jPCT-AE. To fight memory usage, please have a look at this too: http://www.jpct.net/wiki/index.php/Reducing_memory_usage

Offline arianaa30

  • byte
  • *
  • Posts: 44
    • View Profile
Re: out of memory when downloading objects
« Reply #4 on: July 17, 2012, 12:42:25 am »
Egon, a strange problem:
I found out when I want to load Serialized Objects from the web, the demo hangs/crashes!! for other file types (.obj, .md2, .3ds) everything works. Is there anything happening here which I'm not aware of?
(btw, it is in a second thread.)
Code: [Select]

grass = Loader.loadSerializedObject(stream("sergrass.ser"));
tm.addTexture("grass", new Texture(stream("grasstex.jpg")));
grass.setTexture("grass");
grass.translate(-85, -17, -70);
grass.rotateZ((float) Math.PI);
grass.setTransparency(10);
world.addObject(grass);
grass.strip();
« Last Edit: July 17, 2012, 02:02:29 am by arianaa30 »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory when downloading objects
« Reply #5 on: July 17, 2012, 07:11:35 am »
You are not supposed to fiddle around with the World in another one than the rendering thread. So you can load your model in that thread, but you may not add it there. Because if that interferes with the rendering, you'll get random results from display errors to exceptions.

Offline arianaa30

  • byte
  • *
  • Posts: 44
    • View Profile
Re: out of memory when downloading objects
« Reply #6 on: July 17, 2012, 08:41:30 am »
Yes, got it. However I fixed it by moving the .ser object downloading files to the main thread. I was wondering why everything works, except for the loadSerializedObject responsible for loading .ser files!!!!

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory when downloading objects
« Reply #7 on: July 17, 2012, 09:44:17 am »
The loader shouldn't cause the problem but the addObject()-call from outside the rendering thread. Anyway, if it's solved now...

Offline arianaa30

  • byte
  • *
  • Posts: 44
    • View Profile
Re: out of memory when downloading objects
« Reply #8 on: July 18, 2012, 12:48:22 am »
Yeah; Thanks a lot  :)
Btw, so we should put all the downloading stuffs in the 2nd thread, and just put the addObject to the main thread. The threads are concurrently being executed; so when reaching the addObject, how should it find out if that specific object was already downloaded in the 2nd thread, so to add it now?!  :o
« Last Edit: July 18, 2012, 12:50:45 am by arianaa30 »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: out of memory when downloading objects
« Reply #9 on: July 18, 2012, 07:18:52 am »
Just set some flag once the loading thread is done and evaluate that flag in the other thread.