2015-01-19 1 views
0

У меня есть класс путешествиекуча ошибок размера от ArrayList

package com.firstgroup.bbrailapps.pushport.timetable; 

import java.util.ArrayList; 
import java.util.List; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlElements; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 
import javax.xml.datatype.XMLGregorianCalendar; 


/** 
* <p>Java class for anonymous complex type. 
* 
* <p>The following schema fragment specifies the expected content contained within this class. 
* 
* <pre> 
* &lt;complexType> 
* &lt;complexContent> 
*  &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 
*  &lt;sequence> 
*   &lt;choice maxOccurs="unbounded" minOccurs="2"> 
*   &lt;element ref="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}OR"/> 
*   &lt;element ref="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}IP"/> 
*   &lt;element ref="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}DT"/> 
*   &lt;element ref="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}PP"/> 
*   &lt;/choice> 
*   &lt;element ref="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}AS" maxOccurs="unbounded" minOccurs="0"/> 
*  &lt;/sequence> 
*  &lt;attribute name="rid" use="required" type="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}RIDType" /> 
*  &lt;attribute name="uid" type="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}UIDType" /> 
*  &lt;attribute name="ssd" use="required" type="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}RTTIDateType" /> 
*  &lt;attribute name="trainId" type="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}TrainIdType" /> 
*  &lt;attribute name="toc" type="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}TOCType" /> 
*  &lt;attribute name="qtrain" type="{http://www.w3.org/2001/XMLSchema}boolean" default="false" /> 
*  &lt;attribute name="status" use="required" type="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}CIFTrainStatusType" /> 
*  &lt;attribute name="trainCat" use="required" type="{http://www.thalesgroup.com/rtti/XmlTimetable/v7/rttiCTTSchema.xsd}CIFTrainCategoryType" /> 
*  &lt;attribute name="deleted" type="{http://www.w3.org/2001/XMLSchema}boolean" default="false" /> 
*  &lt;attribute name="can" type="{http://www.w3.org/2001/XMLSchema}boolean" default="false" /> 
*  &lt;/restriction> 
* &lt;/complexContent> 
* &lt;/complexType> 
* </pre> 
* 
* 
*/ 
@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "orOrIPOrDT", 
    "as" 
}) 
@XmlRootElement(name = "Journey") 
public class Journey { 

    @XmlElements({ 
     @XmlElement(name = "OR", type = OR.class), 
     @XmlElement(name = "PP", type = PP.class), 
     @XmlElement(name = "IP", type = IP.class), 
     @XmlElement(name = "DT", type = DT.class) 
    }) 
    protected List<Object> orOrIPOrDT; 
    @XmlElement(name = "AS") 
    protected List<AS> as; 
    @XmlAttribute(required = true) 
    protected String rid; 
    @XmlAttribute 
    protected String uid; 
    @XmlAttribute(required = true) 
    protected String ssd; 
    @XmlAttribute 
    protected String trainId; 
    @XmlAttribute 
    protected String toc; 
    @XmlAttribute 
    protected Boolean qtrain; 
    @XmlAttribute(required = true) 
    protected String status; 
    @XmlAttribute(required = true) 
    protected String trainCat; 
    @XmlAttribute 
    protected Boolean deleted; 
    @XmlAttribute 
    protected Boolean can; 

    /** 
    * Gets the value of the orOrIPOrDT property. 
    * 
    * <p> 
    * This accessor method returns a reference to the live list, 
    * not a snapshot. Therefore any modification you make to the 
    * returned list will be present inside the JAXB object. 
    * This is why there is not a <CODE>set</CODE> method for the orOrIPOrDT property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getOROrIPOrDT().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link OR } 
    * {@link PP } 
    * {@link IP } 
    * {@link DT } 
    * 
    * 
    */ 
    public List<Object> getOROrIPOrDT() { 
     if (orOrIPOrDT == null) { 
      orOrIPOrDT = new ArrayList<Object>(); 
     } 
     return this.orOrIPOrDT; 
    } 

    /** 
    * Gets the value of the as property. 
    * 
    * <p> 
    * This accessor method returns a reference to the live list, 
    * not a snapshot. Therefore any modification you make to the 
    * returned list will be present inside the JAXB object. 
    * This is why there is not a <CODE>set</CODE> method for the as property. 
    * 
    * <p> 
    * For example, to add a new item, do as follows: 
    * <pre> 
    * getAS().add(newItem); 
    * </pre> 
    * 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link AS } 
    * 
    * 
    */ 
    public List<AS> getAS() { 
     if (as == null) { 
      as = new ArrayList<AS>(); 
     } 
     return this.as; 
    } 

    /** 
    * Gets the value of the rid property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getRid() { 
     return rid; 
    } 

    /** 
    * Sets the value of the rid property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setRid(String value) { 
     this.rid = value; 
    } 

    /** 
    * Gets the value of the uid property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getUid() { 
     return uid; 
    } 

    /** 
    * Sets the value of the uid property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setUid(String value) { 
     this.uid = value; 
    } 

    /** 
    * Gets the value of the ssd property. 
    * 
    * @return 
    *  possible object is 
    *  {@link XMLGregorianCalendar } 
    *  
    */ 
    public String getSsd() { 
     return ssd; 
    } 

    /** 
    * Sets the value of the ssd property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link XMLGregorianCalendar } 
    *  
    */ 
    public void setSsd(String value) { 
     this.ssd = value; 
    } 

    /** 
    * Gets the value of the trainId property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getTrainId() { 
     return trainId; 
    } 

    /** 
    * Sets the value of the trainId property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setTrainId(String value) { 
     this.trainId = value; 
    } 

    /** 
    * Gets the value of the toc property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getToc() { 
     return toc; 
    } 

    /** 
    * Sets the value of the toc property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setToc(String value) { 
     this.toc = value; 
    } 

    /** 
    * Gets the value of the qtrain property. 
    * 
    * @return 
    *  possible object is 
    *  {@link Boolean } 
    *  
    */ 
    public Boolean isQtrain() { 
      return qtrain; 
    } 

    /** 
    * Sets the value of the qtrain property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link Boolean } 
    *  
    */ 
    public void setQtrain(Boolean value) { 
     this.qtrain = value; 
    } 

    /** 
    * Gets the value of the status property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getStatus() { 
     return status; 
    } 

    /** 
    * Sets the value of the status property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setStatus(String value) { 
     this.status = value; 
    } 

    /** 
    * Gets the value of the trainCat property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public String getTrainCat() { 
     return trainCat; 
    } 

    /** 
    * Sets the value of the trainCat property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setTrainCat(String value) { 
     this.trainCat = value; 
    } 

    /** 
    * Gets the value of the deleted property. 
    * 
    * @return 
    *  possible object is 
    *  {@link Boolean } 
    *  
    */ 
    public Boolean isDeleted() { 
      return deleted; 
    } 

    /** 
    * Sets the value of the deleted property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link Boolean } 
    *  
    */ 
    public void setDeleted(Boolean value) { 
     this.deleted = value; 
    } 

    /** 
    * Gets the value of the can property. 
    * 
    * @return 
    *  possible object is 
    *  {@link Boolean } 
    *  
    */ 
    public Boolean isCan() { 
      return can; 
    } 

    /** 
    * Sets the value of the can property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link Boolean } 
    *  
    */ 
    public void setCan(Boolean value) { 
     this.can = value; 
    } 

} 

, когда я добавить около 60 000 объектов класса путешествия в ArrayList. Он дает ошибку java.lang.outofmemory, и я увеличил максимальный размер до 1 ГБ. Есть ли другой вариант, чем ArrayList для больших элементов.

+0

Не могли бы вы высказать, как вы его загрузили? – vincent

+0

«ArrayList» вряд ли будет вашей проблемой. Хотя, правильное определение «ArrayList» при создании было бы полезным. –

+0

Я прочитал xml-файл, затем добавлю элемент в arraylist. приватный список lstJourney = new ArrayList (); public void endElement (String uri, String localName, String qName) throws SAXException { \t \t // logger.info ("END element" + qName); \t \t if (qName.equalsIgnoreCase ("Journey")) { \t \t \t // printJourney (путешествие); \t \t \t lstJourney.add (путешествие); } } –

ответ

0

ArrayList как таковой, вероятно, не является проблемой; он с большей вероятностью сломается, так как ArrayList выделяет в два раза больше размера массива, когда вы добавляете больше элементов, чем может содержать текущий массив, т. е. когда внутренний массив элементов заполнен, он удваивает массив и копирует все. Поэтому в течение короткого промежутка времени потребуется size()*3 раз памяти (фактические цифры немного отличаются, но вы получаете идею).

Вы можете использовать другие списки, но им нужна еще больше памяти, но в небольших кусках.

Ошибка, указанная выше, также зависит от размера каждого объекта Journey. Объекты 60K - это не так много, но если каждый из них составляет 10 КБ, то для этого требуется 600 МБ или 60% от 1 ГБ, вы предоставили виртуальную машину Java.

Обходные:

  1. Вместо того чтобы держать весь объект в памяти, помните позиции начала и конца в файле. Это всего лишь несколько байтов. Когда кто-то запрашивает объект, используйте RandomFileAccess, чтобы прочитать только XML для одного путешествия и разобрать его.

  2. Вместо того чтобы хранить все объекты в памяти, поместите данные в базу данных.

Смежные вопросы