有没有办法在Java中释放内存,类似于C的free()功能?或者将对象设置为null并依赖GC是唯一的选择?
我想删除我创建的对象(跟随你的椭圆),但是我该怎么做呢?
delete follower1;
Run Code Online (Sandbox Code Playgroud)
没用.
编辑:
好的,我会给出更多背景信息.我正在制作一个你可以控制的椭圆形游戏,还有一个跟随你的椭圆形游戏.现在我有了名为:DrawPanel.class的文件,这个类在屏幕上绘制所有内容,并处理碰撞,声音等.我有一个enemy.class,它是玩家之后的椭圆形.我有一个entity.class,这是你可以控制的玩家.如果玩家与追随者相交,我希望我的玩家对象被删除.我这样做的方式:
public void checkCollisions(){
if(player.getBounds().intersects(follower1.getBounds())){
Follower1Alive = false;
player.health = player.health - 10;
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试删除一个文件,在写完文件后,用FileOutputStream.这是我用来编写的代码:
private void writeContent(File file, String fileContent) {
FileOutputStream to;
try {
to = new FileOutputStream(file);
to.write(fileContent.getBytes());
to.flush();
to.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
如图所示,我刷新并关闭流,但是当我尝试删除时,file.delete()返回false.
我删除前检查,看看是否该文件存在,并且:file.exists(),file.canRead(),file.canWrite(),file.canExecute()所有返回true.在调用这些方法后,我尝试file.delete()返回false.
有什么我做错了吗?
Java自动调用垃圾收集器,那么为什么我们需要手动调用垃圾收集?什么时候应该使用System.gc()
据说我们不能garbage collection在java中强制进程.
毕竟,它是一个守护程序线程.
但有时候,为什么我们System.gc( );明确地调用函数?
值得一试吗?任何Pro和Con的?
如果在许多情况下没用,为什么不从Java中弃用此方法?
PS:用例子说明会很有用
我没有内存错误.我正在进行实时聊天应用程序.它运行正常,但是当我在设备上运行应用程序1到2个小时时,堆大小正在增加,当它达到16 MB时,应用程序开始挂起并在一段时间后崩溃并显示,out of memory due to heap size因为生成的堆大小大于分配.
我在HTC Explorer上测试我的应用程序.在我的应用程序中,大多数活动都使用后台线程,因为我正在使用Asnyc Task.
我收到如下错误.
04-30 16:53:14.658: E/AndroidRuntime(5707): FATAL EXCEPTION: MagentoBackground
04-30 16:53:14.658: E/AndroidRuntime(5707): java.lang.OutOfMemoryError: (Heap Size=20167KB, Allocated=16063KB, Bitmap Size=355KB)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.util.ByteArrayBuffer.<init>(ByteArrayBuffer.java:53)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.io.AbstractSessionInputBuffer.init(AbstractSessionInputBuffer.java:82)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.io.SocketInputBuffer.<init>(SocketInputBuffer.java:98)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.SocketHttpClientConnection.createSessionInputBuffer(SocketHttpClientConnection.java:83)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.conn.DefaultClientConnection.createSessionInputBuffer(DefaultClientConnection.java:170)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.SocketHttpClientConnection.bind(SocketHttpClientConnection.java:106)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.conn.DefaultClientConnection.openCompleted(DefaultClientConnection.java:129)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:173)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-30 16:53:14.658: E/AndroidRuntime(5707): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-30 …Run Code Online (Sandbox Code Playgroud) 我的应用程序有很多迭代.直到现在我还没有遇到任何内存问题.但是从代码级别我可以怀疑很少有地方会导致内存泄漏和内存不足问题.我正在考虑手动调用垃圾收集器.手动调用垃圾收集器是一种好习惯吗?
我开发了一个在Android上使用大量图像的应用程序.
可绘制文件夹中有很多图像说超过100,我正在开发图像动画应用程序.我用imageview来显示GIF图像.我已经将Split gif图像的概念用于多个PNG格式图像,然后使用它.
每次用户进入应用程序时,我都可以看到内存越来越多,直到用户获得java.lang.OutOfMemoryError.
那么处理许多图像的最佳/正确方法是什么?
以下是我的代码:
dog_animation.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/DogView"
android:orientation="vertical" >
<ImageView
android:id="@+id/dog_animation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.19" />
Run Code Online (Sandbox Code Playgroud)
dog_animation.xml(Drawable文件夹)
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<item
android:drawable="@drawable/image"
android:duration="50"/>
<item
android:drawable="@drawable/image1"
android:duration="50"/>
<item
android:drawable="@drawable/image2"
android:duration="50"/>
<item
android:drawable="@drawable/image3"
android:duration="50"/>
<item
android:drawable="@drawable/image4"
android:duration="50"/>
<item
android:drawable="@drawable/image5"
android:duration="50"/>
<item
android:drawable="@drawable/image6"
android:duration="50"/>
<item
android:drawable="@drawable/image7"
android:duration="50"/>
<item
android:drawable="@drawable/image8"
android:duration="50"/>
<item
android:drawable="@drawable/image9"
android:duration="50"/>
<item
android:drawable="@drawable/image10"
android:duration="50"/>
<item
android:drawable="@drawable/image11"
android:duration="50"/>
<item
android:drawable="@drawable/image12"
android:duration="50"/>
<item
android:drawable="@drawable/image13"
android:duration="50"/>
<item
android:drawable="@drawable/image14"
android:duration="50"/>
<item
android:drawable="@drawable/image15"
android:duration="50"/>
<item
android:drawable="@drawable/image16"
android:duration="50"/>
<item
android:drawable="@drawable/image17"
android:duration="50"/> …Run Code Online (Sandbox Code Playgroud) 据我所知,我们不能强迫JAVA中的垃圾收集.我们能做的最好的事情是通过调用System.gc()或发送请求Runtime.gc().这样做会将垃圾收集请求发送到JVM,但不能保证垃圾收集会发生.所以我的问题是:有什么特别的原因,为什么 JVM的设计方式不支持Force Garbage Collection?
我刚刚开始在我的Tomcat webapp中使用Google Guice,并且只要取消部署WAR文件,就会在catalina.out文件中注意到以下内容:
May 16, 2011 5:37:24 PM org.apache.catalina.startup.HostConfig checkResources
INFO: Undeploying context [/app]
May 16, 2011 5:37:24 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads SEVERE: A web application appears to have started a thread named [com.google.inject.internal.util.$Finalizer] but has failed to stop it. This is very likely to create a memory leak.
May 16, 2011 5:37:24 PM org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap SEVERE: A web application created a ThreadLocal with key of type [null] (value [com.google.inject.internal.InjectorImpl$1@10ace8d]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@7e9bed]) but failed to …