File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Garbage collector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage collector" Watch "Garbage collector" New topic
Author

Garbage collector

Hernan Tavella
Ranch Hand

Joined: Apr 28, 2012
Posts: 42
Hello, how can i do for make this variable be force and choosen by the garbage collector. I try several way but each time that open a new browser or tab, the variable still increasing.
this part of the code:



the variable must be static.
thanks you
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3959
    
  17

Hernan Tavella wrote:Hello, how can i do for make this variable be force and choosen by the garbage collector. I try several way but each time that open a new browser or tab, the variable still increasing.
this part of the code:



the variable must be static.
thanks you

You can't make the variable get garbage collected, because it is a primitive, not an Object, so not applicable for garbage collection. The Object the variable is present in could be garbage collected, but this is a static variable, the Object that holds the variable is the Class object, and there are precious few ways to get the Class object to be garbage collected. The ways I can think of:
1) Use a custom class loader that lets you reload the class.
2) Cause the JVM to restart (such as crashing).

I think both of these are bad ideas. You are looking for an implementation (cause the static context to be reloaded) rather than addressing the problem (you need a fresh variable each time a browser window is started).

My first suggestion would be to get rid of this requirement:
the variable must be static.
. Because that is really the source of the problem you are having. In fact, it sounds like you want an Object to have a new and unique state every time the browser is opened. That means 'not static' as the static requirements suggests 'shared state over all uses in the same JVM.' Clearly you do not want shared state, so you do not want a static variable. What you want is a new instance of the Object for each browser window.


Steve
Hernan Tavella
Ranch Hand

Joined: Apr 28, 2012
Posts: 42
Do you know how to restart the JVM from the code??
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3959
    
  17

Hernan Tavella wrote:Do you know how to restart the JVM from the code??

Of all the possible solutions, that was absolutely the worst. I would encourage you to do anything else.

But to be complete, if you are in an applet, you could probably call some javascript that has a delay, then refreshes the page. Execute the javascript, then do System.exit(0); That would cause the JVM to exit, and hopefully the javascript would restart it.

Note that this would stop all other instances of the Applet, running in other tabs of your browser, as well.

Note2: This is a really bad idea. Don't do this.

Note3: No, I am not familiar with how to call Javascript from an applet, but I believe a way does exist, so you can search for it

Note4: But it is not something you should do. See how complicated things get because you want to make a variable static that you don't want to behave statically? Just make it an instance variable already!
Hernan Tavella
Ranch Hand

Joined: Apr 28, 2012
Posts: 42
I need the variable to be static because, each time that i open a tab or the application the variable has to persist with the same value, but when i close the browser i dont need it anymore and must be restarter. i can't achieve that. i need something that kill that variable when i close the browser (internet explorer).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10917
    
  12

Hernan Tavella wrote:I need the variable to be static because...

Any time someone starts with this, I stop reading. Don't tell us how you need to do something. That's like saying "I need to use a hammer to tighten a nut because...".

Tell us what you are actually trying to do. Don't use Java terms, use English. "I need to keep track of <something>, and each new browser tab needs to know the current value. When a browser tab is closed, I need to decrement the counter". Something like that.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3959
    
  17

And you need to know a bit about the technology you are working on as well. There is usually very little difference between a browser tab and a browser window, except which outer-window the page is attached to. There very well could be no way to distinguish between a new tab and a new window. But then again, if you present more details about what you want to do (instead of how you think it needs to be done) then maybe a solution will present itself.
Hernan Tavella
Ranch Hand

Joined: Apr 28, 2012
Posts: 42
ok understand, this is the scenario when and a user log in a page on a especific browser a variable inicialiced and persists in each new tab on the browser that the user open, the problem is when a user close the browser and open again and start working the variable keep the same value and don't restart. we are working on JSF, i going to put the java so you can see it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10917
    
  12

That's not exactly what we meant. You're still saying "I want to use a java int to count something".

Tell us what contador is supposed to represent. tell us why you need a counter, and what it is counting. Tell us when it needs to go up, when it needs to go down, and when it needs to be reset to 0.

Do NOT use terms like int, float, static, private...just simple English words.

Do NOT post code. You need to be able to explain the issue in words before you write a single line of code.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3959
    
  17

The fact that you are running server side code is incredibly important here. Your problem is much worse than you think it is. You should never have static data in a web application. If you want a counter that is per user, it should be in the session, not the class at all.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

According to what i understand steve gave a good idea of a session.
Use cookie for client side storage or use something like serialization.


OCPJP
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7077
    
  16

fred rosenberger wrote:Tell us what contador is supposed to represent. tell us why you need a counter, and what it is counting. Tell us when it needs to go up, when it needs to go down, and when it needs to be reset to 0.
Do NOT use terms like int, float, static, private...just simple English words.

@Hernan - Basically, what Fred is saying is: tell us WhatNotHow (←click).

This is a design issue, and you will never (or almost never) solve a design issue by coding; so I'd also advise you to StopCoding.

Oddly enough, if you can back up and explain what you're trying to do, we (or even you) may be able to come up with a completely different solution; but you will never do it while you're still thinking in terms of "I must use an int...".

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Hernan Tavella
Ranch Hand

Joined: Apr 28, 2012
Posts: 42
Hello, thank you for answering, it is a great idea of using cookies, but i now i have to figure out how to do it, i never use cookies in java. if someone have any idea from where can i start looking for, i will appreciate it. and of course i have to stop thing on coding but in the work sometimes i forget it and i don't see the problem as i should for resolving. Thank to all.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7077
    
  16

Ishan Pandya wrote:According to what i understand steve gave a good idea of a session.
Use cookie for client side storage or use something like serialization.

Weirdly enough, I thought of precisely the same thing (a cookie); but since I'm no Webby expert, I hesitated to suggest it.

Nice to know that great minds think alike.

Winston
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 213

Winston Gutkowski wrote:
Nice to know that great minds think alike.


hahaha.. thanks for the compliment buddy.. ;)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collector
 
Similar Threads
cant add twice
From Boone's Exam Again
Garbage collection....
Method that keep Garbage Collection form collecting?
Using Unreferenced vs a daemon thread