java heap space

L

luca schwarz

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>
 
A

Andrew Thompson

luca schwarz wrote:
....
this is strange. when i run my main i get this error:

java.lang.OutOfMemoryError: Java heap space, ...
....
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.
 
L

luca schwarz

Andrew said:
luca schwarz wrote:
...

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
 
D

Dingmou Li

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.


Andrew said:
luca schwarz wrote:
...
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.
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
 
A

Andrew Thompson

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

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.
 
L

luca schwarz

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 said:
Please refrain from top-posting. I find it most confusing.

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.
 
A

Andrew Thompson

luca said:
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 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top