这是我在 Java 上模拟 Collatz 猜想的程序:
import java.util.*;
public class Collatz {
public static void main(String args[]){
Scanner raj= new Scanner(System.in);
int n;
int k=0;
System.out.print("n? ");
n = raj.nextInt();
while(n > 1){
if(n%2 ==1){
n=3*n+1;
System.out.println(n);
k++;
}
if(n%2==0){
n=n/2;
System.out.println(n);
k++;
}
}
System.out.print("It took " + k + " iterations!");
}
}
Run Code Online (Sandbox Code Playgroud)
当我输入 n=6 时,我得到
3 10 5 16 8 4 2 1 迭代了 8 次!
但是当我输入 n= 63728127 时,我得到
191184382 95592191 286776574 143388287 430164862 215082431 645247294 322623647 …
我目前正在尝试调试我的Python程序的内存使用情况(在Windows上使用CPython 2.7).但不幸的是,我甚至找不到任何方法来可靠地测量它当前使用的内存量.
我一直在使用任务管理器/资源监视器来测量进程内存,但这似乎只对确定峰值内存消耗有用.通常,即使相关对象被垃圾回收,Python也不会减少提交或工作集.
有没有办法找出Python实际使用了多少内存,或者失败了,以迫使它释放未使用的内存?我不想使用任何需要重新编译插补器的东西.
证明它没有释放未使用的内存的行为示例:
(after some calculations) # 290k
gc.collect() # still 290k
x = range(9999999) # 444k
del x # 405k
gc.collect() # 40k
Run Code Online (Sandbox Code Playgroud) 任何人都可以告诉以下代码中的生命周期错误是什么?(从我的实际代码简化)我自己看了看,但我无法弄清楚出了什么问题或如何解决它.当我尝试添加时会出现问题Cell,但我不确定原因.
use std::cell::Cell;
struct Bar<'a> {
bar: &'a str,
}
impl<'a> Bar<'a> {
fn new(foo: &'a Foo<'a>) -> Bar<'a> { Bar{bar: foo.raw} }
}
pub struct Foo<'a> {
raw: &'a str,
cell: Cell<&'a str>,
}
impl<'a> Foo<'a> {
fn get_bar(&self) -> Bar { Bar::new(&self) }
}
Run Code Online (Sandbox Code Playgroud)
编译器错误是
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
--> src/foo.rs:15:32
|
15 | fn get_bar(&self) -> Bar { Bar::new(&self) }
| ^^^^^^^^
Run Code Online (Sandbox Code Playgroud) 抛出异常的行为是否有可能引发不同的异常呢?
为了抛出异常,必须(可选)分配新对象,并调用其构造函数(隐式调用fillinstacktrace).在某些情况下,听起来也称为addSupressed.那么如果没有足够的内存会发生什么?JVM是否可以预先分配内置异常?例如,will(1/0)是否会抛出OutOfMemoryError而不是ArithmeticException?
此外,构造函数是一个方法调用,因此可以自由地抛出其他异常.在这种情况下会发生什么?内置的异常会抛出吗?即使你没有显式抛出,似乎也可能得到一个StackOverflowError.