java heap space

Discussion in 'Java' started by luca schwarz, Jan 19, 2007.

  1. luca schwarz

    luca schwarz Guest

    hello.
    this is strange. when i run my main i get this error:

    java.lang.OutOfMemoryError: Java heap space,

    but my programm doesn't have wild code like arrays, if-else-stuff etc...

    btw: it ran yesterday without problems.

    <code>
    import javax.swing.UIManager;

    public class HIS_Projekt
    {
    public static void main(String[] args) throws Exception
    {
    try
    {

    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch( Exception ex )
    {
    ex.printStackTrace();
    }
    HIS_Control control = new HIS_Control();
    }

    }
    </code>
     
    luca schwarz, Jan 19, 2007
    #1
    1. Advertising

  2. luca schwarz wrote:
    ....
    > this is strange. when i run my main i get this error:
    >
    > java.lang.OutOfMemoryError: Java heap space,

    ...
    > <code>

    ....
    > HIS_Control control = new HIS_Control();


    The problem apparently lies in HIS_Control(), he
    has apparently grown too fat, lazy and forgetful.
    So the solution to this one, is easy!

    HER_Control control = new HER_Control();

    If that does not fix the problem, you may need to
    be a little more specific about what happens in
    the mentioned code. An SSCCE* is generally
    *best* for explaining.

    * <http://www.physci.org/codes/sscce>

    Andrew T.
     
    Andrew Thompson, Jan 19, 2007
    #2
    1. Advertising

  3. luca schwarz

    luca schwarz Guest

    Andrew Thompson schrieb:
    > luca schwarz wrote:
    > ...
    >> this is strange. when i run my main i get this error:
    >>
    >> java.lang.OutOfMemoryError: Java heap space,

    > ..
    >> <code>

    > ...
    >> HIS_Control control = new HIS_Control();

    >
    > The problem apparently lies in HIS_Control(), he
    > has apparently grown too fat, lazy and forgetful.
    > So the solution to this one, is easy!
    >
    > HER_Control control = new HER_Control();
    >
    > If that does not fix the problem, you may need to
    > be a little more specific about what happens in
    > the mentioned code. An SSCCE* is generally
    > *best* for explaining.
    >
    > * <http://www.physci.org/codes/sscce>
    >
    > Andrew T.
    >

    LOL. Good point :)
    Tried it with HER, but nothing changed.

    I believe to make a big mistake in my program-architecture.
    To keep the problem small I reduced my classes to three.
    The main, the control, and the model:

    Main:
    <code>
    public class Start {

    public static void main(String[] args)
    {
    new Control();
    }

    }
    </code>

    Control:
    <code>
    public class Control
    {
    // Gui_Start guiStart = new Gui_Start();
    Model theModel = new Model();

    public Control()
    {
    //guiStart.setVisible(true);
    System.out.print("Control-Constructor run");
    }
    }
    </code>

    Model:
    <code>
    public class Model
    {
    Control theControl = new Control();
    }
    </code>

    If I run the main, I get an Exception.
    If I activate the object Gui_Start and the guiStart.setVisible(true) my
    computer crashes badly (cpu to the max and have to kill the eclipse
    javaw-process).

    Is it a thread-problem?
    Isn't it allowed to create objects of other classes in each single
    class? At least this is OOP!?

    Thanks for any help.

    Luca
     
    luca schwarz, Jan 21, 2007
    #3
  4. luca schwarz

    Dingmou Li Guest

    In your code, creating an instance of Control will create an instance
    of Model, while creating an instance of Model will create an instance
    of Control, so another instance of Model is created....And this process
    runs on and on and on. That's why you have OutOfMemory problem.


    On Jan 21, 6:40 pm, luca schwarz <> wrote:
    > Andrew Thompson schrieb:
    >
    > > luca schwarz wrote:
    > > ...
    > >> this is strange. when i run my main i get this error:

    >
    > >> java.lang.OutOfMemoryError: Java heap space,

    > > ..
    > >> <code>

    > > ...
    > >> HIS_Control control = new HIS_Control();

    >
    > > The problem apparently lies in HIS_Control(), he
    > > has apparently grown too fat, lazy and forgetful.
    > > So the solution to this one, is easy!

    >
    > > HER_Control control = new HER_Control();

    >
    > > If that does not fix the problem, you may need to
    > > be a little more specific about what happens in
    > > the mentioned code. An SSCCE* is generally
    > > *best* for explaining.

    >
    > > * <http://www.physci.org/codes/sscce>

    >
    > > Andrew T.LOL. Good point :)

    > Tried it with HER, but nothing changed.
    >
    > I believe to make a big mistake in my program-architecture.
    > To keep the problem small I reduced my classes to three.
    > The main, the control, and the model:
    >
    > Main:
    > <code>
    > public class Start {
    >
    > public static void main(String[] args)
    > {
    > new Control();
    > }
    >
    > }</code>
    >
    > Control:
    > <code>
    > public class Control
    > {
    > // Gui_Start guiStart = new Gui_Start();
    > Model theModel = new Model();
    >
    > public Control()
    > {
    > //guiStart.setVisible(true);
    > System.out.print("Control-Constructor run");
    > }}</code>
    >
    > Model:
    > <code>
    > public class Model
    > {
    > Control theControl = new Control();}</code>
    >
    > If I run the main, I get an Exception.
    > If I activate the object Gui_Start and the guiStart.setVisible(true) my
    > computer crashes badly (cpu to the max and have to kill the eclipse
    > javaw-process).
    >
    > Is it a thread-problem?
    > Isn't it allowed to create objects of other classes in each single
    > class? At least this is OOP!?
    >
    > Thanks for any help.
    >
    > Luca
     
    Dingmou Li, Jan 22, 2007
    #4
  5. luca schwarz wrote:
    > Andrew Thompson schrieb:
    > > luca schwarz wrote:


    Please refrain from top-posting. I find it most confusing.

    > > ...
    > >> this is strange. when i run my main i get this error:
    > >>
    > >> java.lang.OutOfMemoryError: Java heap space,

    > > ..


    Your complete code tells the story. But first, I
    will repost your code as an SSCCE*. An SSCCE
    is a form of the code inteded to be a (short) self-contained,
    compilable example of the problem.

    Your code fulfilled most of that description, but
    an extra tip is to rework it so it is all in 'one Java file'**
    which can be achieved by demoting classes with no
    'main()' from 'public' to '' (default/package).

    ** This is not good for 'real' code, but we are just
    trying to sort a basic problem - so it is fine.

    Here is what I mean..
    <sscce>
    public class Start {

    public static void main(String[] args)
    {
    new Control();
    }
    }

    class Control
    {
    // GOTO create model
    Model theModel= new Model();

    public Control()
    {
    System.out.print("Control-Constructor run");
    }
    }

    class Model
    {
    // GOTO create control
    Control theControl= new Control();
    }
    </sscce>

    I put some comments in that code, to indicate why it
    is failing, but here is a version that (to the best of my
    current understanding of the code - which is 'not much')
    does what you are attempting to achieve (without the
    recursive element!).

    <sscce>
    public class StartOnce {

    public static void main(String[] args)
    {
    new Control();
    }
    }

    class Control
    {
    // Declare model
    Model theModel;

    public Control()
    {
    // hand the model a reference to this Control
    theModel = new Model(this);
    System.out.println("Control-Constructor run");
    }
    }

    class Model
    {
    // Declare control
    Control theControl;

    Model(Control control) {
    // associate the Control with our Model
    theControl = control;
    }
    }
    </sscce>

    Each of control and model have a reference to 'the'
    *single* instance of the other.

    * The full description of the SSCCE, along with
    why it is so handy for both debuggin and code
    postings, can be found here.
    <http://www.physci.org/codes/sscce/>

    BTW - these are very simple questions, and there is
    a group better suited to those learning Java, it is..
    comp.lang.java.help
    ...I highly recommend it.

    Andrew T.
     
    Andrew Thompson, Jan 22, 2007
    #5
  6. luca schwarz

    luca schwarz Guest

    Sorry for beeing a little confused. I try to solve problems by myself
    and have to learn about the conventions of writing posts.

    My main problem is to learn how to manage this: i have many classes and
    all of them should be able to use the methods of every other class,
    without get recursive.
    I checked the net, but I couldn't find the any solution. No problem with
    2 classes..but with more i get confused...sorry.

    Here is the Code:

    <sscce>
    public class Go
    {
    public static void main(String[] args)
    {
    new Gui_Start();
    }
    }

    public class Gui_Start extends JFrame
    {
    HIS_Go theGo;
    Control theControl;
    Gui_Login theLogin;

    Gui_Start(Control control)
    {
    this.theControl = control;
    }
    Gui_Start(Gui_Login guiLogin)
    {
    this.theLogin = guiLogin;
    }
    Gui_Start()
    {
    if(this.isShowing())
    {
    this.dispose();
    }
    else
    {
    initComponents();
    this.setVisible(true);
    }
    }
    private int buttonActive = 0;
    private void setButtonActive(int i)
    {
    this.buttonActive = i;
    }
    public int getButtonActive()
    {
    return this.buttonActive;
    }
    private void button_personalActionPerformed(ActionEvent e){
    setButtonActive(1);
    new Gui_Login(this);
    }
    private void button_stationActionPerformed(ActionEvent e){
    setButtonActive(2);
    new Gui_Login(this);
    }
    private void initComponents()
    {//gui_stuff
    }
    }

    public class Gui_Login extends JFrame
    {
    Gui_Start guiStart;
    Gui_Login(Gui_Start guiStart)
    {
    if(this.isShowing())
    {
    this.dispose();
    }
    else
    {
    initComponents();
    this.setVisible(true);
    }
    }
    private boolean checkIs = false;
    private final String loginName = "super";
    private final String loginPass = "super";
    private String inputLoginName = null;
    private String inputLoginPass = null;
    private void loginCheck()
    {
    inputLoginName = getField_login().getText().toString();
    inputLoginPass = getField_password().getText().toString();
    if(inputLoginName.equals(loginName) &&
    inputLoginPass.equals(loginPass))
    {
    this.checkIs = true;
    int buttonActive = guiStart.getButtonActive(); //here i get the problem
    switch(buttonActive)
    {
    case 1: new Gui_Personal(this); //if i make buttonActive in Gui_Start
    static, i dont get any exceptions, but the gui_personal will not open
    Gui_Personal-Fenster geht nicht auf.
    break;
    case 2: new Gui_Station(this);//same as case 1
    break;
    }
    }
    else
    {
    JOptionPane.showMessageDialog(null, "Wrong login!", "Login failure",
    JOptionPane.ERROR_MESSAGE);
    }
    }
    private void okButtonActionPerformed(ActionEvent e)
    {
    new Gui_Start(this);
    loginCheck();
    }
    private void cancelButtonActionPerformed(ActionEvent e) {
    // TODO add your code here
    }
    public JTextField getField_login()
    {
    return this.field_login;
    }
    public JPasswordField getField_password()
    {
    return this.field_password;
    }
    private void initComponents()
    {//guiStuff
    }
    }

    public class Gui_Station extends JFrame
    {
    Gui_Station (Gui_Login guiLogin)
    {
    initComponents();
    this.setVisible(true);
    }
    private void initComponents()
    {//Gui-Stuff
    }
    </sscce>

    Luca S.


    Andrew Thompson schrieb:
    > luca schwarz wrote:
    >> Andrew Thompson schrieb:
    >>> luca schwarz wrote:

    >
    > Please refrain from top-posting. I find it most confusing.
    >
    >>> ...
    >>>> this is strange. when i run my main i get this error:
    >>>>
    >>>> java.lang.OutOfMemoryError: Java heap space,
    >>> ..

    >
    > Your complete code tells the story. But first, I
    > will repost your code as an SSCCE*. An SSCCE
    > is a form of the code inteded to be a (short) self-contained,
    > compilable example of the problem.
    >
    > Your code fulfilled most of that description, but
    > an extra tip is to rework it so it is all in 'one Java file'**
    > which can be achieved by demoting classes with no
    > 'main()' from 'public' to '' (default/package).
    >
    > ** This is not good for 'real' code, but we are just
    > trying to sort a basic problem - so it is fine.
    >
    > Here is what I mean..
    > <sscce>
    > public class Start {
    >
    > public static void main(String[] args)
    > {
    > new Control();
    > }
    > }
    >
    > class Control
    > {
    > // GOTO create model
    > Model theModel= new Model();
    >
    > public Control()
    > {
    > System.out.print("Control-Constructor run");
    > }
    > }
    >
    > class Model
    > {
    > // GOTO create control
    > Control theControl= new Control();
    > }
    > </sscce>
    >
    > I put some comments in that code, to indicate why it
    > is failing, but here is a version that (to the best of my
    > current understanding of the code - which is 'not much')
    > does what you are attempting to achieve (without the
    > recursive element!).
    >
    > <sscce>
    > public class StartOnce {
    >
    > public static void main(String[] args)
    > {
    > new Control();
    > }
    > }
    >
    > class Control
    > {
    > // Declare model
    > Model theModel;
    >
    > public Control()
    > {
    > // hand the model a reference to this Control
    > theModel = new Model(this);
    > System.out.println("Control-Constructor run");
    > }
    > }
    >
    > class Model
    > {
    > // Declare control
    > Control theControl;
    >
    > Model(Control control) {
    > // associate the Control with our Model
    > theControl = control;
    > }
    > }
    > </sscce>
    >
    > Each of control and model have a reference to 'the'
    > *single* instance of the other.
    >
    > * The full description of the SSCCE, along with
    > why it is so handy for both debuggin and code
    > postings, can be found here.
    > <http://www.physci.org/codes/sscce/>
    >
    > BTW - these are very simple questions, and there is
    > a group better suited to those learning Java, it is..
    > comp.lang.java.help
    > ..I highly recommend it.
    >
    > Andrew T.
    >
     
    luca schwarz, Jan 22, 2007
    #6
  7. luca schwarz wrote:
    > Sorry for beeing a little confused.


    You still are.

    >...I try to solve problems by myself
    > and have to learn about the conventions of writing posts.


    True. One thing I requested you stop doing is 'top-post'.
    Do you understand what it means?
    Note that whenever I make a comment, I will put it directly
    after what I am commenting on? The ideal way to post is to
    do that, then trim any text not relevant to your reply.

    > My main problem is to learn how to manage this:

    ....

    Not as I see it. I think your main problems at this
    instant are ..
    a) carefully reading the instructions given*
    b) getting people to continue helping you**

    * I gave you a link to the SSCCE document earlier,
    it details what it takes to make an SSCCE, here
    is that link again.

    >> * The full description of the SSCCE, along with
    >> why it is so handy for both debuggin and code
    >> postings, can be found here.
    >> <http://www.physci.org/codes/sscce/>


    The code you posted is not only hard to read,
    since you removed all indentation (please put
    'space' characters to give code a little indent,
    as I did in my first example), but is sure
    not an SSCCE.

    ** When people do not read what I have to say,
    carefully, I become bored very quickly. You
    almost have me at that stage already.

    Andrew T.
     
    Andrew Thompson, Jan 22, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,955
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,766
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,080
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    765
    Michael Hudson
    Oct 24, 2003
  5. Ajay

    How to increase stack space/heap space

    Ajay, May 11, 2006, in forum: C Programming
    Replies:
    9
    Views:
    579
Loading...

Share This Page